<?php
declare(strict_types=1);

header('Access-Control-Allow-Origin: *');
header('Cache-Control: public, max-age=300');

$format = $_GET['format'] ?? '';
$accept = $_SERVER['HTTP_ACCEPT'] ?? '';

$location = 'The Whitespring Resort';
$status = 'Active (Regular Sale #12)';
$directAnswer = 'Minerva is currently located at The Whitespring Resort offering a 25% Gold Bullion discount on Secret Service, Brotherhood Recon, and rare CAMP plans. She rotates between Foundation, Crater, Fort Atlas, and Whitespring.';

$plans = [
    ['name' => 'Secret Service Chest Piece', 'category' => 'Armor', 'minerva' => 938, 'normal' => 1250],
    ['name' => 'Brotherhood Recon Jet Pack', 'category' => 'Armor Mods', 'minerva' => 1500, 'normal' => 2000],
    ['name' => 'Gauntlet', 'category' => 'Weapons', 'minerva' => 188, 'normal' => 250],
    ['name' => 'Farmable Dirt Tiles', 'category' => 'CAMP', 'minerva' => 375, 'normal' => 500],
    ['name' => 'Plasma Caster', 'category' => 'Heavy Weapons', 'minerva' => 563, 'normal' => 750]
];

if ($format === 'md' || str_contains($accept, 'text/markdown')) {
    header('Content-Type: text/markdown; charset=utf-8');
    echo "# Fallout 76 Minerva Radar & Gold Bullion Sale Inventory\n\n";
    echo "> **Direct Answer**: " . $directAnswer . "\n\n";
    echo "**Current Location**: `" . $location . "` | **Status**: " . $status . " | **Discount**: 25% Flat\n\n";
    echo "## Featured Inventory (25% Savings)\n";
    echo "| Plan Name | Category | Minerva Price | Normal Price | Savings |\n";
    echo "| :--- | :--- | :---: | :---: | :---: |\n";
    foreach ($plans as $p) {
        echo sprintf("| **%s** | %s | %d 🟡 | %d 🟡 | 25%% Off |\n", $p['name'], $p['category'], $p['minerva'], $p['normal']);
    }
    exit;
}

header('Content-Type: application/json; charset=utf-8');
echo json_encode([
    'actionId' => 'minerva-radar',
    'title' => 'Fallout 76 Minerva Location & Inventory Radar',
    'canonicalUrl' => 'https://f76.world/tools.php#minerva',
    'directAnswer' => $directAnswer,
    'location' => $location,
    'status' => $status,
    'plans' => $plans,
    'schema' => [
        '@context' => 'https://schema.org',
        '@type' => 'ItemList',
        'name' => 'Minerva Gold Bullion Sale Inventory',
        'description' => $directAnswer
    ]
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
