import pytest


def test_forms_list_smoke(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/form/", 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_smoke(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/form/1", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    payload = response.json()
    assert "result" in payload
    assert isinstance(payload["result"], dict)


def test_forms_sheets_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.get(
        "/api/v1/form/999/sheets", headers=auth_headers(admin_tokens)
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload
    assert isinstance(payload["message"], str)


def test_forms_sheet_get_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.get(
        "/api/v1/form/999/sheet/AHR", headers=auth_headers(admin_tokens)
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_forms_cell_update_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.patch(
        "/api/v1/form/999/sheet/AHR/cell",
        json={"line_id": 1, "column": "q1.m1", "value": 100},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_forms_cell_update(client, admin_tokens, auth_headers):
    response = client.patch(
        "/api/v1/form/1/sheet/AHR/cell?direction=Support&sections=plan,contract_summary",
        json={
            "line_id": 1,
            "column": "plan.q1",
            "value": 225
        },
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert isinstance(payload, dict)
    assert "result" in payload


def test_forms_cells_update_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.patch(
        "/api/v1/form/999/sheet/AHR/cells",
        json={"changes": [{"line_id": 1, "column": "q1.m1", "value": 100}]},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_forms_cells_update(client, admin_tokens, auth_headers):
    response = client.patch(
        "/api/v1/form/1/sheet/AHR/cells?direction=Support&sections=plan,contract_summary",
        json={
            "changes": [
                {
                    "line_id": 1,
                    "column": "plan.q1",
                    "value": 226
                }
            ],
        },
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert isinstance(payload, dict)
    assert "result" in payload


def test_forms_add_line_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.post(
        "/api/v1/form/999/sheet/AHR/line",
        json={},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_forms_add_line(client, admin_tokens, auth_headers):
    response = client.post(
        "/api/v1/form/1/sheet/AHR/line?direction=Support&sections=plan,contract_summary",
        json={
            "expense_item_id": 120,
            "direction": "Support"
        },
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()
    assert isinstance(payload, dict)
    assert "result" in payload


def test_forms_delete_line_not_found_error_shape(client, admin_tokens, auth_headers):
    response = client.delete(
        "/api/v1/form/999/sheet/AHR/line/1",
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_forms_add_form_success(client, admin_tokens, auth_headers):
    response = client.post(
        "/api/v1/form/",
        json={"year": 2025, "form_type_code": "FORM_1", "org_unit_ids": [1]},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()

    assert "result" in payload
    assert isinstance(payload["result"], list)
    assert len(payload["result"]) == 1
    assert "year" in payload["result"][0]
    assert payload["result"][0]["year"] == 2025

    assert "id" in payload["result"][0]
    form_id = payload["result"][0]["id"]
    response = client.get(
        f"/api/v1/form/{form_id}",
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200


def test_forms_add_form_existing_unit(client, admin_tokens, auth_headers):
    response = client.post(
        "/api/v1/form/",
        json={"year": 2026, "form_type_code": "FORM_2", "org_unit_ids": [1, 2]},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 200
    payload = response.json()

    assert "result" in payload
    assert isinstance(payload["result"], list)
    assert len(payload["result"]) == 2
    assert "year" in payload["result"][0]
    assert payload["result"][0]["year"] == 2026

    assert "id" in payload["result"][1]
    assert payload["result"][1]["id"] == 4


def test_forms_add_form_org_unit_not_found(client, admin_tokens, auth_headers):
    response = client.post(
        "/api/v1/form/",
        json={"year": 2025, "form_type_code": "FORM_1", "org_unit_ids": [1, 999]},
        headers=auth_headers(admin_tokens),
    )
    assert response.status_code == 404
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload


def test_forms_add_form_forbidden_not_admin(client, isp_tokens, auth_headers):
    response = client.post(
        "/api/v1/form/",
        json={"year": 2025, "form_type_code": "FORM_1", "org_unit_ids": [1]},
        headers=auth_headers(isp_tokens),
    )
    assert response.status_code == 403
    payload = response.json()
    assert isinstance(payload, dict)
    assert "message" in payload
