<?php

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

use KeyLair\Auth;
use KeyLair\I18n;
use KeyLair\PageStyle;

Auth::requireLogin();

$key = $_GET['key'] ?? '';
$version = $_GET['version'] ?? 'active';

if (!PageStyle::isValidKey($key) || !in_array($version, ['active', 'default'], true)) {
    http_response_code(400);
    die(I18n::t('design_download.invalid_request'));
}

$content = $version === 'default' ? PageStyle::defaultContent($key) : PageStyle::activeContent($key);
$filename = $key . ($version === 'default' ? '.default' : '') . '.css';

header('Content-Type: text/css; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . strlen($content));
echo $content;
