<?php
require __DIR__ . '/helpers.php';

$lang     = strtolower((string)(filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?: 'en')) === 'de' ? 'de' : 'en';
$settings = load_site_settings();
$privacy  = $settings['privacy'][$lang] ?? ($settings['privacy']['en'] ?? []);

$langSwitchLabel = $lang === 'de' ? 'DE' : 'EN';
$langSwitchHref  = lang_url($lang === 'de' ? 'en' : 'de');

$t = [
    'title'      => $lang === 'de' ? 'Datenschutzerklärung' : 'Privacy Policy',
    'controller' => $lang === 'de' ? 'Verantwortlicher' : 'Controller',
    'address'    => $lang === 'de' ? 'Adresse' : 'Address',
    'contact'    => $lang === 'de' ? 'Kontakt für Datenschutz' : 'Privacy contact',
    'hosting'    => $lang === 'de' ? 'Hosting' : 'Hosting',
    'logs'       => $lang === 'de' ? 'Server-Logs' : 'Server logs',
    'cookies'    => $lang === 'de' ? 'Cookies' : 'Cookies',
    'purposes'   => $lang === 'de' ? 'Zwecke der Datenverarbeitung' : 'Purposes of processing',
    'rights'     => $lang === 'de' ? 'Ihre Rechte' : 'Your rights',
    'last_updated' => $lang === 'de' ? 'Stand' : 'Last updated',
];

function privacy_item(string $label, ?string $value): void {
    $value = trim((string) $value);
    if ($value === '') return;
    echo '<div class="legal-item"><span class="legal-label">' . e($label) . '</span><span>' . nl2br(e($value)) . '</span></div>';
}

$rights = $lang === 'de'
    ? 'Auskunft, Berichtigung, Löschung, Einschränkung, Datenübertragbarkeit, Widerspruch, Beschwerde bei der Datenschutzbehörde.'
    : 'Access, rectification, erasure, restriction, data portability, objection, complaint to the data protection authority.';

$logInfo = '';
if (!empty($privacy['log_retention_days'])) {
    $logInfo = ($lang === 'de'
        ? 'Server-Logs (IP-Adresse, Zeitpunkt, URL, User-Agent) werden zur Betriebssicherheit gespeichert und nach '
        : 'Server logs (IP address, timestamp, URL, user agent) are kept for security and deleted after ')
        . intval($privacy['log_retention_days']) . ($lang === 'de' ? ' Tagen gelöscht.' : ' days.');
}

$pageTitle   = e($t['title']) . ' | FlixCooks';
$description = $lang === 'de'
    ? 'Informationen gemäß DSGVO und TKG über die Verarbeitung personenbezogener Daten.'
    : 'Information per GDPR and TKG on how personal data is processed.';

include __DIR__ . '/partials/head.php';
include __DIR__ . '/partials/header.php';
?>
<main class="legal-page">
  <h1><?php echo e($t['title']); ?></h1>
  <p><?php echo $lang === 'de'
    ? 'Informationen gemäß DSGVO und TKG über die Verarbeitung personenbezogener Daten.'
    : 'Information per GDPR and TKG on how personal data is processed.'; ?></p>

  <div class="legal-card legal-section">
    <h2><?php echo e($t['controller']); ?></h2>
    <?php
      privacy_item($t['controller'], $privacy['controller'] ?? '');
      privacy_item($t['address'],    $privacy['address'] ?? '');
      privacy_item($t['contact'],    ($privacy['contact_email'] ?? '') . ($privacy['contact_phone'] ? ' | ' . $privacy['contact_phone'] : ''));
      privacy_item($t['hosting'],    $privacy['hosting_provider'] ?? '');
      privacy_item($t['purposes'],   $privacy['purposes'] ?? '');
      if ($logInfo) { privacy_item($t['logs'], $logInfo); }
      privacy_item($t['cookies'], $privacy['cookie_statement'] ?? '');
      privacy_item($t['rights'],  $rights);
      privacy_item($t['last_updated'], $privacy['last_updated'] ?? '');
    ?>
  </div>
</main>
<?php include __DIR__ . '/partials/footer.php'; ?>
<script>
if (typeof Lenis !== 'undefined') {
  var lenis = new Lenis({ duration: 1.2, smooth: true, smoothTouch: false });
  window.lenis = lenis;
  function raf(t) { lenis.raf(t); requestAnimationFrame(raf); }
  requestAnimationFrame(raf);
}
</script>
