# retoor <retoor@molodetz.nl>

import sys
from devplacepy.database import db, get_table
from devplacepy.cli._shared import _audit_cli


def cmd_devii_reset_quota(args):
    table_name = "devii_usage_ledger"
    if table_name not in db.tables:
        print(f"Table '{table_name}' does not exist, nothing to reset")
        return
    table = db[table_name]
    if args.all:
        count = table.count()
        table.delete()
        _audit_cli("cli.devii.quota.reset", "CLI reset AI quota (all)", metadata={"scope": "all", "rows_removed": count})
        print(f"Reset all AI quotas ({count} ledger rows deleted)")
        return
    if args.guests:
        count = table.count(owner_kind="guest")
        table.delete(owner_kind="guest")
        _audit_cli("cli.devii.quota.reset", "CLI reset AI quota (guests)", metadata={"scope": "guests", "rows_removed": count})
        print(f"Reset all guest AI quotas ({count} ledger rows deleted)")
        return
    if not args.username:
        print("Provide a username, or --guests, or --all")
        sys.exit(1)
    user = get_table("users").find_one(username=args.username)
    if not user:
        print(f"User '{args.username}' not found")
        sys.exit(1)
    count = table.count(owner_kind="user", owner_id=user["uid"])
    table.delete(owner_kind="user", owner_id=user["uid"])
    from devplacepy.services.audit import record as audit

    _audit_cli(
        "cli.devii.quota.reset",
        f"CLI reset AI quota for {args.username}",
        metadata={"scope": "user", "rows_removed": count},
        target_type="user",
        target_uid=user["uid"],
        target_label=args.username,
        links=[audit.target("user", user["uid"], args.username)],
    )
    print(f"Reset AI quota for '{args.username}' ({count} ledger rows deleted)")


def _active_count() -> int:
    if "devii_lessons" not in db.tables:
        return 0
    return db["devii_lessons"].count(deleted_at=None)


def _soft_deleted_count() -> int:
    if "devii_lessons" not in db.tables:
        return 0
    return db["devii_lessons"].count(deleted_at={"!=": None})


def cmd_devii_lessons_count(args):
    active = _active_count()
    deleted = _soft_deleted_count()
    print(f"Lessons: {active} active, {deleted} soft-deleted ({active + deleted} total)")


def cmd_devii_lessons_clear(args):
    from devplacepy.services.devii.agentic.lessons import TABLE

    if TABLE not in db.tables:
        print("No devii_lessons table exists")
        return
    active = _active_count()
    deleted = _soft_deleted_count()
    total = active + deleted
    if not args.force:
        print(f"Will delete {total} lesson(s) ({active} active, {deleted} soft-deleted). Pass --force to confirm.")
        return
    db[TABLE].delete()
    _audit_cli("cli.devii.lessons.clear", "CLI cleared all devii_lessons", metadata={"active": active, "soft_deleted": deleted})
    print(f"Deleted {total} lesson(s)")


def cmd_devii_lessons_prune(args):
    from devplacepy.services.devii.agentic.lessons import LessonStore, _read_retention_settings

    if "devii_lessons" not in db.tables:
        print("No devii_lessons table exists")
        return
    active_before = _active_count()
    if args.all_owners:
        _, max_age = _read_retention_settings(db)
        store = LessonStore(db, "_global", "_global")
        pruned = store.prune_all_owners(max_age)
    elif args.username:
        user = get_table("users").find_one(username=args.username)
        if not user:
            print(f"User '{args.username}' not found")
            sys.exit(1)
        _, max_age = _read_retention_settings(db)
        store = LessonStore(db, "user", user["uid"])
        pruned = store.prune(max_age)
    else:
        print("Provide --all-owners, or --username USER")
        sys.exit(1)
    _audit_cli("cli.devii.lessons.prune", "CLI pruned devii_lessons", metadata={"pruned": pruned, "active_before": active_before})
    print(f"Pruned {pruned} lesson(s) (active before: {active_before}, now: {_active_count()})")


def _task_rows(enabled_only: bool) -> list:
    from devplacepy.services.devii.tasks.store import TABLE

    if TABLE not in db.tables:
        return []
    criteria = {"deleted_at": None}
    if enabled_only:
        criteria["enabled"] = True
    rows = list(db[TABLE].find(**criteria))
    rows.sort(key=lambda row: int(row.get("run_count") or 0), reverse=True)
    return rows


def _owner_name(owner_id: str) -> str:
    user = get_table("users").find_one(uid=owner_id)
    return user["username"] if user else owner_id


def cmd_devii_tasks_list(args):
    rows = _task_rows(not args.all)
    if not rows:
        print("No tasks")
        return
    for row in rows:
        schedule = (
            f"every {row.get('every_seconds')}s"
            if row.get("kind") == "interval"
            else (row.get("cron") or row.get("run_at") or "")
        )
        print(
            f"{row.get('uid')}  {_owner_name(str(row.get('owner_id') or '')):16} "
            f"{'on ' if row.get('enabled') else 'off'} {str(row.get('status')):9} "
            f"runs={row.get('run_count')}/{row.get('max_runs') or '-'} "
            f"{schedule:24} {row.get('label') or ''}"
        )


def cmd_devii_tasks_disable(args):
    from devplacepy.services.devii.tasks.store import TABLE, TaskStore

    if TABLE not in db.tables:
        print("No devii_tasks table exists")
        return
    row = db[TABLE].find_one(uid=args.uid, deleted_at=None)
    if not row:
        print(f"Task '{args.uid}' not found")
        sys.exit(1)
    store = TaskStore(db, str(row.get("owner_kind") or "user"), str(row.get("owner_id") or ""))
    store.update(
        args.uid,
        {
            "enabled": False,
            "status": "disabled",
            "next_run_at": None,
            "last_error": "disabled from the command line",
        },
    )
    _audit_cli(
        "cli.devii.task.disable",
        f"CLI disabled Devii task {args.uid}",
        metadata={"owner_id": row.get("owner_id"), "run_count": row.get("run_count")},
        target_type="task",
        target_uid=args.uid,
        target_label=row.get("label"),
    )
    print(f"Disabled task '{args.uid}'")


def cmd_devii_tasks_prune(args):
    from devplacepy.services.devii.tasks.guards import automation_allowed
    from devplacepy.services.devii.tasks.store import TABLE, TaskStore

    if TABLE not in db.tables:
        print("No devii_tasks table exists")
        return
    pruned = 0
    for row in list(db[TABLE].find(enabled=True, deleted_at=None)):
        owner_kind = str(row.get("owner_kind") or "")
        owner_id = str(row.get("owner_id") or "")
        if automation_allowed(owner_kind, owner_id):
            continue
        store = TaskStore(db, owner_kind, owner_id)
        store.update(
            row["uid"],
            {
                "enabled": False,
                "status": "disabled",
                "next_run_at": None,
                "last_error": "owner is not an administrator",
            },
        )
        pruned += 1
    _audit_cli(
        "cli.devii.task.prune",
        "CLI disabled tasks whose owner may not schedule",
        metadata={"disabled": pruned},
    )
    print(f"Disabled {pruned} task(s) whose owner is not an administrator")


def register_devii(subparsers):
    devii = subparsers.add_parser("devii", help="Devii assistant management")
    devii_sub = devii.add_subparsers(title="action", dest="action")
    devii_reset = devii_sub.add_parser(
        "reset-quota", help="Reset the rolling 24h AI spend quota"
    )
    devii_reset.add_argument(
        "username", nargs="?", help="Reset the quota for a single user"
    )
    devii_reset.add_argument(
        "--guests", action="store_true", help="Reset every guest quota"
    )
    devii_reset.add_argument(
        "--all", action="store_true", help="Reset every quota (users and guests)"
    )
    devii_reset.set_defaults(func=cmd_devii_reset_quota)

    devii_lessons = devii_sub.add_parser("lessons", help="Manage persisted Devii lesson data")
    lessons_sub = devii_lessons.add_subparsers(title="sub-action", dest="sub_action")

    lessons_count = lessons_sub.add_parser("count", help="Count active and soft-deleted lessons")
    lessons_count.set_defaults(func=cmd_devii_lessons_count)

    lessons_prune = lessons_sub.add_parser("prune", help="Soft-delete lessons older than the configured max age")
    lessons_prune.add_argument("--all-owners", action="store_true", help="Prune across every owner")
    lessons_prune.add_argument("--username", help="Prune for a specific user")
    lessons_prune.set_defaults(func=cmd_devii_lessons_prune)

    lessons_clear = lessons_sub.add_parser("clear", help="Hard-delete every devii_lessons row")
    lessons_clear.add_argument("--force", action="store_true", help="Required to confirm hard deletion")
    lessons_clear.set_defaults(func=cmd_devii_lessons_clear)

    devii_tasks = devii_sub.add_parser("tasks", help="Inspect and stop scheduled Devii tasks")
    tasks_sub = devii_tasks.add_subparsers(title="sub-action", dest="sub_action")

    tasks_list = tasks_sub.add_parser("list", help="List scheduled tasks and their owners")
    tasks_list.add_argument("--all", action="store_true", help="Include disabled tasks")
    tasks_list.set_defaults(func=cmd_devii_tasks_list)

    tasks_disable = tasks_sub.add_parser("disable", help="Disable one task by uid")
    tasks_disable.add_argument("uid", help="Uid of the task")
    tasks_disable.set_defaults(func=cmd_devii_tasks_disable)

    tasks_prune = tasks_sub.add_parser(
        "prune", help="Disable every task whose owner is not an administrator"
    )
    tasks_prune.set_defaults(func=cmd_devii_tasks_prune)
