"""
MTN Syria source-activity adapter.

Verified from the uploaded HAR:
- GET/POST /EXTERNAL_WEB/login
- GET /EXTERNAL_WEB/Transaction/TansactionFilter
- POST /EXTERNAL_WEB/Transaction/TansactionView
- GET /EXTERNAL_WEB/api/TransactionView for pagination
- exact listing fields and status/type dictionaries embedded by the provider page

The adapter retrieves all statuses, then separates the two local sources safely:
- وحدات ام تي ان -> TOPUP PREPAID
- فواتير ام تي ان -> TOPUP POSTPAID

The MTN listing returns local timestamps without an offset. They are interpreted using
only the explicit timezone carried by the requested audit period; no server timezone is
invented inside this adapter.
"""
from __future__ import annotations

import math
import re
from datetime import date, datetime, timedelta
from pathlib import Path
from typing import Any, Iterable, Mapping
from urllib.parse import parse_qs, urljoin, urlparse

import requests
from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from source_activity_runtime.exceptions import ProviderDataError
from source_activity_runtime.models import ExtractionContext, ProviderRecord
from source_activity_runtime.provider import SourceActivityProvider
from source_activity_runtime.utils import atomic_write_json, sanitize


_BASE_URL = "https://services.mtnsyr.com:56303/EXTERNAL_WEB"
_PAGE_SIZE = 10  # Exact value used by the verified provider page.

_TYPE_PREPAID = "TOPUP PREPAID"
_TYPE_POSTPAID = "TOPUP POSTPAID"

_TYPE_TEXT_TO_CODE = {
    "شحن مسبق الدفع": _TYPE_PREPAID,
    "شحن لاحق الدفع": _TYPE_POSTPAID,
    _TYPE_PREPAID: _TYPE_PREPAID,
    _TYPE_POSTPAID: _TYPE_POSTPAID,
}

_STATUS_TO_CANONICAL = {
    "1": "completed",
    "success": "completed",
    "نجاح": "completed",
    "0": "failed",
    "failed": "failed",
    "فشل": "failed",
    "2": "pending",
    "pending": "pending",
    "قيد الانتظار": "pending",
    "-1": "cancelled",
    "cancelled": "cancelled",
    "canceled": "cancelled",
    "ملغي": "cancelled",
    "ملغى": "cancelled",
}


class MtnPhpProvider(SourceActivityProvider):
    def __init__(self, session: requests.Session | None = None) -> None:
        self._session = session or self._build_session()
        self._metadata: dict[str, Any] = {
            "coverage_verified": False,
            "zero_records_authoritative": False,
            "coverage_policy": None,
            "pagination": "pageNumber/pageSize",
            "page_size": _PAGE_SIZE,
            "pages_fetched": 0,
            "rows_seen": 0,
            "rows_in_period": 0,
            "rows_of_other_types": 0,
            "rows_outside_period": 0,
            "duplicate_rows": 0,
            "provider_record_key_field": "transactionId",
            "provider_transaction_id_field": "transactionId",
            "provider_reference_field": "rowId",
            "status_mapping": dict(_STATUS_TO_CANONICAL),
        }

    def iter_records(self, context: ExtractionContext) -> Iterable[ProviderRecord]:
        credentials = context.source.credentials
        user = self._required(credentials, ("username", "user", "mobile", "login"), "username")
        pwd = self._required(credentials, ("password", "pass", "pwd"), "password")

        options = context.source.provider_options
        request_options = options.get("request_options") or {}
        if not isinstance(request_options, Mapping):
            raise ProviderDataError("MTN request_options must be an object")

        base_url = str(options.get("base_url") or options.get("api_url") or _BASE_URL).strip().rstrip("/")
        if base_url != _BASE_URL:
            raise ProviderDataError(
                f"MTN base_url must match the HAR-verified endpoint: {_BASE_URL}"
            )

        timeout = self._positive_int(
            request_options.get("timeout", options.get("timeout", 120)),
            "timeout",
            maximum=300,
        )
        max_pages = self._positive_int(
            request_options.get("max_pages", 100000),
            "max_pages",
            maximum=100000,
        )
        verify_tls = self._boolean(request_options.get("verify_tls", False), "verify_tls")

        expected_type = self._resolve_expected_type(context)
        self._metadata.update({
            "base_url": base_url,
            "expected_transaction_type": expected_type,
            "verify_tls": verify_tls,
            "period_timezone": str(context.period_from.tzinfo),
        })

        raw_dir = context.source_directory / "raw"
        raw_dir.mkdir(parents=True, exist_ok=True)

        browser_headers = {
            "User-Agent": (
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                "AppleWebKit/537.36 (KHTML, like Gecko) "
                "Chrome/150.0.0.0 Safari/537.36"
            ),
            "Accept": (
                "text/html,application/xhtml+xml,application/xml;q=0.9,"
                "image/avif,image/webp,image/apng,*/*;q=0.8"
            ),
            "Accept-Language": "ar-SY,ar;q=0.9,en-US;q=0.8,en;q=0.7",
        }

        login_url = base_url + "/login"
        home_url = base_url + "/"
        filter_url = base_url + "/Transaction/TansactionFilter"
        view_url = base_url + "/Transaction/TansactionView"
        api_url = base_url + "/api/TransactionView"

        login_page = self._session.get(
            login_url,
            headers=browser_headers,
            timeout=timeout,
            verify=verify_tls,
            allow_redirects=True,
        )
        self._require_http_ok(login_page, "MTN login page", allow_redirect=False)

        login_response = self._session.post(
            login_url,
            data={"username": user, "password": pwd},
            headers={
                **browser_headers,
                "Content-Type": "application/x-www-form-urlencoded",
                "Origin": "https://services.mtnsyr.com:56303",
                "Referer": login_url,
            },
            timeout=timeout,
            verify=verify_tls,
            allow_redirects=False,
        )
        if login_response.status_code not in (301, 302, 303, 307, 308):
            raise ProviderDataError(
                "MTN login did not return the HAR-verified redirect; "
                f"status={login_response.status_code}"
            )

        # Complete the redirect chain explicitly before opening the transaction filter.
        self._session.get(
            home_url,
            headers={**browser_headers, "Referer": login_url},
            timeout=timeout,
            verify=verify_tls,
            allow_redirects=True,
        )

        filter_response = self._session.get(
            filter_url,
            headers={**browser_headers, "Referer": home_url},
            timeout=timeout,
            verify=verify_tls,
            allow_redirects=True,
        )
        self._require_http_ok(filter_response, "MTN transaction filter", allow_redirect=False)
        self._ensure_authenticated_html(filter_response, "MTN transaction filter")

        csrf = self._csrf_token(filter_response.text)
        if not csrf:
            raise ProviderDataError("MTN transaction filter has no _csrf input")

        provider_tz = context.period_from.tzinfo
        if provider_tz is None or context.period_from.utcoffset() is None:
            raise ProviderDataError("MTN audit period must include an explicit timezone")

        period_from_local = context.period_from.astimezone(provider_tz)
        period_to_local = context.period_to.astimezone(provider_tz)
        query_from = period_from_local.date()
        # The verified provider form uses date boundaries. Query through the day after
        # the requested end date, then enforce the exact half-open window locally.
        query_to = period_to_local.date() + timedelta(days=1)

        form_data = [
            ("_csrf", csrf),
            ("FROM_DATE", query_from.isoformat()),
            ("TO_DATE", query_to.isoformat()),
            ("TRANSACTION_ID", ""),
            ("STATUS", ""),
            ("TO_AGENT", ""),
            ("TYPE", ""),
            ("_csrf", csrf),
        ]

        view_response = self._session.post(
            view_url,
            data=form_data,
            headers={
                **browser_headers,
                "Content-Type": "application/x-www-form-urlencoded",
                "Origin": "https://services.mtnsyr.com:56303",
                "Referer": filter_url,
            },
            timeout=timeout,
            verify=verify_tls,
            allow_redirects=True,
        )
        self._require_http_ok(view_response, "MTN transaction view", allow_redirect=False)
        self._ensure_authenticated_html(view_response, "MTN transaction view")

        initial = self._parse_initial_page(view_response.text)
        total_count = initial["total_count"]
        hidden = initial["hidden"]
        rows = initial["rows"]

        atomic_write_json(
            raw_dir / "transaction-page-000001.json",
            sanitize({
                "query_from": query_from.isoformat(),
                "query_to": query_to.isoformat(),
                "total_count": total_count,
                "hidden": hidden,
                "transactionList": rows,
            }),
        )

        pages = max(1, math.ceil(total_count / _PAGE_SIZE)) if total_count else 1
        if pages > max_pages:
            raise ProviderDataError(
                f"MTN pagination requires {pages} pages, exceeding max_pages={max_pages}"
            )

        seen: dict[str, dict[str, Any]] = {}
        for row in rows:
            self._remember_row(seen, row)
        self._metadata["pages_fetched"] = 1

        for page in range(2, pages + 1):
            payload = self._fetch_json_page(
                api_url=api_url,
                filter_url=view_url,
                hidden=hidden,
                page=page,
                total_count=total_count,
                timeout=timeout,
                verify_tls=verify_tls,
                browser_headers=browser_headers,
            )
            page_total = self._non_negative_int(payload.get("total_count"), "MTN total_count")
            if page_total != total_count:
                raise ProviderDataError(
                    "MTN total_count changed during pagination: "
                    f"initial={total_count}, page_{page}={page_total}"
                )
            page_rows = payload.get("transactionList")
            if not isinstance(page_rows, list):
                raise ProviderDataError("MTN transactionList must be an array")

            atomic_write_json(
                raw_dir / f"transaction-page-{page:06d}.json",
                sanitize(payload),
            )
            self._metadata["pages_fetched"] = page

            for raw_row in page_rows:
                if not isinstance(raw_row, Mapping):
                    raise ProviderDataError("MTN transaction row must be an object")
                row = self._normalize_api_row(raw_row)
                self._remember_row(seen, row)

        self._metadata["provider_total_count"] = total_count
        self._metadata["unique_rows"] = len(seen)

        for row in seen.values():
            self._metadata["rows_seen"] += 1

            transaction_type = self._canonical_type(row.get("type"))
            if transaction_type != expected_type:
                self._metadata["rows_of_other_types"] += 1
                continue

            occurred_at = self._parse_provider_time(row.get("insertDate"), provider_tz)
            if occurred_at < context.period_from or occurred_at >= context.period_to:
                self._metadata["rows_outside_period"] += 1
                continue

            transaction_id = self._stable_identifier(
                row.get("transactionId"),
                row.get("rowId"),
            )
            row_id = self._optional_text(row.get("rowId"))
            status = self._canonical_status(row.get("status"))
            amount = self._required_decimal_text(row.get("amount"), "amount")
            subscriber = self._optional_text(row.get("to_agent"))

            self._metadata["rows_in_period"] += 1
            yield ProviderRecord(
                provider_record_key=f"transaction:{transaction_id}",
                occurred_at=occurred_at,
                record_type=transaction_type,
                provider_status=status,
                provider_transaction_id=transaction_id,
                provider_reference=row_id if row_id and row_id != transaction_id else None,
                subscriber_identifier=subscriber,
                amount=amount,
                cost_amount=amount,
                commission_amount=None,
                currency=self._optional_text(options.get("currency_code")) or "SYR",
                balance_before=self._optional_decimal_text(row.get("senderBalanceValuebefore")),
                balance_after=self._optional_decimal_text(row.get("senderBalanceValueafter")),
                description=self._description(transaction_type, status),
                raw_payload=sanitize({
                    **dict(row),
                    "reconciliation_identifiers": {
                        "phone": subscriber,
                    },
                }),
            )

        self._metadata.update({
            "coverage_verified": True,
            "zero_records_authoritative": True,
            "coverage_policy": "verified_complete_filtered_period",
        })

    def result_metadata(self) -> Mapping[str, Any]:
        return dict(self._metadata)

    def _fetch_json_page(
        self,
        *,
        api_url: str,
        filter_url: str,
        hidden: Mapping[str, str],
        page: int,
        total_count: int,
        timeout: int,
        verify_tls: bool,
        browser_headers: Mapping[str, str],
    ) -> dict[str, Any]:
        params = {
            "fromDate": hidden.get("fromDate", ""),
            "toDate": hidden.get("toDate", ""),
            "transaction_id": hidden.get("transaction_id", ""),
            "status": hidden.get("status", ""),
            "from": hidden.get("from", ""),
            "to": hidden.get("to", ""),
            "requester": hidden.get("requester", ""),
            "type": hidden.get("type", ""),
            "pageNumber": page,
            "pageSize": _PAGE_SIZE,
            "total_count": total_count,
        }
        response = self._session.get(
            api_url,
            params=params,
            headers={
                **browser_headers,
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "X-Requested-With": "XMLHttpRequest",
                "Referer": filter_url,
            },
            timeout=timeout,
            verify=verify_tls,
            allow_redirects=True,
        )
        self._require_http_ok(response, f"MTN transaction API page={page}", allow_redirect=False)
        try:
            payload = response.json()
        except Exception as exc:
            raise ProviderDataError(
                f"MTN transaction API page={page} did not return JSON"
            ) from exc
        if not isinstance(payload, dict):
            raise ProviderDataError(f"MTN transaction API page={page} must return an object")
        return payload

    @staticmethod
    def _parse_initial_page(html: str) -> dict[str, Any]:
        soup = BeautifulSoup(html or "", "html.parser")
        table = soup.find("table", id="table1")
        if table is None:
            raise ProviderDataError("MTN transaction view has no table#table1")

        hidden_names = (
            "total_count",
            "fromDate",
            "toDate",
            "transaction_id",
            "status",
            "from",
            "to",
            "requester",
            "type",
        )
        hidden: dict[str, str] = {}
        for name in hidden_names:
            node = soup.find("input", {"name": name}) or soup.find("input", {"id": name})
            hidden[name] = str(node.get("value") or "") if node else ""

        total_count = MtnPhpProvider._non_negative_int(
            hidden.get("total_count"),
            "MTN total_count",
        )
        if not hidden.get("fromDate") or not hidden.get("toDate"):
            raise ProviderDataError("MTN transaction view is missing date filter state")
        if not hidden.get("from"):
            raise ProviderDataError("MTN transaction view is missing the authenticated sender account")

        rows: list[dict[str, Any]] = []
        body = table.find("tbody")
        for tr in body.find_all("tr", recursive=False) if body else []:
            cells = tr.find_all("td", recursive=False)
            if not cells:
                continue
            if len(cells) != 11:
                raise ProviderDataError(
                    f"MTN transaction table row has {len(cells)} cells; expected 11"
                )

            link = cells[10].find("a", href=True)
            row_id = None
            if link:
                query = parse_qs(urlparse(str(link.get("href") or "")).query)
                values = query.get("rowId") or []
                row_id = values[0] if values else None

            rows.append({
                "transactionId": cells[0].get_text(" ", strip=True),
                "type": cells[1].get("data-arabic") or cells[1].get_text(" ", strip=True),
                "from_agent": cells[2].get_text(" ", strip=True),
                "to_agent": cells[3].get_text(" ", strip=True),
                "accountType": cells[4].get_text(" ", strip=True),
                "amount": cells[5].get_text(" ", strip=True),
                "senderBalanceValuebefore": cells[6].get_text(" ", strip=True),
                "senderBalanceValueafter": cells[7].get_text(" ", strip=True),
                "status": cells[8].get("data-arabic") or cells[8].get_text(" ", strip=True),
                "insertDate": cells[9].get("data-date") or cells[9].get_text(" ", strip=True),
                "rowId": row_id,
            })

        if len(rows) > _PAGE_SIZE:
            raise ProviderDataError(
                f"MTN initial page returned {len(rows)} rows; verified page size is {_PAGE_SIZE}"
            )
        if len(rows) > total_count:
            raise ProviderDataError("MTN initial page rows exceed total_count")

        return {"total_count": total_count, "hidden": hidden, "rows": rows}

    @staticmethod
    def _normalize_api_row(row: Mapping[str, Any]) -> dict[str, Any]:
        required = (
            "transactionId",
            "type",
            "from_agent",
            "to_agent",
            "amount",
            "status",
            "insertDate",
        )
        missing = [key for key in required if key not in row]
        if missing:
            raise ProviderDataError(
                "MTN transaction API row is missing fields: " + ", ".join(missing)
            )
        return dict(row)

    def _remember_row(self, seen: dict[str, dict[str, Any]], row: Mapping[str, Any]) -> None:
        transaction_id = self._stable_identifier(row.get("transactionId"), row.get("rowId"))
        normalized = dict(row)
        previous = seen.get(transaction_id)
        if previous is None:
            seen[transaction_id] = normalized
            return
        if previous != normalized:
            raise ProviderDataError(
                f"MTN transaction {transaction_id} changed or conflicted during pagination"
            )
        self._metadata["duplicate_rows"] += 1

    @staticmethod
    def _resolve_expected_type(context: ExtractionContext) -> str:
        candidates = [
            context.source.provider_options.get("account_type"),
            context.source.account_label,
        ]
        for candidate in candidates:
            normalized = MtnPhpProvider._normalize_text(candidate)
            if not normalized:
                continue
            if normalized in {"1", "prepaid", "topup prepaid"} or "مسبق" in normalized or "وحدات" in normalized:
                return _TYPE_PREPAID
            if normalized in {"2", "postpaid", "topup postpaid"} or "لاحق" in normalized or "فواتير" in normalized:
                return _TYPE_POSTPAID
        raise ProviderDataError(
            "MTN account type is ambiguous. Set credential account_type to PREPAID/POSTPAID "
            "or keep the account label/source name as وحدات ام تي ان or فواتير ام تي ان."
        )

    @staticmethod
    def _canonical_type(value: Any) -> str:
        text = re.sub(r"\s+", " ", str(value or "").strip())
        result = _TYPE_TEXT_TO_CODE.get(text)
        if result is not None:
            return result
        upper = text.upper()
        result = _TYPE_TEXT_TO_CODE.get(upper)
        return result or upper

    @staticmethod
    def _canonical_status(value: Any) -> str:
        text = re.sub(r"\s+", " ", str(value or "").strip())
        normalized = text.lower()
        result = _STATUS_TO_CANONICAL.get(normalized) or _STATUS_TO_CANONICAL.get(text)
        if result is None:
            raise ProviderDataError(f"Unsupported MTN transaction status: {text}")
        return result

    @staticmethod
    def _parse_provider_time(value: Any, tzinfo: Any) -> datetime:
        text = str(value or "").strip()
        if not text:
            raise ProviderDataError("MTN transaction row has no insertDate")
        if text.endswith("Z"):
            text = text[:-1] + "+00:00"
        parsed: datetime | None = None
        try:
            parsed = datetime.fromisoformat(text)
        except ValueError:
            for fmt in (
                "%d/%m/%Y %H:%M:%S",
                "%Y-%m-%d %H:%M:%S.%f",
                "%Y-%m-%d %H:%M:%S",
            ):
                try:
                    parsed = datetime.strptime(text, fmt)
                    break
                except ValueError:
                    continue
        if parsed is None:
            raise ProviderDataError(f"Invalid MTN insertDate: {text}")
        if parsed.tzinfo is None or parsed.utcoffset() is None:
            parsed = parsed.replace(tzinfo=tzinfo)
        return parsed

    @staticmethod
    def _csrf_token(html: str) -> str:
        soup = BeautifulSoup(html or "", "html.parser")
        node = soup.find("input", {"name": "_csrf"})
        return str(node.get("value") or "").strip() if node else ""

    @staticmethod
    def _ensure_authenticated_html(response: requests.Response, label: str) -> None:
        url = str(getattr(response, "url", "") or "")
        text = str(getattr(response, "text", "") or "")
        low = text.lower()
        if "/login" in url.lower() or ('name="username"' in low and 'name="password"' in low):
            raise ProviderDataError(f"{label} returned the login page")

    @staticmethod
    def _require_http_ok(response: requests.Response, label: str, allow_redirect: bool) -> None:
        status = int(response.status_code)
        if allow_redirect:
            ok = 200 <= status < 400
        else:
            ok = 200 <= status < 300
        if not ok:
            body = str(getattr(response, "text", "") or "")[:500]
            raise ProviderDataError(f"{label} returned HTTP {status}: {body}")

    @staticmethod
    def _required(values: Mapping[str, Any], keys: tuple[str, ...], label: str) -> str:
        for key in keys:
            value = str(values.get(key) or "").strip()
            if value:
                return value
        raise ProviderDataError(f"MTN {label} is required")

    @staticmethod
    def _positive_int(value: Any, label: str, maximum: int) -> int:
        try:
            number = int(value)
        except (TypeError, ValueError) as exc:
            raise ProviderDataError(f"MTN {label} must be an integer") from exc
        if number < 1 or number > maximum:
            raise ProviderDataError(f"MTN {label} must be between 1 and {maximum}")
        return number

    @staticmethod
    def _non_negative_int(value: Any, label: str) -> int:
        try:
            number = int(value)
        except (TypeError, ValueError) as exc:
            raise ProviderDataError(f"{label} must be an integer") from exc
        if number < 0:
            raise ProviderDataError(f"{label} must be non-negative")
        return number

    @staticmethod
    def _boolean(value: Any, label: str) -> bool:
        if isinstance(value, bool):
            return value
        text = str(value).strip().lower()
        if text in ("1", "true", "yes", "on"):
            return True
        if text in ("0", "false", "no", "off", ""):
            return False
        raise ProviderDataError(f"MTN {label} must be boolean")

    @staticmethod
    def _stable_identifier(primary: Any, secondary: Any) -> str:
        value = str(primary or "").strip() or str(secondary or "").strip()
        if not value:
            raise ProviderDataError("MTN transaction row has neither transactionId nor rowId")
        if len(value) > 256:
            raise ProviderDataError("MTN transaction identifier is too long")
        return value

    @staticmethod
    def _required_decimal_text(value: Any, label: str) -> str:
        text = str(value or "").strip().replace(",", "")
        if not re.fullmatch(r"[-+]?\d+(?:\.\d+)?", text):
            raise ProviderDataError(f"MTN {label} is not a decimal value: {value}")
        return text

    @staticmethod
    def _optional_decimal_text(value: Any) -> str | None:
        text = str(value or "").strip()
        if text in ("", "-"):
            return None
        return MtnPhpProvider._required_decimal_text(text, "balance")

    @staticmethod
    def _optional_text(value: Any) -> str | None:
        text = str(value or "").strip()
        return text or None

    @staticmethod
    def _description(transaction_type: str, status: str) -> str:
        return f"MTN {transaction_type} {status}"

    @staticmethod
    def _normalize_text(value: Any) -> str:
        text = str(value or "").strip().lower()
        text = text.replace("أ", "ا").replace("إ", "ا").replace("آ", "ا").replace("ة", "ه")
        return re.sub(r"\s+", " ", text)

    @staticmethod
    def _build_session() -> requests.Session:
        session = requests.Session()
        retry = Retry(
            total=3,
            connect=3,
            read=3,
            status=3,
            backoff_factor=0.5,
            status_forcelist=(429, 500, 502, 503, 504),
            allowed_methods=frozenset({"GET"}),
            raise_on_status=False,
        )
        session.mount("https://", HTTPAdapter(max_retries=retry))
        return session


def build_provider() -> SourceActivityProvider:
    return MtnPhpProvider()
