"""Teste US-008 — gate worker: claim_one sare submission-urile conturilor inactive.

TDD: testele se scriu INAINTE de modificarea claim_one; la inceput pica (RED),
dupa modificare trec (GREEN).

C14: LEFT JOIN accounts + COALESCE(a.active, 1) = 1
 (a) cont legacy fara active -> COALESCE(NULL,1)=1 -> tratat ca activ
 (b) submissions.account_id IS NULL (ON DELETE SET NULL) -> LEFT JOIN lasa
     a.active NULL -> COALESCE(NULL,1)=1 -> tratat ca activ/default
"""

from __future__ import annotations

import json
import os
import tempfile

import pytest


# --- Fixture DB (pattern din test_worker_reconcile.py) ---

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


# --- Helpers ---

_CONTENT = {
    "vin": "WVWZZZ1KZAW000123", "nr_inmatriculare": "B999TST",
    "data_prestatie": "2026-06-15", "odometru_final": "123456",
    "prestatii": [{"cod_prestatie": "OE-1"}], "sistem_reparat": "null",
}


def _insert(conn, account_id=None, status="queued", content=None):
    content = content or _CONTENT
    cur = conn.execute(
        "INSERT INTO submissions (idempotency_key, status, payload_json, account_id) "
        "VALUES (?, ?, ?, ?)",
        (f"key-{os.urandom(4).hex()}", status, json.dumps(content), account_id),
    )
    return int(cur.lastrowid)


def _row_status(conn, sid):
    return conn.execute("SELECT status FROM submissions WHERE id=?", (sid,)).fetchone()["status"]


# --- Teste ---

def test_claim_sare_cont_inactiv(env):
    """Cont inactiv (active=0) -> claim_one nu ridica submission-ul; ramane queued."""
    from app.accounts import create_account
    from app.worker.__main__ import claim_one

    conn, _ = env
    acct_id = create_account(conn, "Service Inactiv", active=False)
    sid = _insert(conn, account_id=acct_id)

    result = claim_one(conn)

    assert result is None, "claim_one trebuia sa returneze None pentru cont inactiv"
    assert _row_status(conn, sid) == "queued", "submission-ul trebuia sa ramana queued"


def test_claim_ia_cont_activ(env):
    """Cont activ (active=1) -> claim_one ridica submission-ul si il marcheaza sending."""
    from app.accounts import create_account
    from app.worker.__main__ import claim_one

    conn, _ = env
    acct_id = create_account(conn, "Service Activ", active=True)
    sid = _insert(conn, account_id=acct_id)

    result = claim_one(conn)

    assert result is not None, "claim_one trebuia sa returneze submission-ul pentru cont activ"
    assert result["id"] == sid
    assert _row_status(conn, sid) == "sending"


def test_activare_deblocheaza_trimiterea(env):
    """Cont initial inactiv -> claim_one None; dupa set_active(True) -> claim_one ridica randul."""
    from app.accounts import create_account, set_active
    from app.worker.__main__ import claim_one

    conn, _ = env
    acct_id = create_account(conn, "Service Provizoriu", active=False)
    sid = _insert(conn, account_id=acct_id)

    assert claim_one(conn) is None, "inainte de activare claim_one trebuia sa returneze None"
    assert _row_status(conn, sid) == "queued"

    set_active(conn, acct_id, True)

    result = claim_one(conn)
    assert result is not None, "dupa activare claim_one trebuia sa returneze submission-ul"
    assert result["id"] == sid
    assert _row_status(conn, sid) == "sending"


def test_claim_account_null_tratat_activ(env):
    """submission.account_id IS NULL (ON DELETE SET NULL) -> LEFT JOIN lasa a.active NULL
    -> COALESCE(NULL,1)=1 -> tratat ca activ; claim_one il ridica."""
    from app.worker.__main__ import claim_one

    conn, _ = env
    sid = _insert(conn, account_id=None)

    result = claim_one(conn)

    assert result is not None, "submission cu account_id NULL trebuia sa fie ridicat (tratat ca activ)"
    assert result["id"] == sid
    assert _row_status(conn, sid) == "sending"


def test_claim_sare_cont_blocat(env):
    """5.5: cont blocked -> claim_one nu ridica submission-ul."""
    from app.accounts import create_account, set_status
    from app.worker.__main__ import claim_one

    conn, _ = env
    acct_id = create_account(conn, "Service Blocat", active=True)
    sid = _insert(conn, account_id=acct_id)
    set_status(conn, acct_id, "blocked")

    assert claim_one(conn) is None
    assert _row_status(conn, sid) == "queued"


def test_claim_sare_cont_arhivat(env):
    """5.5: cont archived -> claim_one nu ridica submission-ul."""
    from app.accounts import create_account, set_status
    from app.worker.__main__ import claim_one

    conn, _ = env
    acct_id = create_account(conn, "Service Arhivat", active=True)
    sid = _insert(conn, account_id=acct_id)
    set_status(conn, acct_id, "archived")

    assert claim_one(conn) is None
    assert _row_status(conn, sid) == "queued"


def test_deblocare_reia_trimiterea(env):
    """5.5: blocked -> set_status('active') -> claim_one ridica din nou."""
    from app.accounts import create_account, set_status
    from app.worker.__main__ import claim_one

    conn, _ = env
    acct_id = create_account(conn, "Service Revenit", active=True)
    sid = _insert(conn, account_id=acct_id)
    set_status(conn, acct_id, "blocked")
    assert claim_one(conn) is None

    set_status(conn, acct_id, "active")
    result = claim_one(conn)
    assert result is not None and result["id"] == sid
    assert _row_status(conn, sid) == "sending"


def test_claim_cont_legacy_fara_active(env):
    """Simuleaza cont legacy: LEFT JOIN nu gaseste randul in accounts (account_id nul dupa stergere cont)
    -> a.active=NULL dupa JOIN -> COALESCE(NULL,1)=1 -> tratat ca activ.

    Nota: schema curenta are active NOT NULL, deci NULL pe coloana `active` e imposibil;
    COALESCE acopera NULL-ul de pe a.active produs de LEFT JOIN fara match, nu din coloana.
    Simulam prin setarea directa a account_id la NULL (ca dupa ON DELETE SET NULL).
    """
    from app.worker.__main__ import claim_one

    conn, _ = env
    sid = _insert(conn, account_id=None)
    conn.execute("UPDATE submissions SET account_id=NULL WHERE id=?", (sid,))

    result = claim_one(conn)

    assert result is not None, "submission fara cont (account_id NULL) trebuia tratat ca activ"
    assert result["id"] == sid
