<?php
/**
 * manifest.json dinamico per PWA
 * Configurabile tramite variabili .env
 */

require_once __DIR__ . '/../config.php';

// Determina base URL
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$siteUrl = env('SEO_SITE_URL', $protocol . $host);
$siteUrl = rtrim($siteUrl, '/');

// Configurazione manifest
$manifest = [
    'name' => env('PWA_NAME', $config['company']['name'] ?? 'Sito Web'),
    'short_name' => env('PWA_SHORT_NAME', substr($config['company']['name'] ?? 'Sito', 0, 12)),
    'description' => env('PWA_DESCRIPTION', $config['company']['description'] ?? ''),
    'start_url' => env('PWA_START_URL', '/'),
    'display' => env('PWA_DISPLAY', 'standalone'), // fullscreen, standalone, minimal-ui, browser
    'background_color' => env('PWA_BACKGROUND_COLOR', '#FFFFFF'),
    'theme_color' => env('PWA_THEME_COLOR', env('SEO_THEME_COLOR', $config['colors']['primary'] ?? '#DC2626')),
    'orientation' => env('PWA_ORIENTATION', 'portrait'), // portrait, landscape, any
    'scope' => env('PWA_SCOPE', '/'),
    'lang' => env('PWA_LANG', 'it'),
    'dir' => env('PWA_DIR', 'ltr'), // ltr, rtl, auto
];

// Icons
$icons = [];
$iconSizes = ['72', '96', '128', '144', '152', '192', '384', '512'];
$iconPath = env('PWA_ICON_PATH', $config['company']['logo'] ?? 'assets/images/logo-cliente.png');

foreach ($iconSizes as $size) {
    $icons[] = [
        'src' => $siteUrl . '/' . ltrim($iconPath, '/'),
        'sizes' => $size . 'x' . $size,
        'type' => 'image/png',
        'purpose' => 'any maskable'
    ];
}

// Aggiungi icona personalizzata se specificata
$customIcon = env('PWA_CUSTOM_ICON', '');
if (!empty($customIcon)) {
    $icons = [];
    $customIconSizes = explode(',', env('PWA_CUSTOM_ICON_SIZES', '192,512'));
    foreach ($customIconSizes as $size) {
        $size = trim($size);
        $icons[] = [
            'src' => $siteUrl . '/' . ltrim($customIcon, '/'),
            'sizes' => $size . 'x' . $size,
            'type' => 'image/png'
        ];
    }
}

$manifest['icons'] = $icons;

// Screenshots (opzionali)
$screenshots = env('PWA_SCREENSHOTS', '');
if (!empty($screenshots)) {
    $screenshotArray = explode(',', $screenshots);
    $manifest['screenshots'] = [];
    foreach ($screenshotArray as $screenshot) {
        $screenshot = trim($screenshot);
        if (!empty($screenshot)) {
            $manifest['screenshots'][] = [
                'src' => $siteUrl . '/' . ltrim($screenshot, '/'),
                'sizes' => '1280x720',
                'type' => 'image/png'
            ];
        }
    }
}

// Categories (opzionali)
$categories = env('PWA_CATEGORIES', '');
if (!empty($categories)) {
    $manifest['categories'] = explode(',', $categories);
}

// Shortcuts (opzionali)
$shortcuts = env('PWA_SHORTCUTS', '');
if (!empty($shortcuts)) {
    $shortcutArray = explode(';', $shortcuts);
    $manifest['shortcuts'] = [];
    foreach ($shortcutArray as $shortcut) {
        $parts = explode('|', trim($shortcut));
        if (count($parts) >= 2) {
            $manifest['shortcuts'][] = [
                'name' => $parts[0],
                'short_name' => $parts[1] ?? $parts[0],
                'url' => $siteUrl . '/' . ltrim($parts[2] ?? '/', '/'),
                'icons' => isset($parts[3]) ? [
                    [
                        'src' => $siteUrl . '/' . ltrim($parts[3], '/'),
                        'sizes' => '96x96'
                    ]
                ] : []
            ];
        }
    }
}

// Output JSON
header('Content-Type: application/manifest+json; charset=utf-8');

echo json_encode($manifest, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

