<?php
declare(strict_types=1);
require_once __DIR__ . '/_util.php';

$world = read_json(data_path('world.json')) ?? [];

$champions = [];
foreach (["mon","tue","wed","thu","fri","sat","sun"] as $id) {
  $champions[$id] = read_json(data_path("champions/$id.json")) ?? null;
}

$quests = [
  "dailies" => read_json(data_path('quests/dailies.json')) ?? [],
  "goals" => read_json(data_path('quests/goals.json')) ?? [],
  "random_pool" => read_json(data_path('quests/random_pool.json')) ?? []
];

$inventory = [
  "supplements" => read_json(data_path('inventory/supplements.json')) ?? [],
  "stash"       => read_json(data_path('inventory/stash.json')) ?? [],
  "wishlist"    => read_json(data_path('inventory/wishlist.json')) ?? []
];

$save = [
  "exportedAt" => iso_now(),
  "world" => $world,
  "champions" => $champions,
  "quests" => $quests,
  "inventory" => $inventory
];

$filename = "habit_rpg_save_" . date('Ymd_His') . ".json";
header('Content-Type: application/json; charset=utf-8');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Cache-Control: no-store');
echo json_encode($save, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);