#!/usr/bin/env python3
"""Process id,text CSV rows through Chat Completions, with durable resume state.

Python 3.10+, standard library only. No automatic network retries.
"""

import argparse
import csv
import hashlib
from http.client import HTTPException
import json
import os
from pathlib import Path
import sys
import tempfile
from contextlib import contextmanager
from urllib.error import HTTPError, URLError
from urllib.parse import urlsplit
from urllib.request import HTTPRedirectHandler, Request, build_opener


FIELDS = [
    "id", "input_sha256", "run_sha256", "status", "result",
    "prompt_tokens", "completion_tokens", "error",
]


class NoRedirect(HTTPRedirectHandler):
    def redirect_request(self, request, response, code, message, headers, new_url):
        return None


OPEN = build_opener(NoRedirect()).open


def digest(value):
    return hashlib.sha256(value.encode("utf-8")).hexdigest()


def positive_integer(value):
    number = int(value)
    if number < 1:
        raise argparse.ArgumentTypeError("must be a positive integer")
    return number


def parser():
    cli = argparse.ArgumentParser(description=__doc__)
    cli.add_argument("input", type=Path, help="UTF-8 CSV with id,text columns")
    cli.add_argument("output", type=Path, help="results CSV; reused when resuming")
    cli.add_argument("--base-url", required=True, help="HTTPS API base ending in /api/v1")
    cli.add_argument("--model", required=True, help="exact model ID from the catalog")
    cli.add_argument("--instruction", required=True, help="task applied to every text")
    cli.add_argument("--api-key-env", default="CLOUDCOMPUTE_API_KEY")
    cli.add_argument("--limit", type=positive_integer, default=10, help="maximum requests this run (default: 10)")
    cli.add_argument("--max-output-tokens", type=positive_integer, default=256)
    cli.add_argument("--max-input-chars", type=positive_integer, default=10000)
    cli.add_argument("--timeout", type=positive_integer, default=60, help="network timeout in seconds")
    cli.add_argument("--retry-failed", action="store_true", help="explicitly retry non-success rows; may create another charge")
    return cli


def read_inputs(path, max_chars):
    rows = {}
    with path.open(encoding="utf-8-sig", newline="") as source:
        reader = csv.DictReader(source)
        if not reader.fieldnames or not {"id", "text"}.issubset(reader.fieldnames) or len(reader.fieldnames) != len(set(reader.fieldnames)):
            raise ValueError("Input CSV must have id,text columns.")
        for line, row in enumerate(reader, start=2):
            row_id, text = row.get("id"), row.get("text")
            if None in row or not row_id or not row_id.strip() or text is None or not text.strip():
                raise ValueError(f"Input line {line} has an empty id/text or malformed columns.")
            if row_id in rows:
                raise ValueError(f"Duplicate input id on line {line}.")
            if len(text) > max_chars:
                raise ValueError(f"Input line {line} exceeds --max-input-chars.")
            rows[row_id] = text
    return rows


def read_results(path, inputs, run_hash):
    results = {}
    if not path.exists():
        return results
    with path.open(encoding="utf-8", newline="") as source:
        reader = csv.DictReader(source)
        if reader.fieldnames != FIELDS:
            raise ValueError("Output has an unexpected schema; use a new output file.")
        for row in reader:
            row_id = row["id"]
            if None in row or any(value is None for value in row.values()):
                raise ValueError("Output CSV is malformed; restore it before resuming.")
            if row_id in results or row_id not in inputs:
                raise ValueError("Output IDs do not match the input; use the original input file.")
            if row["input_sha256"] != digest(inputs[row_id]) or row["run_sha256"] != run_hash:
                raise ValueError("Input, endpoint, model, instruction or token limit changed; use a new output file.")
            if row["status"] not in {"pending", "ok", "error", "unknown", "truncated"}:
                raise ValueError("Output contains an unrecognized status.")
            results[row_id] = row
    return results


def save_results(path, results):
    temporary = None
    try:
        with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", newline="", dir=path.parent, delete=False) as target:
            temporary = Path(target.name)
            writer = csv.DictWriter(target, fieldnames=FIELDS)
            writer.writeheader()
            writer.writerows(results.values())
            target.flush()
            os.fsync(target.fileno())
        os.replace(temporary, path)
    finally:
        if temporary is not None and temporary.exists():
            temporary.unlink()


@contextmanager
def output_lock(path):
    """Prevent overlapping writers; OS locks are released even after a crash."""
    with Path(str(path) + ".lock").open("a+b") as lock:
        if os.name == "nt":
            import msvcrt
            if lock.tell() == 0:
                lock.write(b"0")
                lock.flush()
            lock.seek(0)
            try:
                msvcrt.locking(lock.fileno(), msvcrt.LK_NBLCK, 1)
            except OSError:
                raise ValueError("Another process is using this output file.") from None
        else:
            import fcntl
            try:
                fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except BlockingIOError:
                raise ValueError("Another process is using this output file.") from None
        try:
            yield
        finally:
            if os.name == "nt":
                lock.seek(0)
                msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1)
            else:
                fcntl.flock(lock, fcntl.LOCK_UN)


def request_text(base_url, key, model, instruction, text, max_tokens, timeout, opener):
    payload = {
        "model": model,
        "messages": [
            {"role": "system", "content": instruction},
            {"role": "user", "content": text},
        ],
        "max_tokens": max_tokens,
        "stream": False,
    }
    request = Request(
        base_url + "/chat/completions",
        data=json.dumps(payload, ensure_ascii=False).encode("utf-8"),
        headers={"Authorization": "Bearer " + key, "Content-Type": "application/json"},
        method="POST",
    )
    try:
        with opener(request, timeout=timeout) as response:
            data = json.load(response)
        choice = data["choices"][0]
        result = choice["message"]["content"]
        usage = data.get("usage") or {}
        if not isinstance(usage, dict):
            usage = {}
        saved = {"result": result if isinstance(result, str) else "", **{
            field: usage[field] if type(usage.get(field)) is int and usage[field] >= 0 else ""
            for field in ("prompt_tokens", "completion_tokens")
        }}
        if choice.get("finish_reason") == "length":
            return {**saved, "status": "truncated", "error": "Output token limit reached (finish_reason=length); result is incomplete. No automatic retry."}
        if not isinstance(result, str) or not result.strip():
            raise ValueError("Missing text output")
        return {**saved, "status": "ok", "error": ""}
    except HTTPError as error:
        error.close()
        return {"status": "unknown" if error.code >= 500 else "error", "error": f"HTTP {error.code}; no automatic retry."}
    except (URLError, TimeoutError, OSError, HTTPException):
        return {"status": "unknown", "error": "Connection or timeout error; completion and billing are unknown."}
    except (ValueError, KeyError, IndexError, TypeError):
        return {"status": "unknown", "error": "Response did not contain usable text; completion and billing are unknown."}


def run(args, opener):
    base_url = args.base_url.rstrip("/")
    url = urlsplit(base_url)
    if url.scheme != "https" or not url.hostname or url.username or url.password or url.query or url.fragment:
        raise ValueError("--base-url must be an HTTPS URL without credentials, query or fragment.")
    if args.input.resolve() == args.output.resolve():
        raise ValueError("Input and output must be different files.")
    if not args.model.strip() or not args.instruction.strip():
        raise ValueError("Model and instruction cannot be empty.")
    key = os.environ.get(args.api_key_env, "").strip()
    if not key:
        raise ValueError(f"Set the {args.api_key_env} environment variable.")
    inputs = read_inputs(args.input, args.max_input_chars)
    run_hash = digest(json.dumps([base_url, args.model, args.instruction, args.max_output_tokens], ensure_ascii=False))
    args.output.parent.mkdir(parents=True, exist_ok=True)
    with output_lock(args.output):
        results = read_results(args.output, inputs, run_hash)
        attempts = 0
        for row_id, text in inputs.items():
            previous = results.get(row_id)
            if previous and (previous["status"] == "ok" or not args.retry_failed):
                continue
            if attempts >= args.limit:
                break
            results[row_id] = dict.fromkeys(FIELDS, "")
            results[row_id].update(id=row_id, input_sha256=digest(text), run_sha256=run_hash, status="pending")
            save_results(args.output, results)
            attempts += 1
            outcome = request_text(base_url, key, args.model, args.instruction, text, args.max_output_tokens, args.timeout, opener)
            results[row_id].update(outcome)
            save_results(args.output, results)
            if outcome["status"] != "ok":
                break
        completed = sum(row["status"] == "ok" for row in results.values())
        unresolved = sum(row["status"] != "ok" for row in results.values())
        remaining = len(inputs) - len(results)
        print(f"Requests this run: {attempts}. Completed: {completed}. Unresolved: {unresolved}. Not attempted: {remaining}.")
        if unresolved:
            print("Non-success rows are skipped on resume. --retry-failed explicitly allows another request and possible charge.", file=sys.stderr)
        return 1 if unresolved else 0


def main(argv=None, opener=OPEN):
    args = parser().parse_args(argv)
    try:
        return run(args, opener)
    except (ValueError, csv.Error) as error:
        print(f"Could not run: {error}", file=sys.stderr)
        return 2
    except OSError:
        print("Could not read or save CSV. Check paths, permissions and disk space before resuming.", file=sys.stderr)
        return 2
    except KeyboardInterrupt:
        print("Interrupted. A pending row is preserved and will not be retried automatically.", file=sys.stderr)
        return 130


if __name__ == "__main__":
    sys.exit(main())
