from ova_portable_text import (
    bibliography_entry,
    create_document,
    footnote_entry,
    footnote_ref,
    glossary_entry,
    glossary_term,
    image_asset,
    image_block,
    link_def,
    paragraph,
    pie_chart_from_parallel_arrays,
    section,
    span,
    table_block,
    table_column,
    table_dataset,
    xref,
)


report = create_document(
    title="Full Workflow Demo",
    language="en",
    subtitle="Step 8 end-to-end example",
    documentType="report",
)

report.add_image_asset(
    image_asset(
        id="img-overview",
        src="https://example.com/overview.png",
        alt="Overview diagram",
    )
)

report.add_table_dataset(
    table_dataset(
        id="table-kpis",
        columns=[
            table_column(key="name", header="KPI"),
            table_column(key="value", header="Value"),
        ],
        rows=[
            {"name": "Revenue", "value": "15.8M"},
            {"name": "Growth", "value": "18%"},
        ],
    )
)

report.add_chart_dataset(
    pie_chart_from_parallel_arrays(
        id="chart-share",
        area_en=["Technology", "Finance", "Healthcare"],
        area_zh=["技术", "金融", "医疗"],
        value=[55, 30, 15],
    )
)

report.add_bibliography_entry(
    bibliography_entry(
        id="bib-pt",
        title="Portable Text Specification",
        authors=["Portable Text Authors"],
        year=2026,
        type="specification",
    )
)

report.add_footnote(
    footnote_entry(
        id="fn-note-1",
        blocks=[paragraph("This is a footnote generated by OVAPortableText.")],
    )
)

report.add_glossary_entry(
    glossary_entry(
        id="term-kpi",
        term="KPI",
        definition="Key Performance Indicator.",
        aliases=["Key KPI"],
    )
)

intro = report.new_section(id="sec-1", level=1, title="Executive Summary")
intro.append_lead("This section demonstrates a complete authoring workflow.")
intro.append_paragraph(
    "The report references ",
    glossary_term("term-kpi"),
    ", cites ",
    xref(target_type="bibliography", target_id="bib-pt"),
    ", and includes a footnote",
    footnote_ref("fn-note-1"),
    ".",
)
intro.append_paragraph(
    "Visit ",
    span("OpenAI", marks=["m-openai"]),
    " for more examples.",
    mark_defs=[link_def(key="m-openai", href="https://openai.com", title="OpenAI")],
)
intro.append_image_with_caption(id="fig-overview", image_ref="img-overview", caption="Figure 1. System overview")
intro.append_table_with_caption(id="tbl-kpis", table_ref="table-kpis", caption="Table 1. KPI summary")
intro.append_chart_with_caption(id="fig-share", chart_ref="chart-share", caption="Figure 2. Area share")

sub = intro.new_subsection(id="sec-1-1", title="Notes")
sub.append_bullet_item("Builder API can be used incrementally.")
sub.append_bullet_item("Validation should be run before export.")
sub.append_paragraph("See ", xref(target_type="figure", target_id="fig-share"), " for the chart block instance.")

report.assert_valid()
print(report.build_resolver().debug_summary())
print(report.to_json())
