import pydantic_core
import pytest


def test_org_units_list_smoke(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/org-unit/", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    assert "count" in payload
    assert isinstance(payload["result"], list)


def test_org_units_list_with_users_count(client, admin_tokens, auth_headers):
    response = client.get(
        "/api/v1/org-unit/?load_users_count=true",
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    assert isinstance(payload["result"], list)
    if payload["result"]:
        assert "users_count" in payload["result"][0]


def test_org_units_get(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/org-unit/1", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    assert payload["result"]["id"] == 1


def test_org_units_get_not_found(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/org-unit/99999", headers=auth_headers(admin_tokens))
    assert response.status_code == 404


# def test_org_units_create_smoke(client, admin_tokens, auth_headers):
#     suffix = uuid.uuid4().hex[:8]
#     title = f"Test Org {suffix}"
#     response = client.post(
#         "/api/v1/org-unit/",
#         json={"title": title, "is_ssp": True},
#         headers=auth_headers(admin_tokens),
#     )
#     assert response.status_code == 200
#     payload = response.json()
#     assert payload["result"]["title"] == title
#     assert payload["result"]["is_ssp"] is True


# def test_org_units_create_non_ssp(client, admin_tokens, auth_headers):
#     suffix = uuid.uuid4().hex[:8]
#     title = f"Test RF {suffix}"
#     response = client.post(
#         "/api/v1/org-unit/",
#         json={"title": title, "is_ssp": False},
#         headers=auth_headers(admin_tokens),
#     )
#     assert response.status_code == 200
#     payload = response.json()
#     assert payload["result"]["title"] == title
#     assert payload["result"]["is_ssp"] is False


# def test_org_units_update_smoke(client, admin_tokens, auth_headers):
#     new_title = f"Updated_{uuid.uuid4().hex[:8]}"
#     response = client.patch(
#         "/api/v1/org-unit/1",
#         json={"title": new_title},
#         headers=auth_headers(admin_tokens),
#     )
#     assert response.status_code == 200
#     payload = response.json()
#     assert payload["result"]["title"] == new_title
#     assert payload["result"]["id"] == 1


def test_org_units_update_offset_smoke(client, admin_tokens, auth_headers):
    old_offset = "+05"
    new_offset = "+04"
    response = client.get(
        "/api/v1/stages/form/4?sheet=AHR&phase_code=test", headers=auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    phases = payload["result"]
    assert isinstance(phases, list)
    assert len(phases)
    phase_old = phases[0]

    response = client.patch(
        "/api/v1/org-unit/2",
        json={"utc_offset": new_offset},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert payload["result"]["id"] == 2
    assert payload["result"]["utc_offset"] == new_offset

    response = client.get(
        "/api/v1/stages/form/4?sheet=AHR&phase_code=test", headers=auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    phases = payload["result"]
    assert isinstance(phases, list)
    assert len(phases)
    phase_new = phases[0]

    assert new_offset in phase_new["opens_at"]
    assert new_offset in phase_new["closes_at"]
    assert phase_old["opens_at"].split(old_offset)[0] == phase_new["opens_at"].split(new_offset)[0]
    assert phase_old["closes_at"].split(old_offset)[0] == phase_new["closes_at"].split(new_offset)[0]



def test_org_units_form3_update_offset_smoke(client, admin_tokens, auth_headers):
    old_offset = "+03"
    new_offset = "+04"
    response = client.get(
        "/api/v1/stages/project/2", headers=auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    phases = payload["result"]
    assert isinstance(phases, list)
    assert len(phases)
    phase_old = phases[0]

    response = client.patch(
        "/api/v1/org-unit/1",
        json={"utc_offset": new_offset},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert payload["result"]["id"] == 1
    assert payload["result"]["utc_offset"] == new_offset

    response = client.get(
        "/api/v1/stages/project/2", headers=auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    phases = payload["result"]
    assert isinstance(phases, list)
    assert len(phases)
    phase_new = phases[0]

    assert new_offset in phase_new["opens_at"]
    assert new_offset in phase_new["closes_at"]
    assert phase_old["opens_at"].split(old_offset)[0] == phase_new["opens_at"].split(new_offset)[0]
    assert phase_old["closes_at"].split(old_offset)[0] == phase_new["closes_at"].split(new_offset)[0]


def test_org_units_update_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.patch(
        "/api/v1/org-unit/99999",
        json={"title": "No Matter"},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "code" in payload
    assert "message" in payload


def test_org_units_delete_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.delete(
        "/api/v1/org-unit/99999", headers=auth_headers(admin_tokens)
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "code" in payload
    assert "message" in payload


def test_org_units_access_denied_for_non_admin(client, isp_tokens, auth_headers):
    response = client.get("/api/v1/org-unit/", headers=auth_headers(isp_tokens))
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "code" in payload
    assert "message" in payload
