"""Teste US-002 (PRD 3.1): CLI tools/account.py (create/list/activate/deactivate)."""

from __future__ import annotations

import os
import tempfile

import pytest


@pytest.fixture()
def env(monkeypatch):
    tmp = tempfile.mkdtemp()
    monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "test_tools_account.db"))
    from app.config import get_settings
    get_settings.cache_clear()
    yield
    get_settings.cache_clear()


def _run(argv):
    from tools.account import main
    return main(argv)


def test_create_afiseaza_id(env, capsys):
    rc = _run(["create", "--name", "Service X", "--cui", "RO001", "--email", "x@test.com"])
    out = capsys.readouterr().out
    assert rc == 0
    assert "id=2" in out
    assert "activ=da" in out


def test_create_inactive_in_asteptare(env, capsys):
    rc = _run(["create", "--name", "Service X", "--cui", "RO002", "--email", "x2@test.com", "--inactive"])
    out = capsys.readouterr().out
    assert rc == 0
    assert "activ=nu" in out


def test_create_with_key_emite_cheie(env, capsys):
    rc = _run(["create", "--name", "Service X", "--cui", "RO003", "--email", "x3@test.com", "--with-key"])
    out = capsys.readouterr().out
    assert rc == 0
    assert "rfak_" in out
    # cheia exista in DB pentru contul nou
    from app.db import get_connection
    conn = get_connection()
    try:
        n = conn.execute(
            "SELECT COUNT(*) AS n FROM api_keys k JOIN accounts a ON a.id=k.account_id "
            "WHERE a.name='Service X'"
        ).fetchone()["n"]
    finally:
        conn.close()
    assert n == 1


def test_create_cui_duplicat_exit_2(env, capsys):
    assert _run(["create", "--name", "Service A", "--cui", "RO123", "--email", "a@test.com"]) == 0
    rc = _run(["create", "--name", "Service B", "--cui", "RO123", "--email", "b@test.com"])
    err = capsys.readouterr().err
    assert rc == 2
    assert "RO123" in err


def test_with_key_atomic_pe_cui_duplicat(env, capsys):
    # cont initial care ocupa CUI
    assert _run(["create", "--name", "Service A", "--cui", "RO123", "--email", "a@test.com"]) == 0
    capsys.readouterr()
    # --with-key pe CUI duplicat: rollback -> niciun cont B, nicio cheie orfana
    rc = _run(["create", "--name", "Service B", "--cui", "RO123", "--email", "b@test.com", "--with-key"])
    assert rc == 2
    from app.db import get_connection
    conn = get_connection()
    try:
        assert conn.execute("SELECT COUNT(*) AS n FROM accounts WHERE name='Service B'").fetchone()["n"] == 0
        # o singura cheie n-a fost emisa (doar contul A nu are cheie)
        assert conn.execute("SELECT COUNT(*) AS n FROM api_keys").fetchone()["n"] == 0
    finally:
        conn.close()


def test_activate_comuta_starea(env, capsys):
    _run(["create", "--name", "Service X", "--cui", "RO004", "--email", "x4@test.com", "--inactive"])
    capsys.readouterr()
    assert _run(["deactivate", "--account", "2"]) == 0
    assert _run(["activate", "--account", "2"]) == 0
    capsys.readouterr()
    rc = _run(["list"])
    out = capsys.readouterr().out
    assert rc == 0
    assert "Service X" in out
    # contul 2 apare activ
    line = [ln for ln in out.splitlines() if "Service X" in ln][0]
    assert "da" in line


def test_activate_inexistent_exit_2(env, capsys):
    rc = _run(["activate", "--account", "9999"])
    err = capsys.readouterr().err
    assert rc == 2
    assert "inexistent" in err


def test_list_afiseaza_activ(env, capsys):
    _run(["create", "--name", "Service X", "--cui", "RO005", "--email", "x5@test.com"])
    capsys.readouterr()
    rc = _run(["list"])
    out = capsys.readouterr().out
    assert rc == 0
    assert "Service X" in out
    assert "activ" in out  # antet tabel


def test_list_pending_filtreaza(env, capsys):
    _run(["create", "--name", "Activ SRL", "--cui", "RO006", "--email", "activ@test.com"])
    _run(["create", "--name", "Asteptare SRL", "--cui", "RO007", "--email", "ast@test.com", "--inactive"])
    capsys.readouterr()
    rc = _run(["list", "--pending"])
    out = capsys.readouterr().out
    assert rc == 0
    assert "Asteptare SRL" in out
    assert "Activ SRL" not in out
