"""In-memory job queue for OCR, extraction and merge (/ocr/jobs/*).

Every job is one unit of work: the OCR of one page, the extraction of one page, or the merge of one
company. The frontend sends all its requests at once; each becomes a job at the end of a queue and the
client polls GET /ocr/jobs/{job_id} (or lists GET /ocr/jobs) instead of holding an HTTP request open.

Order and limits:
- Each Gemini step (ocr, extract, merge) has its own queue and its own workers, because each step has its
  own API key and quota. A queue starts its jobs one by one in the order they were sent; at most
  config.JOB_WORKERS[stage] of them run at once, and every Gemini call still waits for that step's shared
  rate limiter (rate_limiter.py), so the requests per minute never exceed RATE_LIMIT_RPM whatever is queued.
- A job waits for the earlier jobs on the same target, so they run in the order sent: a page's extraction
  waits for that page's OCR, and a merge waits for every earlier job of its company (and later page jobs of
  that company wait for the merge).
- Sending the same work again while it is still queued (not started) returns the queued job.

Jobs live in this process only: the app runs as a single process (see Procfile), and jobs do not survive a
restart. Finished jobs are dropped after config.JOB_TTL_SEC.
"""

import itertools
import logging
import threading
import time
import uuid
from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Optional

from fastapi import HTTPException

from . import config

logger = logging.getLogger("ocr.jobs")

STAGES = ("ocr", "extract", "merge")
ACTIVE = ("queued", "running")


class JobCancelled(Exception):
    """Raised by a job's function when it stopped because the job was cancelled."""


class JobContext:
    """Given to a job function that takes ``job=``: report progress, check for a cancel request."""

    def __init__(self, job_id: str):
        self.job_id = job_id
        self._cancel = threading.Event()

    def progress(self, data: Dict[str, Any]) -> None:
        with _cond:
            job = _jobs.get(self.job_id)
            if job is not None:
                job.update(progress=data, updated_at=_now_iso())
                job["_events"].append(data)          # kept for clients that poll (events_since); trimmed below
                if len(job["_events"]) > MAX_KEPT_EVENTS:
                    del job["_events"][0]
                    job["_events_dropped"] += 1
        _emit(self.job_id, {"event": "progress", "progress": data})

    def cancelled(self) -> bool:
        return self._cancel.is_set()


# Progress events a job keeps for polling clients (GET /jobs/{job_id}/events); a merge reports a few per Fintra AI call.
MAX_KEPT_EVENTS = 5000

_jobs: Dict[str, Dict[str, Any]] = {}
_subscribers: Dict[str, List[Callable[[Dict[str, Any]], None]]] = {}
_cond = threading.Condition()
_seq = itertools.count(1)
_workers_started = False


def subscribe(job_id: str, callback: Callable[[Dict[str, Any]], None]) -> None:
    """callback({"event": "progress", "progress": {...}}) on every progress report of the job and callback({"event": "end"}) once
    when it finishes or is cancelled. It runs in the thread that reports (a worker): hand the data over, do not block. Read the
    job with get() after subscribing, a job that ended before is not announced again."""
    with _cond:
        _subscribers.setdefault(job_id, []).append(callback)


def unsubscribe(job_id: str, callback: Callable[[Dict[str, Any]], None]) -> None:
    with _cond:
        subs = _subscribers.get(job_id)
        if subs and callback in subs:
            subs.remove(callback)
        if not subs:
            _subscribers.pop(job_id, None)


def _emit(job_id: str, payload: Dict[str, Any]) -> None:
    with _cond:
        subs = list(_subscribers.get(job_id, ()))
    for cb in subs:
        try:
            cb(payload)
        except Exception:
            logger.exception("Job %s subscriber failed", job_id)


def _now_iso() -> str:
    return datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")


def _purge_expired() -> None:
    cutoff = time.monotonic() - config.JOB_TTL_SEC
    with _cond:
        for job_id in [j for j, job in _jobs.items() if job["_finished"] and job["_finished"] < cutoff]:
            del _jobs[job_id]


def _set(job_id: str, **fields) -> None:
    with _cond:
        if job_id in _jobs:
            _jobs[job_id].update(fields, updated_at=_now_iso())


def _conflicts(a: Dict[str, Any], b: Dict[str, Any]) -> bool:
    """True when two jobs touch the same data, so the later one must wait for the earlier one."""
    if not a["instrument_code"] or a["instrument_code"] != b["instrument_code"]:
        return False
    if a["kind"] == "merge" or b["kind"] == "merge":
        return True
    return (a["fiscal_year"], a["page_number"]) == (b["fiscal_year"], b["page_number"])


def _blocked(job: Dict[str, Any]) -> bool:
    """An earlier job on the same target is still queued or running (call with _cond held)."""
    return any(other["_seq"] < job["_seq"] and other["status"] in ACTIVE and _conflicts(other, job)
               for other in _jobs.values())


def _next_ready(stage: str) -> Optional[Dict[str, Any]]:
    """The oldest queued job of a stage that is not waiting for an earlier job (call with _cond held)."""
    for job in sorted((j for j in _jobs.values() if j["stage"] == stage and j["status"] == "queued"),
                      key=lambda j: j["_seq"]):
        if not _blocked(job):
            return job
    return None


def _worker(stage: str) -> None:
    while True:
        with _cond:
            job = _next_ready(stage)
            while job is None:
                _cond.wait()
                job = _next_ready(stage)
            job.update(status="running", started_at=_now_iso(), updated_at=_now_iso())
        _run(job)
        with _cond:
            _cond.notify_all()


def _run(job: Dict[str, Any]) -> None:
    job_id = job["job_id"]
    try:
        kwargs = {"job": job["_ctx"]} if job["_takes_job"] else {}
        result = job["_fn"](*job["_args"], **kwargs)
        _finish(job_id, "done", result=result)
    except JobCancelled:
        _finish(job_id, "cancelled", error="Job cancelled")
    except HTTPException as exc:
        _finish(job_id, "error", error=str(exc.detail), status_code=exc.status_code)
    except Exception as exc:
        logger.exception("Job %s (%s) failed", job_id, job["kind"])
        _finish(job_id, "error", error=f"Job failed ({type(exc).__name__})", status_code=500)


def _finish(job_id: str, status: str, **fields) -> None:
    with _cond:
        job = _jobs.get(job_id)
        if job is None:
            return
        job.update(fields, status=status, finished_at=_now_iso(), updated_at=_now_iso(), _finished=time.monotonic())
        # the arguments can be large (uploads): drop them once the job is over
        job.update(_fn=None, _args=(), _on_cancel=None)
        _cond.notify_all()
    _emit(job_id, {"event": "end"})


def _start_workers() -> None:
    global _workers_started
    with _cond:
        if _workers_started:
            return
        _workers_started = True
    for stage in STAGES:
        for n in range(config.JOB_WORKERS[stage]):
            threading.Thread(target=_worker, args=(stage,), daemon=True, name=f"job-{stage}-{n + 1}").start()


def submit(kind: str, stage: str, fn: Callable, *args, instrument_code: Optional[str] = None,
           fiscal_year: Optional[str] = None, page_number: Optional[int] = None, takes_job: bool = False,
           on_cancel: Optional[Callable[[], None]] = None, on_queued: Optional[Callable[[str], None]] = None) -> Dict[str, Any]:
    """Queues fn(*args) (fn(*args, job=JobContext) when takes_job) and returns the job's public view.

    The same kind of work on the same target that is still queued is not queued twice: its job is returned
    with "deduplicated": true and on_queued is not called. on_queued(job_id) runs for a new job (under the queue
    lock, before a worker can pick it up); on_cancel() runs when the job is cancelled before it started.
    Raises 429 when config.JOB_QUEUE_MAX jobs are already waiting."""
    _purge_expired()
    _start_workers()
    code = instrument_code.upper().strip() if instrument_code else None
    with _cond:
        for job in _jobs.values():
            if (job["status"] == "queued" and job["kind"] == kind and job["instrument_code"] == code
                    and job["fiscal_year"] == fiscal_year and job["page_number"] == page_number):
                return {**_public(job), "deduplicated": True}
        if sum(1 for j in _jobs.values() if j["status"] == "queued") >= config.JOB_QUEUE_MAX:
            raise HTTPException(status_code=429, detail=f"Too many queued jobs (limit {config.JOB_QUEUE_MAX}); try again later")
        job_id = uuid.uuid4().hex
        now = _now_iso()
        job = {
            "job_id": job_id, "kind": kind, "stage": stage, "status": "queued",
            "instrument_code": code, "fiscal_year": fiscal_year, "page_number": page_number,
            "progress": None, "result": None, "error": None, "status_code": None, "cancel_requested": False,
            "created_at": now, "started_at": None, "finished_at": None, "updated_at": now,
            "_seq": next(_seq), "_finished": None, "_fn": fn, "_args": args, "_takes_job": takes_job,
            "_ctx": JobContext(job_id), "_on_cancel": on_cancel, "_events": [], "_events_dropped": 0,
        }
        _jobs[job_id] = job
        if on_queued is not None:
            on_queued(job_id)
        _cond.notify_all()
        return _public(job)


def cancel(job_id: str) -> Optional[Dict[str, Any]]:
    """Cancels a job: a queued one at once; a running one that can stop (a merge) at its next Gemini batch.
    None when the job is unknown; raises 409 when it already finished or cannot be stopped while running."""
    on_cancel = None
    ended = False
    with _cond:
        job = _jobs.get(job_id)
        if job is None:
            return None
        if job["status"] == "queued":
            on_cancel = job["_on_cancel"]
            job.update(status="cancelled", error="Job cancelled", finished_at=_now_iso(), updated_at=_now_iso(),
                       _finished=time.monotonic(), _fn=None, _args=(), _on_cancel=None)
            _cond.notify_all()
            ended = True
        elif job["status"] == "running" and job["_takes_job"]:
            job["_ctx"]._cancel.set()
            job.update(cancel_requested=True, updated_at=_now_iso())
        elif job["status"] == "running":
            raise HTTPException(status_code=409, detail="This job is already running and cannot be stopped (one Fintra AI call)")
        else:
            raise HTTPException(status_code=409, detail=f"Job already {job['status']}")
    if on_cancel is not None:
        try:
            on_cancel()
        except Exception:
            logger.exception("on_cancel of job %s failed", job_id)
    if ended:
        _emit(job_id, {"event": "end"})
    return get(job_id)


def _public(job: Dict[str, Any]) -> Dict[str, Any]:
    """The client view of a job (call with _cond held): adds the queue position and whether it waits for another job."""
    out = {k: v for k, v in job.items() if not k.startswith("_")}
    if job["status"] == "queued":
        out["position"] = 1 + sum(1 for j in _jobs.values()
                                  if j["stage"] == job["stage"] and j["status"] == "queued" and j["_seq"] < job["_seq"])
        out["waiting_for_earlier_job"] = _blocked(job)
    else:
        out["position"] = None
        out["waiting_for_earlier_job"] = False
    return out


def get(job_id: str) -> Optional[Dict[str, Any]]:
    _purge_expired()
    with _cond:
        job = _jobs.get(job_id)
        return _public(job) if job is not None else None


def events_since(job_id: str, after: int = 0) -> Optional[Dict[str, Any]]:
    """The job (public view, with its result) and the progress events it reported after the first `after` ones:
    {"job": {...}, "events": [...], "total": events reported so far, "next": the count to pass next time}. None when the job is unknown or expired.
    Events are numbered from the job's first report; when more than MAX_KEPT_EVENTS were reported the oldest are gone."""
    _purge_expired()
    with _cond:
        job = _jobs.get(job_id)
        if job is None:
            return None
        dropped = job["_events_dropped"]
        total = dropped + len(job["_events"])
        start = max(int(after or 0), dropped)
        return {"job": _public(job), "events": list(job["_events"][start - dropped:]), "total": total,
                "next": max(total, int(after or 0))}


def list_jobs(instrument_code: Optional[str] = None, status: Optional[str] = None,
              kind: Optional[str] = None) -> List[Dict[str, Any]]:
    """Jobs in the order they were sent, without their results (fetch one job for its result)."""
    _purge_expired()
    code = instrument_code.upper().strip() if instrument_code else None
    with _cond:
        jobs = sorted(_jobs.values(), key=lambda j: j["_seq"])
        return [{k: v for k, v in _public(j).items() if k != "result"} for j in jobs
                if (code is None or j["instrument_code"] == code) and (status is None or j["status"] == status)
                and (kind is None or j["kind"] == kind)]


def active_by_target() -> Dict[Any, Dict[str, int]]:
    """{(instrument_code, fiscal_year): {queued, running}} for the queued and running jobs; fiscal_year is None for a
    merge job (it belongs to the company, not to a report)."""
    out: Dict[Any, Dict[str, int]] = {}
    with _cond:
        for j in _jobs.values():
            if j["status"] in ACTIVE and j["instrument_code"]:
                out.setdefault((j["instrument_code"], j["fiscal_year"]), {"queued": 0, "running": 0})[j["status"]] += 1
    return out


def counts() -> Dict[str, Dict[str, int]]:
    """{stage: {queued, running}}: how much work each queue holds."""
    with _cond:
        out = {s: {"queued": 0, "running": 0} for s in STAGES}
        for j in _jobs.values():
            if j["status"] in ACTIVE:
                out[j["stage"]][j["status"]] += 1
        return out
