<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/LegalTools.php';

dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();

$input  = dbnToolsJsonInput(600000);
$text   = dbnToolsString($input, 'text', 500000);
$format = in_array((string)($input['format'] ?? ''), ['txt', 'docx'], true)
    ? (string)$input['format']
    : 'txt';

if ($format === 'txt') {
    header('Content-Type: text/plain; charset=UTF-8');
    header('Content-Disposition: attachment; filename="redacted.txt"');
    header('Cache-Control: no-store');
    echo $text;
    exit;
}

// DOCX: minimal valid Office Open XML package built with ZipArchive
if (!class_exists('ZipArchive')) {
    http_response_code(500);
    echo json_encode(['error' => ['message' => 'ZipArchive extension not available.']]);
    exit;
}

$docx = buildMinimalDocx($text);

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="redacted.docx"');
header('Content-Length: ' . strlen($docx));
header('Cache-Control: no-store');
echo $docx;
exit;

function buildMinimalDocx(string $text): string
{
    $tmp = tempnam(sys_get_temp_dir(), 'dbn_docx_');
    @unlink($tmp);
    $tmp .= '.docx';

    $zip = new ZipArchive();
    $zip->open($tmp, ZipArchive::CREATE | ZipArchive::OVERWRITE);

    $zip->addFromString('[Content_Types].xml', contentTypesXml());
    $zip->addFromString('_rels/.rels', relsXml());
    $zip->addFromString('word/document.xml', documentXml($text));
    $zip->addFromString('word/_rels/document.xml.rels', wordRelsXml());
    $zip->addFromString('docProps/app.xml', appXml());
    $zip->addFromString('docProps/core.xml', coreXml());

    $zip->close();

    $bytes = file_get_contents($tmp);
    @unlink($tmp);
    return $bytes;
}

function documentXml(string $text): string
{
    $lines = explode("\n", str_replace("\r\n", "\n", str_replace("\r", "\n", $text)));
    $paras = [];
    foreach ($lines as $line) {
        $safe = htmlspecialchars($line, ENT_XML1 | ENT_COMPAT, 'UTF-8');
        if ($safe === '') {
            $paras[] = '<w:p/>';
        } else {
            $paras[] = '<w:p><w:r><w:rPr><w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/><w:sz w:val="20"/></w:rPr>'
                     . '<w:t xml:space="preserve">' . $safe . '</w:t></w:r></w:p>';
        }
    }
    return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
        . '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
        . '<w:body>' . implode('', $paras)
        . '<w:sectPr><w:pgSz w:w="12240" w:h="15840"/>'
        . '<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/>'
        . '</w:sectPr></w:body></w:document>';
}

function contentTypesXml(): string
{
    return '<?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"/>'
        . '<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>'
        . '<Override PartName="/docProps/core.xml" ContentType="application/package/2006/metadata/core-properties+xml"/>'
        . '</Types>';
}

function relsXml(): string
{
    return '<?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"/>'
        . '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>'
        . '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>'
        . '</Relationships>';
}

function wordRelsXml(): string
{
    return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
        . '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>';
}

function appXml(): string
{
    return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
        . '<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">'
        . '<Application>DoBetterNorge Redact</Application>'
        . '</Properties>';
}

function coreXml(): string
{
    $date = date('Y-m-d\TH:i:s\Z');
    return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
        . '<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"'
        . ' xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/"'
        . ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
        . '<dc:creator>DoBetterNorge</dc:creator>'
        . '<dcterms:created xsi:type="dcterms:W3CDTF">' . $date . '</dcterms:created>'
        . '</cp:coreProperties>';
}
