from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def test_outbox_dry_run_has_explicit_non_sent_state():
    text = (ROOT / "scripts" / "process_outbox.py").read_text()
    service = (ROOT / "app" / "integration_outbox_service.py").read_text()
    assert "mark_outbox_dry_run" in text
    assert 'status="dry_run"' in service
    assert "OUTBOX_DRY_RUN=true; ação não executada" in service


def test_jasmin_quotation_idempotency_is_deterministic():
    text = (ROOT / "app" / "jasmin_service.py").read_text()
    assert 'idempotency_key=f"jasmin:quotation:{opportunity_id}"' in text
    assert 'idempotency_key=f"jasmin:quotation:{opportunity_id}:{uuid.uuid4()}"' not in text


def test_admin_token_setting_is_wired():
    config = (ROOT / "app" / "config.py").read_text()
    dashboard = (ROOT / "app" / "admin_dashboard.py").read_text()
    assert "clientflow_admin_token" in config
    assert "require_admin_access" in dashboard
    assert "X-ClientFlow-Admin-Token" in dashboard


def test_outbox_operational_states_are_visible():
    service = (ROOT / "app" / "integration_outbox_service.py").read_text()
    dashboard = (ROOT / "app" / "admin_dashboard.py").read_text()
    for state in ["blocked", "dry_run", "cancelled", "processing"]:
        assert state in service
        assert state in dashboard


def test_internal_api_uses_admin_token_when_configured():
    text = (ROOT / "app" / "api" / "internal.py").read_text()
    assert "require_internal_access" in text
    assert "X-ClientFlow-Admin-Token" in text
    assert "dependencies=[Depends(require_internal_access)]" in text
