import io
import csv
import pytest
from app.ocr.services import csv_service
from tests.test_ocr import KEY, ocr  # noqa: F401

def test_raw_table_subcategorization_no_overwriting_and_coordinate_refs():
    # Two annual reports:
    # 1. FY 2014-15 reports 2014 (100) and 2015 (120) on Page 36
    # 2. FY 2015-16 reports 2015 (125) and 2016 (150) on Page 38
    # Note: 2015 was restated from 120 to 125. Both MUST be preserved!
    metrics = [
        {
            "id": 1,
            "field": "Accounts and other receivable",
            "concept_name": "Accounts and other receivable",
            "raw_field": "Accounts and other receivable",
            "statement_type": "BALANCE_SHEET",
            "category": "Current Assets",
            "aggregation_level": "LINE_ITEM",
            "scope": "CONSOLIDATED",
            "page_number": 36,
            "fiscal_year": "2014-15",
            "notes": ["Note 10"],
            "data": [
                {"year": 2014, "numeric_value": 100.0, "unit": "BDT", "multiplier": 1.0,
                 "table_num": 1, "table_row": 5, "table_col": 3},
                {"year": 2015, "numeric_value": 120.0, "unit": "BDT", "multiplier": 1.0,
                 "table_num": 1, "table_row": 5, "table_col": 2}
            ]
        },
        {
            "id": 2,
            "field": "Accounts and other receivable",
            "concept_name": "Accounts and other receivable",
            "raw_field": "Accounts and other receivable",
            "statement_type": "BALANCE_SHEET",
            "category": "Current Assets",
            "aggregation_level": "LINE_ITEM",
            "scope": "CONSOLIDATED",
            "page_number": 38,
            "fiscal_year": "2015-16",
            "notes": ["Note 10"],
            "data": [
                {"year": 2015, "numeric_value": 125.0, "unit": "BDT", "multiplier": 1.0,
                 "table_num": 2, "table_row": 8, "table_col": 3},
                {"year": 2016, "numeric_value": 150.0, "unit": "BDT", "multiplier": 1.0,
                 "table_num": 2, "table_row": 8, "table_col": 2}
            ]
        }
    ]

    res = csv_service.build_raw_csv_from_metrics(metrics)

    # 1. Assert raw_columns subcategorization
    raw_cols = res["raw_columns"]
    assert len(raw_cols) == 4
    col_keys = [c["key"] for c in raw_cols]
    assert col_keys == ["2014-15__2014", "2014-15__2015", "2015-16__2015", "2015-16__2016"]

    assert raw_cols[1]["report_fiscal_year"] == "2014-15"
    assert raw_cols[1]["observation_year"] == 2015
    assert raw_cols[1]["group_header"] == "FY 2014-15"
    assert raw_cols[1]["header"] == "2015"

    assert raw_cols[2]["report_fiscal_year"] == "2015-16"
    assert raw_cols[2]["observation_year"] == 2015
    assert raw_cols[2]["group_header"] == "FY 2015-16"
    assert raw_cols[2]["header"] == "2015"

    # 2. Assert raw_rows: single grouped row for this concept with BOTH 2015 observations
    assert len(res["rows"]) == 1
    row = res["rows"][0]
    assert row["field"] == "Accounts and other receivable"

    # Both values exist simultaneously in row["values"]
    assert row["values"]["2014-15__2014"] == 100.0
    assert row["values"]["2014-15__2015"] == 120.0
    assert row["values"]["2015-16__2015"] == 125.0
    assert row["values"]["2015-16__2016"] == 150.0

    # 3. Assert coordinate ref tokens on cells
    cells = row["cells"]
    cell_1415 = cells["2014-15__2015"]
    assert cell_1415["value"] == 120.0
    assert cell_1415["page_number"] == 36
    assert cell_1415["table_num"] == 1
    assert cell_1415["table_row"] == 5
    assert cell_1415["table_col"] == 2
    assert cell_1415["report_fiscal_year"] == "2014-15" and "ref" not in cell_1415      # the frontend builds the reference

    cell_1516 = cells["2015-16__2015"]
    assert cell_1516["value"] == 125.0
    assert cell_1516["page_number"] == 38
    assert cell_1516["table_num"] == 2
    assert cell_1516["table_row"] == 8
    assert cell_1516["table_col"] == 3
    assert cell_1516["report_fiscal_year"] == "2015-16" and "ref" not in cell_1516

    # 4. Assert no restatement dots on raw row
    assert row["restatement_dots"] == {}
    assert row["duplicate_in_report_years"] == []

    # 5. Assert CSV two-tier format
    csv_text = res["csv_text"]
    lines = list(csv.reader(io.StringIO(csv_text)))
    assert lines[0] == ["field", "unit", "tag", "statement_type", "FY 2014-15", "FY 2014-15", "FY 2015-16", "FY 2015-16"]
    assert lines[1] == ["", "", "", "", "2014", "2015", "2015", "2016"]
    assert lines[2] == ["Accounts and other receivable", "BDT", "FINANCIAL_METRIC", "BALANCE_SHEET", "100", "120", "125", "150"]

    # 6. Assert round-trip through parse_csv_text_to_rows
    parsed_rows = csv_service.parse_csv_text_to_rows(csv_text)
    assert len(parsed_rows) == 1
    p_row = parsed_rows[0]
    assert p_row["values"]["2014-15__2015"] == 120.0
    assert p_row["values"]["2015-16__2015"] == 125.0
    assert p_row["values"]["FY 2014-15 | 2015"] == 120.0
    assert p_row["values"]["FY 2015-16 | 2015"] == 125.0


def test_raw_table_results_endpoint(ocr, monkeypatch):
    from app.ocr import database
    from tests.test_ocr import KEY

    sample_metrics = [
        {
            "id": 1,
            "field": "Inventory",
            "concept_name": "Inventory",
            "raw_field": "Inventory",
            "statement_type": "BALANCE_SHEET",
            "category": "Current Assets",
            "aggregation_level": "LINE_ITEM",
            "scope": "CONSOLIDATED",
            "page_number": 10,
            "fiscal_year": "2023-24",
            "notes": [],
            "data": [
                {"year": 2023, "numeric_value": 50.0, "unit": "BDT", "multiplier": 1.0,
                 "table_num": 1, "table_row": 3, "table_col": 2},
                {"year": 2024, "numeric_value": 60.0, "unit": "BDT", "multiplier": 1.0,
                 "table_num": 1, "table_row": 3, "table_col": 1}
            ]
        }
    ]
    monkeypatch.setattr(database, "get_all_metrics_for_company", lambda code: sample_metrics)
    monkeypatch.setattr(database, "get_company_results", lambda code: {})

    resp = ocr.get("/ocr/company/TESTCO/results/raw", headers=KEY, params={"view": "full"})
    assert resp.status_code == 200
    data = resp.json()
    assert "raw_columns" in data
    assert len(data["raw_columns"]) == 2
    assert data["raw_columns"][0]["key"] == "2023-24__2023"
    assert data["raw_columns"][1]["key"] == "2023-24__2024"
    assert len(data["raw_rows"]) == 1
    assert data["raw_rows"][0]["restatement_dots"] == {}
    assert "cells" in data["raw_rows"][0]
    cell = data["raw_rows"][0]["cells"]["2023-24__2024"]
    assert "ref" not in cell and (cell["report_fiscal_year"], cell["page_number"], cell["table_num"], cell["table_row"], cell["table_col"]) == ("2023-24", 10, 1, 3, 1)



def test_raw_table_default_rows_are_slim(ocr, monkeypatch):
    """Without view=full a raw row has only what the table needs; the cell says where it is printed."""
    slim = ocr.get("/ocr/company/TESTCO/results/raw", headers=KEY).json()
    full = ocr.get("/ocr/company/TESTCO/results/raw", headers=KEY, params={"view": "full"}).json()
    row = slim["raw_rows"][0]
    assert set(row) == {"field", "raw_field", "statement_type", "scope", "unit", "metric_type", "cells"}
    assert set(row["cells"]["2023-24__2024"]) == {"value", "page_number", "table_num", "table_row", "table_col"}
    big = full["raw_rows"][0]["cells"]["2023-24__2024"]
    assert row["cells"]["2023-24__2024"] == {k: big[k] for k in ("value", "page_number", "table_num", "table_row", "table_col")}
    assert row["field"] == full["raw_rows"][0]["field"] and "values" not in row and "restatement_dots" not in row
    assert {k: v for k, v in slim.items() if k != "raw_rows"} == {k: v for k, v in full.items() if k != "raw_rows"}   # same columns and paging
    assert len(str(slim["raw_rows"])) < len(str(full["raw_rows"]))
