import pytest
from api.v1.modules.public.reference_data.countries.routes import router
from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()
app.include_router(router)
client = TestClient(app)


@pytest.mark.usefixtures("client", "access_token")
def test_list_countries_with_search(client, access_token):
    headers = {"Authorization": f"Bearer {access_token}"}
    # Search for a known country from seed, e.g., "Mexico" or "MEX"
    response = client.get("/countries/?search=Mexico", headers=headers)
    assert response.status_code == 200
    data = response.json()
    assert "items" in data
    # Depending on seed data, there should be at least one item if "Mexico" exists
    if data["items"]:
        for item in data["items"]:
            assert "mexico" in item["description_es"].lower() or "mexico" in item["description_en"].lower()


@pytest.mark.usefixtures("client", "access_token")
def test_get_country_not_found(client, access_token):
    headers = {"Authorization": f"Bearer {access_token}"}
    response = client.get("/countries/invalid_key", headers=headers)
    assert response.status_code == 404


def test_create_country_forbidden():
    response = client.post("/countries/", json={"m3_key": "TST", "description": "Test"})
    assert response.status_code in (403, 405, 404)


def test_update_country_forbidden():
    response = client.put(
        "/countries/TST", json={"m3_key": "TST", "description": "Test"}
    )
    assert response.status_code in (403, 405, 404)


def test_delete_country_forbidden():
    response = client.delete("/countries/TST")
    assert response.status_code in (403, 405, 404)
