import os

# All output lines for the JavaScript file
lines = []

def add(s):
    lines.append(s)

# ─── HELPERS (from part1) ───
add("const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell, ")
add("        Header, Footer, AlignmentType, LevelFormat, HeadingLevel, ")
add("        BorderStyle, WidthType, ShadingType, PageNumber, PageBreak } = require('docx');")
add("const fs = require('fs');")
add("")
add("const border = { style: BorderStyle.SINGLE, size: 1, color: 'BBBBBB' };")
add("const b = { top: border, bottom: border, left: border, right: border };")
add("")
add("function C(text, w, bold, fill, sz, color) {")
add("    let opts = { text: text||'', bold, font: 'Arial', size: sz||19 };")
add("    if (color) opts.color = color;")
add("    return new TableCell({ borders: b, width: { size: w, type: WidthType.DXA },")
add("        shading: fill ? { fill, type: ShadingType.CLEAR } : undefined,")
add("        margins: { top: 60, bottom: 60, left: 100, right: 100 },")
add("        children: [new Paragraph({ children: [new TextRun(opts)] })] });")
add("}")
add("function HC(text, w, sz) { return C(text, w, true, '1F4E79', sz||19, 'FFFFFF'); }")
add("function BC(text, w, sz) { return C(text, w, true, 'D5E8F0', sz||19); }")
add("function GC(text, w, sz) { return C(text, w, true, 'E8F5E9', sz||19); }")
add("function OC(text, w, sz) { return C(text, w, true, 'FFF3E0', sz||19); }")
add("function YC(text, w, sz) { return C(text, w, true, 'FFF9C4', sz||19); }")
add("function RC(text, w, sz) { return C(text, w, true, 'FFEBEE', sz||19); }")
add("")
add("function sp(n) { return new Paragraph({ spacing: { before: n||120 } }); }")
add("function P(text, opts) {")
add("    let runs = typeof text==='string' ? [new TextRun({ text, font:'Arial', size:21, ...(opts||{}) })] : text;")
add("    return new Paragraph({ children: runs });")
add("}")
add("function BP(text) { return P([new TextRun({ text, bold:true, font:'Arial', size:21 })]); }")
add("function Bul(text, bold, sz) { return new Paragraph({ numbering:{reference:'bullets',level:0},")
add("    children:[new TextRun({text,bold:!!bold,font:'Arial',size:sz||21})] }); }")
add("function Num(text, sz) { return new Paragraph({ numbering:{reference:'numbers',level:0},")
add("    children:[new TextRun({text,font:'Arial',size:sz||21})] }); }")
add("function PB() { return new Paragraph({ children: [new PageBreak()] }); }")
add("")
add("function MT(headers, rows, widths, rowFn) {")
add("    const hr = new TableRow({ children: headers.map((h,i) => HC(h, widths[i], 20)) });")
add("    const br = rows.map((r,ri) => {")
add("        const fill = ri%2===0 ? null : 'F5F9FC';")
add("        return new TableRow({ children: rowFn ? rowFn(r, widths, fill) : r.map((cv,i) => C(cv, widths[i], false, fill)) });")
add("    });")
add("    return new Table({ width:{size:9360,type:WidthType.DXA}, columnWidths:widths, rows:[hr,...br] });")
add("}")
add("")
add("function AdBox(title, copy, cta, format) {")
add("    return [")
add("        P([new TextRun({text:'   '+title,bold:true,font:'Arial',size:20,color:'1F4E79'})]),")
add("        new Paragraph({ border:{top:{style:BorderStyle.SINGLE,size:2,color:'1F4E79'},")
add("            bottom:{style:BorderStyle.SINGLE,size:2,color:'1F4E79'},")
add("            left:{style:BorderStyle.SINGLE,size:2,color:'1F4E79'},")
add("            right:{style:BorderStyle.SINGLE,size:2,color:'1F4E79'}},")
add("            spacing:{before:80,after:80},")
add("            children:[")
add("                new TextRun({text:'Format: ',bold:true,font:'Arial',size:19,color:'666666'}),")
add("                new TextRun({text:format,font:'Arial',size:19,color:'444444'}),")
add("                new TextRun({text:unescape('%0A%0A'),font:'Arial',size:16}),")
add("                new TextRun({text:copy,font:'Arial',size:19,italics:true,color:'333333'}),")
add("                new TextRun({text:unescape('%0A%0A'),font:'Arial',size:16}),")
add("                new TextRun({text:'CTA: ',bold:true,font:'Arial',size:19,color:'2E75B6'}),")
add("                new TextRun({text:cta,font:'Arial',size:19,bold:true})] }),")
add("        sp(60) ];")
add("}")
add("")
add("function CAd(country, flag, stage, audience, copy, cta, visual) {")
add("    return [")
add("        P([new TextRun({text:flag+' '+country+'  |  '+stage,bold:true,font:'Arial',size:22,color:'1F4E79'})]),")
add("        sp(40),")
add("        new Table({ width:{size:9360,type:WidthType.DXA}, columnWidths:[2200,7160], rows:[")
add("            new TableRow({ children:[HC('Element',2200,19),HC('Detail',7160,19)] }),")
add("            new TableRow({ children:[BC('Audience',2200,19),C(audience,7160,false,null,19)] }),")
add("            new TableRow({ children:[BC('Ad Copy',2200,19),C(copy,7160,false,'F9F9F9',19)] }),")
add("            new TableRow({ children:[BC('CTA',2200,19),C(cta,7160,true,'E8F5E9',19)] }),")
add("            new TableRow({ children:[BC('Visual Format',2200,19),C(visual,7160,false,null,19)] }),")
add("        ]}), sp(80) ];")
add("}")
add("")

os.makedirs('/app/working/workspaces/default', exist_ok=True)
with open('/app/working/workspaces/default/build_helpers.js', 'w') as f:
    f.write('\n'.join(lines))
print(f'Helpers: {len(lines)} lines written')
