<?php

require __DIR__ . '/../bootstrap.php';

use KeyLair\Auth;
use KeyLair\Csrf;
use KeyLair\Helpers;
use KeyLair\I18n;
use KeyLair\Settings;
use KeyLair\AuditLog;

$admin = Auth::requireAdminTierView();
$isDemo = $admin['role'] === 'demo';

$fields = [
    'legal_company_name' => 'Company name',
    'legal_street' => 'Street and house number',
    'legal_zip' => 'Postal code',
    'legal_city' => 'City',
    'legal_country' => 'Country',
    'legal_managing_director' => 'Managing director(s)',
    'legal_register_court' => 'Register court',
    'legal_register_number' => 'Register number',
    'legal_vat_id' => 'VAT ID',
    'legal_email' => 'Contact email',
    'legal_phone' => 'Phone (optional)',
    'legal_dpo' => 'Data protection officer (optional)',
];

$flash = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($isDemo) {
        http_response_code(403);
        die(I18n::t('legal.error_demo_readonly'));
    }
    Csrf::requireValid();

    $required = ['legal_company_name', 'legal_street', 'legal_zip', 'legal_city', 'legal_country', 'legal_managing_director', 'legal_email'];
    $missing = false;
    foreach ($fields as $key => $label) {
        $value = trim($_POST[$key] ?? '');
        if (in_array($key, $required, true) && $value === '') {
            $missing = true;
        }
    }

    if ($missing) {
        $flash = ['type' => 'error', 'message' => I18n::t('legal.flash.missing_required')];
    } else {
        foreach ($fields as $key => $label) {
            Settings::setLegal($key, trim($_POST[$key] ?? ''));
        }
        AuditLog::log($admin['id'], 'legal_info_updated', Settings::legal('legal_company_name'));
        $flash = ['type' => 'success', 'message' => I18n::t('legal.flash.saved')];
    }
}

$current = Settings::legalAll();

$pageTitle = I18n::t('legal.page_title');
$activeNav = 'legal';
$extraAdminCss = 'admin-product-editor';
require __DIR__ . '/includes/layout_header.php';
?>
<div class="topbar"><h1><?= Helpers::e(I18n::t('legal.page_title')) ?></h1></div>

<?php if ($flash): ?>
  <div class="form-section" style="border-color:<?= $flash['type'] === 'success' ? 'var(--signal-dim)' : 'var(--danger)' ?>; margin-bottom:16px;">
    <p style="color:<?= $flash['type'] === 'success' ? 'var(--signal)' : 'var(--danger)' ?>; font-size:13px; margin:0;"><?= Helpers::e($flash['message']) ?></p>
  </div>
<?php endif; ?>

<div class="form-section" style="margin-bottom:16px;">
  <p class="form-section-hint" style="margin:0;">
    <?= sprintf(
      I18n::t('legal.intro_hint'),
      '<a href="/imprint" target="_blank" rel="noopener">/imprint</a>',
      '<a href="/privacy" target="_blank" rel="noopener">/privacy</a>'
    ) ?>
  </p>
</div>

<form method="post">
  <?= Csrf::field() ?>
  <fieldset <?= $isDemo ? 'disabled' : '' ?> style="border:0; margin:0; padding:0;">

  <div class="form-section">
    <p class="form-section-title"><?= Helpers::e(I18n::t('legal.section_company')) ?></p>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_company_name')) ?></label>
      <input type="text" name="legal_company_name" value="<?= Helpers::e($current['legal_company_name']) ?>" required maxlength="128">
    </div>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_street')) ?></label>
      <input type="text" name="legal_street" value="<?= Helpers::e($current['legal_street']) ?>" required maxlength="128">
    </div>
    <div style="display:flex; gap:12px;">
      <div class="field" style="flex:0 0 140px;">
        <label><?= Helpers::e(I18n::t('legal.field_zip')) ?></label>
        <input type="text" name="legal_zip" value="<?= Helpers::e($current['legal_zip']) ?>" required maxlength="16">
      </div>
      <div class="field" style="flex:1;">
        <label><?= Helpers::e(I18n::t('legal.field_city')) ?></label>
        <input type="text" name="legal_city" value="<?= Helpers::e($current['legal_city']) ?>" required maxlength="64">
      </div>
    </div>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_country')) ?></label>
      <input type="text" name="legal_country" value="<?= Helpers::e($current['legal_country']) ?>" required maxlength="64">
    </div>
  </div>

  <div class="form-section">
    <p class="form-section-title"><?= Helpers::e(I18n::t('legal.section_representation')) ?></p>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_managing_director')) ?></label>
      <input type="text" name="legal_managing_director" value="<?= Helpers::e($current['legal_managing_director']) ?>" required maxlength="128">
    </div>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_register_court')) ?></label>
      <input type="text" name="legal_register_court" value="<?= Helpers::e($current['legal_register_court']) ?>" maxlength="128" placeholder="<?= Helpers::e(I18n::t('legal.field_register_court_placeholder')) ?>">
    </div>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_register_number')) ?></label>
      <input type="text" name="legal_register_number" value="<?= Helpers::e($current['legal_register_number']) ?>" maxlength="64">
    </div>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_vat_id')) ?></label>
      <input type="text" name="legal_vat_id" value="<?= Helpers::e($current['legal_vat_id']) ?>" maxlength="32">
    </div>
  </div>

  <div class="form-section">
    <p class="form-section-title"><?= Helpers::e(I18n::t('legal.section_contact')) ?></p>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_contact_email')) ?></label>
      <input type="email" name="legal_email" value="<?= Helpers::e($current['legal_email']) ?>" required maxlength="128">
    </div>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_phone')) ?></label>
      <input type="text" name="legal_phone" value="<?= Helpers::e($current['legal_phone']) ?>" maxlength="32">
    </div>
    <div class="field">
      <label><?= Helpers::e(I18n::t('legal.field_dpo')) ?></label>
      <input type="text" name="legal_dpo" value="<?= Helpers::e($current['legal_dpo']) ?>" maxlength="128" placeholder="<?= Helpers::e(I18n::t('legal.field_dpo_placeholder')) ?>">
    </div>
  </div>

  <div class="form-actions">
    <button type="submit" class="btn btn-primary"><?= Helpers::e(I18n::t('legal.save_button')) ?></button>
  </div>
  </fieldset>
</form>

<?php require __DIR__ . '/includes/layout_footer.php'; ?>
