import pytest


def _auth_headers(tokens: dict) -> dict:
    return {"Authorization": f"Bearer {tokens['access_token']}"}


def test_form_phases_list_smoke(client, admin_tokens):
    response = client.get(
        "/api/v1/stages/form/1", 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_form_phases_list_filter_smoke(client, admin_tokens):

    response = client.get(
        "/api/v1/stages/form/1", headers=_auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "count" in payload
    count = payload["count"]

    response = client.get(
        "/api/v1/stages/form/1?sheet=AHR&phase_code=test", headers=_auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "count" in payload
    assert payload["count"] <= count


def test_form_phases_create_forbidden(client, isp_tokens):
    response = client.post(
        "/api/v1/stages/form/1",
        json={
            "sheet": "AHR",
            "phase_code": "new_phase",
            "role": "DFIP",
            "column_keys": ["plan.q1"],
            "opens_at": "2026-01-01T00:00:00Z",
            "closes_at": "2026-12-31T00:00:00Z",
        },
        headers=_auth_headers(isp_tokens),
    )
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_form_phases_update_forbidden(client, isp_tokens):
    response = client.patch(
        "/api/v1/stages/form/1/AHR/test",
        json={
            "role": "DFIP",
            "column_keys": ["plan.q1"],
            "opens_at": "2026-01-01T00:00:00Z",
            "closes_at": "2026-12-31T00:00:00Z",
        },
        headers=_auth_headers(isp_tokens),
    )
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_form_phases_delete_forbidden(client, isp_tokens):
    response = client.delete(
        "/api/v1/stages/form/1/AHR/test",
        headers=_auth_headers(isp_tokens),
    )
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_form_phases_create(client, admin_tokens):
    body = {
        "budget_form_id": 4,
        "sheet": "AHR",
        "phase_code": "new_phase",
        "role": "DFIP",
        "column_keys": ["plan.q1"],
        "opens_at": "2026-01-01T00:00:00",
        "closes_at": "2026-12-31T00:00:00",
    }
    response = client.post(
        "/api/v1/stages/form/4",
        json=body,
        headers=_auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert "success" in payload
    assert payload["success"]
    assert "result" in payload
    result = payload["result"]
    assert isinstance(result, dict)
    opens_at = result.pop("opens_at", None)
    assert "+05" in opens_at
    assert body.pop("opens_at") == opens_at.split("+")[0]
    closes_at = result.pop("closes_at", None)
    assert "+05" in closes_at
    assert body.pop("closes_at") == closes_at.split("+")[0]
    assert result == body

    assert payload["result"] == body


def test_form_phases_update(client, admin_tokens):
    body = {
        "budget_form_id": 4,
        "sheet": "AHR",
        "phase_code": "test",
        "role": "EXECUTOR_RF",
        "column_keys": ["plan.q1"],
        "opens_at": "2026-01-01T00:00:00",
        "closes_at": "2026-12-31T00:00:00",
    }
    response = client.patch(
        "/api/v1/stages/form/4/AHR/test",
        json=body,
        headers=_auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()

    assert "success" in payload
    assert payload["success"]
    assert "result" in payload
    result = payload["result"]
    assert isinstance(result, dict)
    opens_at = result.pop("opens_at", None)
    assert "+05" in opens_at
    assert body.pop("opens_at") == opens_at.split("+")[0]
    closes_at = result.pop("closes_at", None)
    assert "+05" in closes_at
    assert body.pop("closes_at") == closes_at.split("+")[0]
    assert result == body


def test_form_phases_delete(client, admin_tokens):
    response = client.delete(
        "/api/v1/stages/form/1/AHR/test",
        headers=_auth_headers(admin_tokens),
    )
    assert response.status_code == 200


def test_form3_phases_list_smoke(client, admin_tokens):
    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
    assert "count" in payload
    assert isinstance(payload["result"], list)


def test_form3_phases_list_filter_smoke(client, admin_tokens):

    response = client.get(
        "/api/v1/stages/project/2", headers=_auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "count" in payload
    count = payload["count"]

    response = client.get(
        "/api/v1/stages/project/2?&phase_code=test", headers=_auth_headers(admin_tokens)
    )
    assert response.status_code == 200
    payload = response.json()
    assert "count" in payload
    assert payload["count"] <= count


def test_form3_phases_create_forbidden(client, isp_tokens):
    response = client.post(
        "/api/v1/stages/project/2",
        json={
            "year": 2026,
            "report_type": "LIMIT",
            "phase_code": "CODE1",
            "role": "DFIP",
            "column_keys": [
                    "q1.adj_by_items",
                    "q1.adj_increase"
            ],
            "opens_at": "2027-07-20T00:00:00",
            "closes_at": "2027-09-21T00:00:00"
        },
        headers=_auth_headers(isp_tokens),
    )
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_form3_phases_update_forbidden(client, isp_tokens):
    response = client.patch(
        "/api/v1/stages/project/2/2026/LIMIT/RF_FILL_2026",
        json={
            "role": "DFIP",
            "column_keys": [
                "q1.adj_by_items",
                "q1.adj_increase"
            ],
            "opens_at": "2027-07-20T00:00:00",
            "closes_at": "2027-09-21T00:00:00"
        },
        headers=_auth_headers(isp_tokens),
    )
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_form3_phases_delete_forbidden(client, isp_tokens):
    response = client.delete(
        "/api/v1/stages/project/2/2026/LIMIT/RF_FILL_2026",
        headers=_auth_headers(isp_tokens),
    )
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_form3_phases_create(client, admin_tokens):
    body = {
        "year": 2026,
        "report_type": "LIMIT",
        "phase_code": "CODE1",
        "role": "DFIP",
        "column_keys": [
                "q1.adj_by_items",
                "q1.adj_increase"
        ],
        "opens_at": "2027-07-20T00:00:00",
        "closes_at": "2027-09-21T00:00:00"
    }
    response = client.post(
        "/api/v1/stages/project/2",
        json=body,
        headers=_auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert "success" in payload
    assert payload["success"]
    assert "result" in payload
    result = payload["result"]
    assert isinstance(result, dict)
    opens_at = result.pop("opens_at", None)
    assert "+03" in opens_at
    assert body.pop("opens_at") == opens_at.split("+")[0]
    closes_at = result.pop("closes_at", None)
    assert "+03" in closes_at
    assert body.pop("closes_at") == closes_at.split("+")[0]
    assert result == body

    assert payload["result"] == body


def test_form3_phases_update(client, admin_tokens):
    body = {
        "role": "DFIP",
        "column_keys": [
            "q1.adj_by_items",
            "q1.adj_increase"
        ],
        "opens_at": "2027-07-20T00:00:00",
        "closes_at": "2027-09-21T00:00:00"
    }
    response = client.patch(
        "/api/v1/stages/project/2/2026/LIMIT/RF_FILL_2026",
        json=body,
        headers=_auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()

    assert "success" in payload
    assert payload["success"]
    assert "result" in payload
    result = payload["result"]
    assert isinstance(result, dict)
    opens_at = result.pop("opens_at", None)
    assert "+03" in opens_at
    assert body.pop("opens_at") == opens_at.split("+")[0]
    closes_at = result.pop("closes_at", None)
    assert "+03" in closes_at
    assert body.pop("closes_at") == closes_at.split("+")[0]
    
    body.update({'phase_code': 'RF_FILL_2026', 'report_type': 'LIMIT', 'year': 2026})
    assert result == body


def test_form3_phases_delete(client, admin_tokens):
    response = client.delete(
        "/api/v1/stages/project/2/2026/LIMIT/RF_FILL_2026",
        headers=_auth_headers(admin_tokens),
    )
    assert response.status_code == 200

