"""Generate DOCX: Internal memo."""

from pathlib import Path
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH


def generate(output_path: Path, firm: dict, body: str, date: str, subject: str):
    """Generate an internal memo document."""
    doc = Document()

    style = doc.styles["Normal"]
    font = style.font
    font.name = "Arial"
    font.size = Pt(11)

    # MEMO header
    title = doc.add_paragraph()
    title.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run = title.add_run("INTERNAL MEMORANDUM")
    run.bold = True
    run.font.size = Pt(16)
    run.font.color.rgb = RGBColor(0, 51, 102)

    # Confidential marker
    conf = doc.add_paragraph()
    conf.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run = conf.add_run("CONFIDENTIAL")
    run.font.size = Pt(9)
    run.font.color.rgb = RGBColor(200, 0, 0)

    doc.add_paragraph("_" * 60).runs[0].font.color.rgb = RGBColor(180, 180, 180)

    # Memo fields
    fields = [
        ("TO:", firm["attorney"]["name"]),
        ("FROM:", firm["attorney"]["name"]),
        ("DATE:", date),
        ("RE:", subject),
    ]

    for label, value in fields:
        p = doc.add_paragraph()
        run_label = p.add_run(f"{label}\t")
        run_label.bold = True
        run_label.font.size = Pt(11)
        run_value = p.add_run(value)
        run_value.font.size = Pt(11)

    doc.add_paragraph("_" * 60).runs[0].font.color.rgb = RGBColor(180, 180, 180)
    doc.add_paragraph()

    # Body
    for para_text in body.split("\n\n"):
        if para_text.strip():
            doc.add_paragraph(para_text.strip())

    doc.add_paragraph()

    # Sign-off
    p = doc.add_paragraph()
    run = p.add_run(firm["attorney"]["name"])
    run.bold = True
    doc.add_paragraph("Partner")
    doc.add_paragraph(firm["name"])

    doc.save(str(output_path))
