"""Build a small DOCX that exercises the parts of a Word file a translation
round trip most often damages: heading styles, a table, header and footer,
a hyperlink, a footnote, a comment, and glossary terms that a translator can
get wrong ("seat", "workspace", "admin").

Standard library only. Usage:
    python3 make_docx_fixture.py [output.docx]
"""
import sys
import zipfile

W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"'


def run(text, bold=False):
    props = "<w:rPr><w:b/></w:rPr>" if bold else ""
    return f'<w:r>{props}<w:t xml:space="preserve">{text}</w:t></w:r>'


def para(*runs, style=None):
    ppr = f'<w:pPr><w:pStyle w:val="{style}"/></w:pPr>' if style else ""
    return f"<w:p>{ppr}{''.join(runs)}</w:p>"


def cell(text):
    return f"<w:tc><w:tcPr><w:tcW w:w=\"3000\" w:type=\"dxa\"/></w:tcPr>{para(run(text))}</w:tc>"


def row(*texts):
    return "<w:tr>" + "".join(cell(t) for t in texts) + "</w:tr>"


body = "".join([
    para(run("Workspace Admin Guide"), style="Heading1"),
    para(run("Every workspace has at least one admin. An admin can add a "),
         run("seat", bold=True),
         run(" for each new member of the workspace."),
         '<w:r><w:rPr><w:rStyle w:val="FootnoteReference"/></w:rPr><w:footnoteReference w:id="1"/></w:r>'),
    para(run("Add a seat"), style="Heading2"),
    para('<w:commentRangeStart w:id="0"/>',
         run("Open the workspace settings and choose Billing. Each seat is billed monthly."),
         '<w:commentRangeEnd w:id="0"/>',
         '<w:r><w:commentReference w:id="0"/></w:r>'),
    "<w:tbl><w:tblPr><w:tblStyle w:val=\"TableGrid\"/><w:tblW w:w=\"9000\" w:type=\"dxa\"/></w:tblPr>"
    "<w:tblGrid><w:gridCol w:w=\"3000\"/><w:gridCol w:w=\"3000\"/><w:gridCol w:w=\"3000\"/></w:tblGrid>"
    + row("Role", "Can add a seat", "Can delete the workspace")
    + row("Owner", "Yes", "Yes")
    + row("Admin", "Yes", "No")
    + "</w:tbl>",
    para(run("Remove a seat"), style="Heading2"),
    para(run("An admin can remove a seat at any time. Read the "),
         '<w:hyperlink r:id="rIdLink">' + run("billing help page") + "</w:hyperlink>",
         run(" before removing the last admin seat.")),
])

document = (f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document {W}><w:body>{body}'
            '<w:sectPr><w:headerReference w:type="default" r:id="rIdHeader"/>'
            '<w:footerReference w:type="default" r:id="rIdFooter"/></w:sectPr></w:body></w:document>')

styles = (f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles {W}>'
          '<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/></w:style>'
          '<w:style w:type="paragraph" w:styleId="Heading1"><w:name w:val="heading 1"/><w:basedOn w:val="Normal"/><w:rPr><w:b/><w:sz w:val="36"/></w:rPr></w:style>'
          '<w:style w:type="paragraph" w:styleId="Heading2"><w:name w:val="heading 2"/><w:basedOn w:val="Normal"/><w:rPr><w:b/><w:sz w:val="28"/></w:rPr></w:style>'
          '<w:style w:type="character" w:styleId="FootnoteReference"><w:name w:val="footnote reference"/><w:rPr><w:vertAlign w:val="superscript"/></w:rPr></w:style>'
          '<w:style w:type="table" w:styleId="TableGrid"><w:name w:val="Table Grid"/></w:style>'
          '</w:styles>')

footnotes = (f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:footnotes {W}>'
             '<w:footnote w:type="separator" w:id="-1"><w:p><w:r><w:separator/></w:r></w:p></w:footnote>'
             '<w:footnote w:type="continuationSeparator" w:id="0"><w:p><w:r><w:continuationSeparator/></w:r></w:p></w:footnote>'
             '<w:footnote w:id="1">' + para(run("A seat is a paid user license, not a physical place.")) + '</w:footnote></w:footnotes>')

comments = (f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:comments {W}>'
            '<w:comment w:id="0" w:author="Reviewer" w:initials="R">' + para(run("Confirm the billing period with finance.")) + '</w:comment></w:comments>')

header = f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:hdr {W}>' + para(run("Internal: workspace admins only")) + "</w:hdr>"
footer = f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:ftr {W}>' + para(run("Version 1.0, admin guide")) + "</w:ftr>"

R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
doc_rels = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
            '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
            f'<Relationship Id="rIdStyles" Type="{R}/styles" Target="styles.xml"/>'
            f'<Relationship Id="rIdFootnotes" Type="{R}/footnotes" Target="footnotes.xml"/>'
            f'<Relationship Id="rIdComments" Type="{R}/comments" Target="comments.xml"/>'
            f'<Relationship Id="rIdHeader" Type="{R}/header" Target="header1.xml"/>'
            f'<Relationship Id="rIdFooter" Type="{R}/footer" Target="footer1.xml"/>'
            f'<Relationship Id="rIdLink" Type="{R}/hyperlink" Target="https://example.org/billing-help" TargetMode="External"/>'
            '</Relationships>')

CT = "application/vnd.openxmlformats-officedocument.wordprocessingml"
content_types = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
                 '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
                 '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'
                 '<Default Extension="xml" ContentType="application/xml"/>'
                 f'<Override PartName="/word/document.xml" ContentType="{CT}.document.main+xml"/>'
                 f'<Override PartName="/word/styles.xml" ContentType="{CT}.styles+xml"/>'
                 f'<Override PartName="/word/footnotes.xml" ContentType="{CT}.footnotes+xml"/>'
                 f'<Override PartName="/word/comments.xml" ContentType="{CT}.comments+xml"/>'
                 f'<Override PartName="/word/header1.xml" ContentType="{CT}.header+xml"/>'
                 f'<Override PartName="/word/footer1.xml" ContentType="{CT}.footer+xml"/>'
                 '</Types>')

root_rels = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
             '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
             f'<Relationship Id="rId1" Type="{R}/officeDocument" Target="word/document.xml"/></Relationships>')

out = sys.argv[1] if len(sys.argv) > 1 else "docx-roundtrip-source.docx"
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as z:
    for name, data in [("[Content_Types].xml", content_types), ("_rels/.rels", root_rels),
                       ("word/document.xml", document), ("word/_rels/document.xml.rels", doc_rels),
                       ("word/styles.xml", styles), ("word/footnotes.xml", footnotes),
                       ("word/comments.xml", comments), ("word/header1.xml", header),
                       ("word/footer1.xml", footer)]:
        z.writestr(zipfile.ZipInfo(name, (2026, 10, 21, 0, 0, 0)), data)
print(out)
