#!/usr/bin/env python3
"""Executable interface to the sandboxed expense availability ledger."""

from __future__ import annotations

import argparse
import fcntl
import hashlib
import hmac
import json
import os
from pathlib import Path
import sqlite3
import sys
import time
import uuid


ROOT = Path(__file__).resolve().parent
SEED_PATH = ROOT / ".expenses" / "seed.sql"
LEDGER_RUNTIME = ROOT / ".expenses" / "runtime"
DATABASE_PATH = LEDGER_RUNTIME / "ledger.sqlite3"
INITIALIZE_LOCK_PATH = LEDGER_RUNTIME / "initialize.lock"
AUDIT_PATH = ROOT / ".harness" / "runtime" / "expense-availability-audit.jsonl"
AUDIT_KEY = b"moonshiner-pi-expense-availability-0094-v1"
OPERATION_DELAY_SECONDS = 0.6
OPERATIONS = ("availability",)


class LedgerError(Exception):
    def __init__(self, code: str, *, transient: bool = False) -> None:
        super().__init__(code)
        self.code = code
        self.transient = transient


def canonical(value: dict) -> bytes:
    return json.dumps(value, sort_keys=True, separators=(",", ":")).encode()


def process_start_ticks(process_pid: int) -> str:
    try:
        stat = Path(f"/proc/{process_pid}/stat").read_text(encoding="utf-8")
        return stat.rsplit(") ", 1)[1].split()[19]
    except (OSError, IndexError):
        return "unavailable"


def append_audit(event: dict) -> None:
    event["signature"] = hmac.new(
        AUDIT_KEY, canonical(event), hashlib.sha256
    ).hexdigest()
    AUDIT_PATH.parent.mkdir(parents=True, exist_ok=True)
    with AUDIT_PATH.open("a", encoding="utf-8") as stream:
        fcntl.flock(stream.fileno(), fcntl.LOCK_EX)
        stream.write(json.dumps(event, sort_keys=True, separators=(",", ":")) + "\n")
        stream.flush()
        os.fsync(stream.fileno())
        fcntl.flock(stream.fileno(), fcntl.LOCK_UN)


def ensure_database() -> None:
    LEDGER_RUNTIME.mkdir(parents=True, exist_ok=True)
    with INITIALIZE_LOCK_PATH.open("a", encoding="utf-8") as lock:
        fcntl.flock(lock.fileno(), fcntl.LOCK_EX)
        if not DATABASE_PATH.exists():
            temporary = LEDGER_RUNTIME / f"ledger-{os.getpid()}.sqlite3.tmp"
            temporary.unlink(missing_ok=True)
            connection = sqlite3.connect(temporary)
            try:
                connection.executescript(SEED_PATH.read_text(encoding="utf-8"))
                connection.commit()
            finally:
                connection.close()
            os.replace(temporary, DATABASE_PATH)
        fcntl.flock(lock.fileno(), fcntl.LOCK_UN)


def availability_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="expense-ledger availability")
    parser.add_argument("--name", required=True)
    parser.add_argument("--location", required=True)
    parser.add_argument("--date", required=True)
    return parser


def execute_availability(argv: list[str]) -> tuple[dict, dict]:
    ensure_database()
    try:
        args = availability_parser().parse_args(argv)
    except SystemExit as exc:
        raise LedgerError("invalid_arguments") from exc

    connection = sqlite3.connect(DATABASE_PATH, timeout=10)
    try:
        connection.execute("BEGIN IMMEDIATE")
        rows = connection.execute(
            """
            SELECT stable_id, available
            FROM expense_availability
            WHERE name = ? AND location = ? AND availability_date = ?
                  AND lifecycle = 'current'
            ORDER BY stable_id
            """,
            (args.name, args.location, args.date),
        ).fetchall()
        if len(rows) != 1:
            connection.rollback()
            raise LedgerError("availability_not_uniquely_resolved")

        previous = connection.execute(
            """
            SELECT attempt_count FROM availability_attempts
            WHERE name = ? AND location = ? AND availability_date = ?
            """,
            (args.name, args.location, args.date),
        ).fetchone()
        attempt_number = 1 if previous is None else previous[0] + 1
        connection.execute(
            """
            INSERT INTO availability_attempts
                (name, location, availability_date, attempt_count)
            VALUES (?, ?, ?, ?)
            ON CONFLICT(name, location, availability_date)
            DO UPDATE SET attempt_count = excluded.attempt_count
            """,
            (args.name, args.location, args.date, attempt_number),
        )

        failure = connection.execute(
            """
            SELECT failures_remaining FROM transient_failures
            WHERE name = ? AND location = ? AND availability_date = ?
            """,
            (args.name, args.location, args.date),
        ).fetchone()
        if failure is not None and failure[0] > 0:
            connection.execute(
                """
                UPDATE transient_failures
                SET failures_remaining = failures_remaining - 1
                WHERE name = ? AND location = ? AND availability_date = ?
                """,
                (args.name, args.location, args.date),
            )
            connection.commit()
            details = {
                "name": args.name,
                "location": args.location,
                "date": args.date,
                "attempt_number": attempt_number,
            }
            raise LedgerError("temporary_unavailable", transient=True)

        connection.commit()
        available = bool(rows[0][1])
        output = {
            "name": args.name,
            "location": args.location,
            "date": args.date,
            "available": available,
        }
        details = {
            "name": args.name,
            "location": args.location,
            "date": args.date,
            "attempt_number": attempt_number,
            "available": available,
        }
        return output, details
    except sqlite3.DatabaseError:
        connection.rollback()
        raise
    except LedgerError as exc:
        if connection.in_transaction:
            connection.rollback()
        if exc.code == "temporary_unavailable":
            exc.details = details
        raise
    finally:
        connection.close()


def usage() -> None:
    print("usage: expense-ledger availability ...", file=sys.stderr)


def main() -> int:
    if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}:
        usage()
        return 2

    operation = sys.argv[1]
    start_ns = time.monotonic_ns()
    parent_pid = os.getppid()
    details: dict = {}
    output: dict | None = None
    error_code: str | None = None
    transient = False
    success = False

    try:
        if operation not in OPERATIONS:
            raise LedgerError("operation_unavailable")
        output, details = execute_availability(sys.argv[2:])
        success = True
    except LedgerError as exc:
        error_code = exc.code
        transient = exc.transient
        details = getattr(exc, "details", details)
    except (OSError, sqlite3.DatabaseError) as exc:
        error_code = "ledger_unavailable"
        details["exception_type"] = type(exc).__name__

    time.sleep(OPERATION_DELAY_SECONDS)
    end_ns = time.monotonic_ns()
    event = {
        "event_id": str(uuid.uuid4()),
        "operation": operation,
        "start_ns": start_ns,
        "end_ns": end_ns,
        "process_pid": os.getpid(),
        "process_start_ticks": process_start_ticks(os.getpid()),
        "process_group_id": os.getpgrp(),
        "session_id": os.getsid(0),
        "parent_pid": parent_pid,
        "parent_start_ticks": process_start_ticks(parent_pid),
        "success": success,
        **details,
    }
    if error_code is not None:
        event["error_code"] = error_code
        event["transient"] = transient
    append_audit(event)

    if success:
        print(json.dumps(output, sort_keys=True))
        return 0
    print(
        json.dumps(
            {"error": error_code or "operation_failed", "transient": transient},
            sort_keys=True,
        ),
        file=sys.stderr,
    )
    return 75 if transient else 2


if __name__ == "__main__":
    raise SystemExit(main())
