from uuid import UUID
from starlette.testclient import TestClient

from fmatch.saas.main import create_app
from fmatch.saas.auth import get_current_account


def test_run_idempotency_returns_same_job_id():
    app = create_app()
    # Force a stable account for test
    acct = UUID("11111111-1111-1111-1111-111111111111")
    app.dependency_overrides[get_current_account] = lambda: acct

    client = TestClient(app)
    idem = "test-idem-key-123"

    payload = {
        "input": {"uri": "file://dummy.csv"},
        "intelligence": {"profile": "leads_to_accounts", "threshold": 0.75, "options": {}},
    }

    r1 = client.post("/api/v2/jobs/run", headers={"Idempotency-Key": idem}, json=payload)
    assert r1.status_code in (201, 200), r1.text
    j1 = r1.json()["job_id"]

    r2 = client.post("/api/v2/jobs/run", headers={"Idempotency-Key": idem}, json=payload)
    assert r2.status_code in (200, 201), r2.text
    j2 = r2.json()["job_id"]

    assert j1 == j2

