<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Database;

final class DoiExportController extends Controller
{
    public function exportXml(): void
    {
        require_permission('doi.export');

        $submissionId = (int) ($_GET['submission_id'] ?? 0);
        if ($submissionId <= 0) {
            http_response_code(400);
            exit('submission_id required');
        }

        $db = Database::getInstance();

        $stmt = $db->prepare("
            SELECT s.*, c.title_en AS conference_title_en, c.title_ar AS conference_title_ar
            FROM submissions s
            INNER JOIN conferences c ON c.id = s.conference_id
            WHERE s.id = :id
            LIMIT 1
        ");
        $stmt->execute([':id' => $submissionId]);
        $s = $stmt->fetch();
        if (!$s) {
            http_response_code(404);
            exit('Not found');
        }

        $stmt = $db->prepare("SELECT * FROM submission_doi_metadata WHERE submission_id = :id LIMIT 1");
        $stmt->execute([':id' => $submissionId]);
        $d = $stmt->fetch();
        if (!$d || empty($d['doi'])) {
            http_response_code(400);
            exit('DOI not set');
        }

        $meta = [];
        if (!empty($d['meta_json'])) {
            $meta = json_decode((string)$d['meta_json'], true) ?: [];
        }

        $doi = (string)$d['doi'];
        $url = (string)($d['landing_url'] ?? '');
        $publisher = (string)($d['publisher_name'] ?? 'JPU');
        $year = (int)($d['year'] ?? (int)date('Y'));

        $title = (string)($meta['title_en'] ?? ($s['title'] ?? ''));
        $authors = (string)($meta['authors'] ?? '');
        $keywords = (string)($meta['keywords'] ?? '');

        header('Content-Type: application/xml; charset=UTF-8');
        header('Content-Disposition: attachment; filename="doi_' . $submissionId . '.xml"');

        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        echo "<doi_record>\n";
        echo "  <publisher>" . htmlspecialchars($publisher, ENT_QUOTES, 'UTF-8') . "</publisher>\n";
        echo "  <conference>" . htmlspecialchars((string)($s['conference_title_en'] ?? ''), ENT_QUOTES, 'UTF-8') . "</conference>\n";
        echo "  <paper>\n";
        echo "    <submission_no>" . htmlspecialchars((string)($s['submission_no'] ?? ''), ENT_QUOTES, 'UTF-8') . "</submission_no>\n";
        echo "    <title>" . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . "</title>\n";
        echo "    <authors>" . htmlspecialchars($authors, ENT_QUOTES, 'UTF-8') . "</authors>\n";
        echo "    <keywords>" . htmlspecialchars($keywords, ENT_QUOTES, 'UTF-8') . "</keywords>\n";
        echo "    <year>" . $year . "</year>\n";
        echo "    <doi>" . htmlspecialchars($doi, ENT_QUOTES, 'UTF-8') . "</doi>\n";
        echo "    <landing_url>" . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . "</landing_url>\n";
        echo "  </paper>\n";
        echo "</doi_record>\n";
        exit;
    }
}
