#!/usr/bin/env python3
"""Synthetic DOCX/PPTX fixture generator for file-remediation Phase 3
(Engine E1) tests.

No faculty content (plan §9, R11 — synthetic fixtures only). Deterministic:
no clock reads, no randomness. Pictures are generated in-process via
PyMuPDF (no Pillow dependency) — a tiny solid-color PNG.
"""

from __future__ import annotations

from pathlib import Path

import fitz  # PyMuPDF
from docx.shared import Pt
from docx import Document
from pptx import Presentation
from pptx.util import Inches


def _tiny_png(path, rgb=(200, 30, 30)):
    pix = fitz.Pixmap(fitz.csRGB, fitz.IRect(0, 0, 40, 40))
    pix.set_rect(pix.irect, rgb)
    pix.save(str(path))
    return path


def build_bad_docx(path, tmp_dir=None):
    """DOCX with every Engine E1 defect: one picture left at default (no)
    alt text (doc.inline_shapes[0]), two paragraphs styled bold/large to
    LOOK like headings but carrying no Heading style (paragraphs[1] =
    "Section One", paragraphs[3] = "Section Two"), one table with no
    header row (tables[0]), no document title. Returns path."""
    tmp_dir = Path(tmp_dir) if tmp_dir else Path(path).parent
    png = _tiny_png(tmp_dir / "_fixture_bad_docx.png")

    doc = Document()
    doc.add_picture(str(png))  # paragraphs[0]; default (unset) alt text

    p1 = doc.add_paragraph()  # paragraphs[1]
    r1 = p1.add_run("Section One")
    r1.bold = True
    r1.font.size = Pt(16)
    doc.add_paragraph("Body text under the fake heading, plain paragraph style.")  # [2]

    p2 = doc.add_paragraph()  # paragraphs[3]
    r2 = p2.add_run("Section Two")
    r2.bold = True
    r2.font.size = Pt(16)
    doc.add_paragraph("More plain body text under the second fake heading.")  # [4]

    table = doc.add_table(rows=2, cols=2)  # tables[0]; no tblHeader set
    table.cell(0, 0).text = "Header A"
    table.cell(0, 1).text = "Header B"
    table.cell(1, 0).text = "Value 1"
    table.cell(1, 1).text = "Value 2"

    doc.save(str(path))
    return Path(path)


def build_bad_pptx(path, tmp_dir=None):
    """PPTX with every Engine E1 defect: slide[0] is a blank layout with a
    textbox pretending to be a title (no real placeholder), slide[1] uses
    the Title Slide layout with a real ctrTitle left blank, slide[2] has a
    picture left at its default filename-based descr, slide[3] has a table
    with first_row off. No core title. Returns path."""
    tmp_dir = Path(tmp_dir) if tmp_dir else Path(path).parent
    png = _tiny_png(tmp_dir / "_fixture_bad_pptx.png")

    prs = Presentation()

    blank = prs.slides.add_slide(prs.slide_layouts[6])  # slides[0]: Blank, no title placeholder
    tb = blank.shapes.add_textbox(Inches(0.5), Inches(0.3), Inches(6), Inches(1))
    tb.text_frame.text = "Pseudo Title (textbox, not a real placeholder)"

    prs.slides.add_slide(prs.slide_layouts[0])  # slides[1]: Title Slide, ctrTitle left blank

    pic_slide = prs.slides.add_slide(prs.slide_layouts[6])  # slides[2]
    pic_slide.shapes.add_picture(str(png), 0, 0)  # descr defaults to filename

    table_slide = prs.slides.add_slide(prs.slide_layouts[6])  # slides[3]
    gf = table_slide.shapes.add_table(2, 2, 0, 0, Inches(3), Inches(2))
    gf.table.first_row = False
    gf.table.cell(0, 0).text = "Header A"
    gf.table.cell(0, 1).text = "Header B"
    gf.table.cell(1, 0).text = "Value 1"
    gf.table.cell(1, 1).text = "Value 2"

    prs.save(str(path))
    return Path(path)


def build_good_pptx(path, tmp_dir=None):
    """Already-clean PPTX: real title placeholders filled, picture alt set,
    table first_row on, core title/language set — the AC11 "well-formed
    deck" fixture (export + postfix should pass veraPDF without any
    judgment repairs needed beyond what LibreOffice/Impress tags itself)."""
    tmp_dir = Path(tmp_dir) if tmp_dir else Path(path).parent
    png = _tiny_png(tmp_dir / "_fixture_good_pptx.png")

    prs = Presentation()
    prs.core_properties.title = "Good Deck Fixture"
    prs.core_properties.language = "en-US"

    s1 = prs.slides.add_slide(prs.slide_layouts[0])
    s1.shapes.title.text = "Good Deck Fixture"

    s2 = prs.slides.add_slide(prs.slide_layouts[1])
    s2.shapes.title.text = "A Titled Content Slide"
    pic = s2.shapes.add_picture(str(png), Inches(1), Inches(2))
    pic._element.nvPicPr.cNvPr.set("descr", "A small solid red square, decorative accent.")

    s3 = prs.slides.add_slide(prs.slide_layouts[5])  # Title Only
    s3.shapes.title.text = "A Table Slide"
    gf = s3.shapes.add_table(2, 2, Inches(1), Inches(2), Inches(4), Inches(2))
    gf.table.first_row = True
    gf.table.cell(0, 0).text = "Header A"
    gf.table.cell(0, 1).text = "Header B"
    gf.table.cell(1, 0).text = "Value 1"
    gf.table.cell(1, 1).text = "Value 2"

    prs.save(str(path))
    return Path(path)
