<?php

// Generates a minimal DOCX template with token placeholders for testing.
// Output: resources/examples/contract_summary_template.docx

$outDir = __DIR__;
$file = $outDir.DIRECTORY_SEPARATOR.'contract_summary_template.docx';

if (file_exists($file)) {
    unlink($file);
}

$contentTypes = <<<'XML'
<?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"/>
  <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>
XML;

$rels = <<<'XML'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
XML;

$document = <<<'XML'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
 xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
 xmlns:v="urn:schemas-microsoft-com:vml"
 xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
 xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
 xmlns:w10="urn:schemas-microsoft-com:office:word"
 xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
 xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
 xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
 xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
 xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
 xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
 mc:Ignorable="w14 wp14">
 <w:body>
  <w:p><w:r><w:t>Contract Summary Report</w:t></w:r></w:p>
  <w:p><w:r><w:t>Reference: {{contract.reference}}</w:t></w:r></w:p>
  <w:p><w:r><w:t>Client Case Ref: {{client_case.client_ref}}</w:t></w:r></w:p>
  <w:p><w:r><w:t>Start Date: {{contract.start_date}}</w:t></w:r></w:p>
  <w:p><w:r><w:t>End Date: {{contract.end_date}}</w:t></w:r></w:p>
  <w:p><w:r><w:t>Description: {{contract.description}}</w:t></w:r></w:p>
  <w:p><w:r><w:t>Generated By: {{generation.user_name}} on {{generation.date}}</w:t></w:r></w:p>
 </w:body>
</w:document>
XML;

$zip = new ZipArchive;
if ($zip->open($file, ZipArchive::CREATE) !== true) {
    fwrite(STDERR, "Cannot create docx file\n");
    exit(1);
}
// Core parts
$zip->addFromString('[Content_Types].xml', $contentTypes);
$zip->addEmptyDir('_rels');
$zip->addFromString('_rels/.rels', $rels);
$zip->addEmptyDir('word');
$zip->addFromString('word/document.xml', $document);
$zip->close();

$hash = hash_file('sha256', $file);
$size = filesize($file);
echo json_encode([
    'file' => $file,
    'size_bytes' => $size,
    'sha256' => $hash,
    'tokens_included' => [
        'contract.reference', 'client_case.client_ref', 'contract.start_date', 'contract.end_date', 'contract.description', 'generation.user_name', 'generation.date',
    ],
], JSON_PRETTY_PRINT).PHP_EOL;
