from __future__ import annotations

from uuid import uuid4

import pytest
from fastapi.testclient import TestClient

from app.modules.sync import repository as repo
from tests.orchestrator_dual_harness import OrchestratorDualHubHarness, make_enterprise


def test_hub_conflicts_via_hub_auth(client: TestClient):
    enterprise_id = make_enterprise(client)
    harness = OrchestratorDualHubHarness(client, enterprise_id)
    hub_a, hub_b = harness.pair_hubs()
    record_id = "hub-auth-conflict-1"
    harness.push_from(
        hub_a,
        [
            {
                "event_id": str(uuid4()),
                "seq": 1,
                "domain": "global",
                "table": "component",
                "record_id": record_id,
                "action": "upsert",
                "version": 1,
                "content_hash": "hub-h1",
                "payload": {"name": "A", "type": "g", "dry_matter": 1, "id": record_id, "version": 1, "content_hash": "hub-h1"},
                "emitted_at": "2026-07-15T12:00:00Z",
                "origin_site_id": hub_a.hub_site_id,
            }
        ],
    )
    harness.push_from(
        hub_b,
        [
            {
                "event_id": str(uuid4()),
                "seq": 1,
                "domain": "global",
                "table": "component",
                "record_id": record_id,
                "action": "upsert",
                "version": 1,
                "content_hash": "hub-h2",
                "payload": {"name": "B", "type": "g", "dry_matter": 2, "id": record_id, "version": 1, "content_hash": "hub-h2"},
                "emitted_at": "2026-07-15T12:01:00Z",
                "origin_site_id": hub_b.hub_site_id,
            }
        ],
    )
    listed = client.get("/api/v1/sync/hub/conflicts", headers=hub_a.auth_header())
    assert listed.status_code == 200
    conflicts = listed.json()
    assert len(conflicts) >= 1


def test_multiserver_list_and_resolve(client: TestClient):
    enterprise_id = make_enterprise(client)
    harness = OrchestratorDualHubHarness(client, enterprise_id)
    hub_a, hub_b = harness.pair_hubs()
    record_id = "ms-conflict-1"
    harness.push_from(
        hub_a,
        [
            {
                "event_id": str(uuid4()),
                "seq": 1,
                "domain": "global",
                "table": "component",
                "record_id": record_id,
                "action": "upsert",
                "version": 1,
                "content_hash": "ms-h1",
                "payload": {"name": "A", "type": "g", "dry_matter": 1, "id": record_id, "version": 1, "content_hash": "ms-h1"},
                "emitted_at": "2026-07-15T12:00:00Z",
                "origin_site_id": hub_a.hub_site_id,
            }
        ],
    )
    harness.push_from(
        hub_b,
        [
            {
                "event_id": str(uuid4()),
                "seq": 1,
                "domain": "global",
                "table": "component",
                "record_id": record_id,
                "action": "upsert",
                "version": 1,
                "content_hash": "ms-h2",
                "payload": {"name": "B", "type": "g", "dry_matter": 2, "id": record_id, "version": 1, "content_hash": "ms-h2"},
                "emitted_at": "2026-07-15T12:01:00Z",
                "origin_site_id": hub_b.hub_site_id,
            }
        ],
    )
    login = client.post("/api/v1/auth/login", json={"email": "admin@compton.example", "password": "Admin1234"})
    token = login.json()["access_token"]
    listed = client.get(
        f"/api/v1/sync/conflicts?enterprise_id={enterprise_id}",
        headers={"Authorization": f"Bearer {token}"},
    )
    assert listed.status_code == 200
    conflicts = listed.json()
    assert len(conflicts) >= 1
    cid = conflicts[0]["id"]
    detail = client.get(
        f"/api/v1/sync/conflicts/{cid}?enterprise_id={enterprise_id}",
        headers={"Authorization": f"Bearer {token}"},
    )
    assert detail.status_code == 200
    resolved = client.post(
        f"/api/v1/sync/conflicts/{cid}/resolve?enterprise_id={enterprise_id}",
        headers={"Authorization": f"Bearer {token}"},
        json={"resolution": "keep_orchestrator"},
    )
    assert resolved.status_code == 200
