<?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();
$imprint  = $settings['imprint'][$lang] ?? ($settings['imprint']['en'] ?? []);

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

$t = [
    'title'      => $lang === 'de' ? 'Impressum' : 'Imprint',
    'owner'      => $lang === 'de' ? 'Medieninhaber' : 'Media owner',
    'address'    => $lang === 'de' ? 'Adresse' : 'Address',
    'contact'    => $lang === 'de' ? 'Kontakt' : 'Contact',
    'legal'      => $lang === 'de' ? 'Rechtliches' : 'Legal',
    'purpose'    => $lang === 'de' ? 'Unternehmensgegenstand / Blattlinie' : 'Business purpose / editorial line',
    'authority'  => $lang === 'de' ? 'Aufsichts-/Gewerbebehörde' : 'Supervisory authority',
    'membership' => $lang === 'de' ? 'WKO-Mitgliedschaft' : 'Chamber membership',
    'uid'        => $lang === 'de' ? 'UID' : 'VAT ID',
    'odr'        => $lang === 'de' ? 'Verbraucherstreitbeilegung / ODR' : 'Consumer dispute resolution / ODR',
    'last_updated' => $lang === 'de' ? 'Stand' : 'Last updated',
];

function legal_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>';
}

$pageTitle   = e($t['title']) . ' | FlixCooks';
$description = $lang === 'de'
    ? 'Pflichtangaben für diese Website gemäß ECG, MedienG und GewO.'
    : 'Mandatory website information in line with Austrian ECG, Media Act and Trade Regulation.';

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'
    ? 'Pflichtangaben für diese Website gemäß ECG, MedienG und GewO.'
    : 'Mandatory website information in line with Austrian ECG, Media Act and Trade Regulation.'; ?></p>

  <div class="legal-card legal-section">
    <h2><?php echo e($t['owner']); ?></h2>
    <?php
      legal_item($t['owner'],   $imprint['owner_name'] ?? '');
      legal_item($t['address'], $imprint['address'] ?? '');
      legal_item($t['contact'], ($imprint['email'] ?? '') . ($imprint['phone'] ? ' | ' . $imprint['phone'] : ''));
      legal_item($t['legal'],   $imprint['legal_form'] ?? '');
      legal_item($t['purpose'], $imprint['business_purpose'] ?? '');
      legal_item($t['membership'], $imprint['wko_membership'] ?? '');
      legal_item($t['authority'],  $imprint['authority'] ?? '');
      legal_item($t['uid'],        $imprint['uid'] ?? '');
      if (!empty($imprint['odr'])) {
          $odrText = $lang === 'de'
            ? 'Die EU-ODR-Plattform wurde am 20. Juli 2025 eingestellt. Informationen zur Verbraucherstreitbeilegung finden Sie hier:'
            : 'The EU ODR platform was discontinued on 20 July 2025. Consumer dispute resolution information:';
          echo '<div class="legal-item"><span class="legal-label">' . e($t['odr']) . '</span><span>' . e($odrText) . ' <a href="' . e($imprint['odr']) . '" target="_blank" rel="noopener" class="underline-link">consumer-redress.ec.europa.eu</a></span></div>';
      }
      legal_item($t['last_updated'], $imprint['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>
