from __future__ import annotations

import json
import logging
import traceback
import uuid
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Mapping

from .exceptions import (
    InputValidationError,
    ProviderDataError,
    ProviderNotImplementedError,
)
from .models import ExtractionContext, ProviderRecord, SourceRequest
from .registry import build_provider
from .utils import (
    atomic_write_json,
    canonical_json,
    decimal_string,
    ensure_within,
    parse_aware_datetime,
    safe_job_id,
    safe_key,
    sanitize,
    redact_text,
    sha256_file,
    sha256_text,
    utc_iso,
)
from .writer import JsonlGzipPartWriter

LOGGER = logging.getLogger("source_activity")
SCHEMA_VERSION = "2.0"


def _optional_int(value: Any, label: str) -> int | None:
    if value in (None, ""):
        return None
    try:
        result = int(value)
    except (TypeError, ValueError) as exc:
        raise InputValidationError(f"{label} must be an integer") from exc
    if result < 0:
        raise InputValidationError(f"{label} must be non-negative")
    return result


def _source_request(item: Mapping[str, Any]) -> SourceRequest:
    source = safe_key(item.get("source"), "source")
    service_source_id = _optional_int(item.get("service_source_id"), "service_source_id")
    credential_id = _optional_int(item.get("credential_id"), "credential_id")
    account_label = str(item.get("account_label") or "").strip() or None
    credentials = item.get("credentials") or {}
    provider_options = item.get("provider_options") or {}
    if not isinstance(credentials, Mapping):
        raise InputValidationError("credentials must be an object")
    if not isinstance(provider_options, Mapping):
        raise InputValidationError("provider_options must be an object")

    supplied_key = str(item.get("source_account_key") or "").strip()
    if supplied_key:
        if len(supplied_key) != 64 or any(ch not in "0123456789abcdef" for ch in supplied_key.lower()):
            raise InputValidationError("source_account_key must be a lowercase SHA-256 hex value")
        source_account_key = supplied_key.lower()
    else:
        identity = {
            "source": source,
            "service_source_id": service_source_id,
            "credential_id": credential_id,
            "account_label": account_label,
        }
        source_account_key = sha256_text(canonical_json(identity))

    return SourceRequest(
        source=source,
        source_account_key=source_account_key,
        service_source_id=service_source_id,
        credential_id=credential_id,
        account_label=account_label,
        credentials=dict(credentials),
        provider_options=dict(provider_options),
    )


def _normalize_record(record: ProviderRecord, context: ExtractionContext) -> dict[str, Any]:
    if not isinstance(record, ProviderRecord):
        raise ProviderDataError("provider yielded an object that is not ProviderRecord")
    provider_record_key = str(record.provider_record_key or "").strip()
    if not provider_record_key:
        raise ProviderDataError("provider_record_key is required and must come from the provider adapter")
    if len(provider_record_key) > 512:
        raise ProviderDataError("provider_record_key is longer than 512 characters")
    if record.occurred_at.tzinfo is None or record.occurred_at.utcoffset() is None:
        raise ProviderDataError("occurred_at must include timezone")
    occurred_at = record.occurred_at.astimezone(timezone.utc)
    if not (context.period_from.astimezone(timezone.utc) <= occurred_at < context.period_to.astimezone(timezone.utc)):
        raise ProviderDataError("provider yielded a record outside requested [from,to) period")
    record_type = str(record.record_type or "").strip()
    provider_status = str(record.provider_status or "").strip()
    if not record_type or not provider_status:
        raise ProviderDataError("record_type and provider_status are required")

    fingerprint = sha256_text(
        context.source.source_account_key + "\n" + provider_record_key
    )
    raw_payload = dict(record.raw_payload or {})

    return {
        "schema_version": SCHEMA_VERSION,
        "source": context.source.source,
        "source_account_key": context.source.source_account_key,
        "service_source_id": context.source.service_source_id,
        "credential_id": context.source.credential_id,
        "provider_record_key": provider_record_key,
        "fingerprint": fingerprint,
        "provider_transaction_id": str(record.provider_transaction_id or "").strip() or None,
        "provider_reference": str(record.provider_reference or "").strip() or None,
        "occurred_at": utc_iso(occurred_at),
        "record_type": record_type,
        "provider_status": provider_status,
        "subscriber_identifier": str(record.subscriber_identifier or "").strip() or None,
        "amount": decimal_string(record.amount, "amount"),
        "cost_amount": decimal_string(record.cost_amount, "cost_amount"),
        "commission_amount": decimal_string(record.commission_amount, "commission_amount"),
        "currency": str(record.currency or "").strip() or None,
        "balance_before": decimal_string(record.balance_before, "balance_before"),
        "balance_after": decimal_string(record.balance_after, "balance_after"),
        "description": str(record.description or "").strip() or None,
        "raw_payload": raw_payload,
    }


def _extract_source(
    source_item: Mapping[str, Any],
    source_request: SourceRequest,
    *,
    job_id: str,
    audit_run_id: str | None,
    period_from: datetime,
    period_to: datetime,
    attempt_root: Path,
    checkpoints_root: Path,
    storage_root: Path,
    part_records: int,
    gzip_compression_level: int,
) -> dict[str, Any]:
    source_dir = ensure_within(
        attempt_root,
        attempt_root / "sources" / source_request.source / source_request.source_account_key,
    )
    source_dir.mkdir(parents=True, exist_ok=True)
    checkpoint_path = ensure_within(
        checkpoints_root,
        checkpoints_root / f"{source_request.source_account_key}.json",
    )
    atomic_write_json(source_dir / "input.sanitized.json", sanitize(dict(source_item)))

    source_result: dict[str, Any] = {
        "source": source_request.source,
        "source_account_key": source_request.source_account_key,
        "service_source_id": source_request.service_source_id,
        "credential_id": source_request.credential_id,
        "account_label": source_request.account_label,
        "status": "pending",
        "complete": False,
        "record_count": 0,
        "parts": [],
        "metadata": {},
        "error_code": None,
        "error_message": None,
    }

    writer = None
    try:
        provider = build_provider(source_request.source)
        context = ExtractionContext(
            job_id=job_id,
            audit_run_id=audit_run_id,
            period_from=period_from,
            period_to=period_to,
            source=source_request,
            source_directory=source_dir,
            checkpoint_path=checkpoint_path,
        )
        writer = JsonlGzipPartWriter(
            source_dir / "records",
            part_records,
            compression_level=gzip_compression_level,
        )
        for provider_record in provider.iter_records(context):
            writer.write(_normalize_record(provider_record, context))
        written = writer.finish()
        for part in written["parts"]:
            part_path = Path(part.pop("path"))
            part["relative_path"] = str(part_path.relative_to(storage_root))
        source_result.update({
            "status": "completed",
            "complete": True,
            "record_count": written["record_count"],
            "records_sequence_sha256": written["records_sequence_sha256"],
            "parts": written["parts"],
            "metadata": dict(provider.result_metadata() or {}),
        })
    except ProviderNotImplementedError as exc:
        if writer is not None:
            writer.abort()
        source_result.update({
            "status": "not_implemented",
            "error_code": "provider_not_implemented",
            "error_message": str(exc),
        })
    except Exception as exc:
        if writer is not None:
            writer.abort()
        secret_values = [
            str(value)
            for value in source_request.credentials.values()
            if isinstance(value, (str, int, float)) and str(value)
        ]
        safe_message = redact_text(str(exc), secret_values)[:2000]
        safe_traceback = redact_text(traceback.format_exc(), secret_values)
        LOGGER.error(
            "source extraction failed: %s (%s)",
            source_request.source,
            exc.__class__.__name__,
        )
        source_result.update({
            "status": "failed",
            "error_code": exc.__class__.__name__,
            "error_message": safe_message,
        })
        atomic_write_json(source_dir / "error.json", {
            "type": exc.__class__.__name__,
            "message": safe_message,
            "traceback": safe_traceback,
        })

    atomic_write_json(source_dir / "result.json", source_result)
    return source_result


def run_extraction(payload: Mapping[str, Any]) -> dict[str, Any]:
    if not isinstance(payload, Mapping):
        raise InputValidationError("input must be a JSON object")

    job_id = safe_job_id(payload.get("job_id"))
    audit_run_id = str(payload.get("audit_run_id") or "").strip() or None
    period_from = parse_aware_datetime(payload.get("from"), "from")
    period_to = parse_aware_datetime(payload.get("to"), "to")
    if period_from >= period_to:
        raise InputValidationError("from must be earlier than to")

    storage_root_raw = str(payload.get("storage_root") or "").strip()
    if not storage_root_raw:
        raise InputValidationError("storage_root is required")
    storage_root = Path(storage_root_raw).expanduser().resolve()
    storage_root.mkdir(parents=True, exist_ok=True)

    try:
        part_records = int(payload.get("part_records") or 100000)
    except (TypeError, ValueError) as exc:
        raise InputValidationError("part_records must be an integer") from exc
    if not (1 <= part_records <= 1_000_000):
        raise InputValidationError("part_records must be between 1 and 1000000")

    try:
        max_workers = int(payload.get("max_workers") or 4)
    except (TypeError, ValueError) as exc:
        raise InputValidationError("max_workers must be an integer") from exc
    if not (1 <= max_workers <= 32):
        raise InputValidationError("max_workers must be between 1 and 32")

    try:
        gzip_compression_level = int(payload.get("gzip_compression_level") or 1)
    except (TypeError, ValueError) as exc:
        raise InputValidationError("gzip_compression_level must be an integer") from exc
    if not (0 <= gzip_compression_level <= 9):
        raise InputValidationError("gzip_compression_level must be between 0 and 9")

    sources_raw = payload.get("sources")
    if not isinstance(sources_raw, list) or not sources_raw:
        raise InputValidationError("sources must be a non-empty array")
    source_inputs: list[tuple[Mapping[str, Any], SourceRequest]] = []
    for source_item in sources_raw:
        if not isinstance(source_item, Mapping):
            raise InputValidationError("each source item must be an object")
        source_inputs.append((source_item, _source_request(source_item)))

    attempt_id = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%S") + "-" + uuid.uuid4().hex[:12]
    run_root = ensure_within(storage_root, storage_root / job_id)
    attempt_root = ensure_within(run_root, run_root / "attempts" / attempt_id)
    checkpoints_root = ensure_within(run_root, run_root / "checkpoints")
    attempt_root.mkdir(parents=True, exist_ok=False)
    checkpoints_root.mkdir(parents=True, exist_ok=True)

    atomic_write_json(attempt_root / "request.sanitized.json", sanitize(dict(payload)))

    worker_count = min(max_workers, len(source_inputs))

    def execute_source(
        source_input: tuple[Mapping[str, Any], SourceRequest],
    ) -> dict[str, Any]:
        source_item, source_request = source_input
        return _extract_source(
            source_item,
            source_request,
            job_id=job_id,
            audit_run_id=audit_run_id,
            period_from=period_from,
            period_to=period_to,
            attempt_root=attempt_root,
            checkpoints_root=checkpoints_root,
            storage_root=storage_root,
            part_records=part_records,
            gzip_compression_level=gzip_compression_level,
        )

    if worker_count == 1:
        manifest_sources = [execute_source(source_input) for source_input in source_inputs]
    else:
        with ThreadPoolExecutor(
            max_workers=worker_count,
            thread_name_prefix="source-activity",
        ) as executor:
            manifest_sources = list(executor.map(execute_source, source_inputs))

    total_records = sum(int(item["record_count"]) for item in manifest_sources)

    all_complete = all(item["status"] == "completed" for item in manifest_sources)
    any_completed = any(item["status"] == "completed" for item in manifest_sources)
    overall_status = "completed" if all_complete else ("completed_with_notes" if any_completed or manifest_sources else "failed")

    manifest = {
        "schema_version": SCHEMA_VERSION,
        "job_id": job_id,
        "audit_run_id": audit_run_id,
        "attempt_id": attempt_id,
        "period": {
            "from": utc_iso(period_from),
            "to": utc_iso(period_to),
            "interval": "[from,to)",
        },
        "status": overall_status,
        "generated_at": utc_iso(datetime.now(timezone.utc)),
        "record_count": total_records,
        "execution": {
            "max_workers": max_workers,
            "workers_used": worker_count,
            "gzip_compression_level": gzip_compression_level,
        },
        "sources": manifest_sources,
    }
    manifest_path = attempt_root / "manifest.json"
    atomic_write_json(manifest_path, manifest)
    manifest_sha256 = sha256_file(manifest_path)
    pointer = {
        "manifest_path": str(manifest_path),
        "manifest_sha256": manifest_sha256,
        "attempt_id": attempt_id,
    }
    atomic_write_json(run_root / "latest_manifest.json", pointer)

    return {
        "ok": True,
        "job_id": job_id,
        "status": overall_status,
        "manifest_path": str(manifest_path),
        "manifest_sha256": manifest_sha256,
        "record_count": total_records,
    }
