"""Structural note rules applied to OCR Markdown (app/ocr/services/ocr_markdown.py)."""
from app.ocr.services.ocr_markdown import normalize_note_tables
from app.ocr.services.extraction_verifier import parse_markdown_tables

HEAD = "| Particulars | Note | 2013 | 2012 |\n|---|---|---|---|\n"


def test_note_title_in_the_note_column_becomes_a_heading_and_splits_the_table():
    md = (HEAD + "| **41.a** | **Payments for other operating activities** | | |\n| Rent | | 10 | 9 |\n"
          "| **Total** | | **10** | **9** |\n| | | | |\n| **42.** | **CONSOLIDATED INCREASE/(DECREASE) OF OTHER ASSETS** | | |\n"
          "| The Bank (note 42.a) | | 5 | 4 |\n| **Total** | | **5** | **4** |\n")
    out = normalize_note_tables(md)
    assert "### 41.a Payments for other operating activities" in out
    assert "### 42. CONSOLIDATED INCREASE/(DECREASE) OF OTHER ASSETS" in out
    tables = parse_markdown_tables(out)
    assert [t["rows"][0][0] for t in tables] == ["Rent", "The Bank (note 42.a)"]
    assert all(r[1] == "" or r[1][0].isdigit() for t in tables for r in t["rows"])   # no text in the Note column


def test_note_title_as_a_particulars_row_becomes_a_heading():
    md = (HEAD + "| **41.a Payments for other operating activities - The Bank** | | | |\n| Rent | | 10 | 9 |\n"
          "| **Total** | | **10** | **9** |\n\n" + HEAD + "| **42. CONSOLIDATED OTHER ASSETS** | | | |\n| A | | 5 | 4 |\n")
    out = normalize_note_tables(md)
    assert out.count("### ") == 2 and "| **41.a" not in out and "| **42." not in out
    assert [t["rows"][0][0] for t in parse_markdown_tables(out)] == ["Rent", "A"]


def test_descriptive_text_in_the_note_column_moves_to_particulars_without_moving_values():
    md = HEAD + "| Cash | in hand | 10 | 9 |\n| Bank | 4 | 7 | 6 |\n"
    rows = parse_markdown_tables(normalize_note_tables(md))[0]["rows"]
    assert rows[0] == ["Cash in hand", "", "10", "9"] and rows[1] == ["Bank", "4", "7", "6"]


def test_numbered_sections_inside_a_primary_statement_stay_in_one_table():
    md = ("CONSOLIDATED CASH FLOW STATEMENT\n\n" + HEAD + "| **1. Cash flows from operating activities** | | | |\n"
          "| Receipts | | 10 | 9 |\n| **Net cash from operating activities** | | 10 | 9 |\n"
          "| **2. Cash flows from investing activities** | | | |\n| Purchases | | (3) | (2) |\n")
    out = normalize_note_tables(md)
    tables = parse_markdown_tables(out)
    assert len(tables) == 1 and tables[0]["rows"][2][0].startswith("**2.")          # mid-statement section kept
    assert "### 1. Cash flows from operating activities" in out                        # only the top title row lifted


def test_tables_without_note_rows_are_unchanged():
    md = "Source: X\n\n" + HEAD + "| Cash | 4 | 10 | 9 |\n| **Total** | | **10** | **9** |\n"
    assert normalize_note_tables(md) == md


def test_on_a_notes_page_every_note_title_row_splits_the_table():
    md = (HEAD + "| **47 Consolidated EPS** | | | |\n| (i) Profit | | 10 | 9 |\n| (ii) Shares | | 5 | 5 |\n"
          "| **EPS (i/ii)** | | **2.00** | **1.80** |\n| **47.a EPS - The Bank** | | | |\n| (i) Profit | | 8 | 7 |\n")
    out = normalize_note_tables(md)
    assert "### 47 Consolidated EPS" in out and "### 47.a EPS - The Bank" in out
    assert len(parse_markdown_tables(out)) == 2
