<?php

declare(strict_types=1);

// ─── Production Safety Guard ─────────────────────────────────────────────────
// This script exposes database statistics. It MUST NEVER run in production.
(function () {
    $envFile = __DIR__ . '/.env';
    $appEnv  = 'production';
    $devMode = 'false';
    if (is_readable($envFile)) {
        foreach (file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
            $line = trim($line);
            if ($line === '' || $line[0] === '#' || strpos($line, '=') === false) {
                continue;
            }
            list($k, $v) = explode('=', $line, 2);
            $k = trim($k);
            $v = trim($v);
            if ($k === 'APP_ENV')       $appEnv  = $v;
            if ($k === 'DEVELOPER_MODE') $devMode = strtolower($v);
        }
    }
    if ($appEnv !== 'local' || $devMode !== 'true') {
        if (!headers_sent()) {
            http_response_code(403);
        }
        echo "403 Forbidden — this script may only run in local developer mode.\n";
        exit(1);
    }
})();
// ─────────────────────────────────────────────────────────────────────────────

define('ROOT_PATH', __DIR__);

require_once ROOT_PATH . '/app/Core/Autoloader.php';
\App\Core\Autoloader::register();
require_once ROOT_PATH . '/app/helpers/env.php';
require_once ROOT_PATH . '/app/helpers/config.php';

$envFile = ROOT_PATH . '/.env';
if (file_exists($envFile)) {
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line === '' || $line[0] === '#') continue;
        if (strpos($line, '=') !== false) {
            list($key, $value) = explode('=', $line, 2);
            $_ENV[trim($key)] = trim($value);
        }
    }
}

use App\Core\Database;

$db = Database::getConnection();

$users = $db->query("SELECT id, username, email, display_name FROM users ORDER BY id ASC")->fetchAll(\PDO::FETCH_ASSOC);
$workspaces = $db->query("SELECT id, slug, groom_name, bride_name FROM workspaces")->fetchAll(\PDO::FETCH_ASSOC);
$invitations = $db->query("SELECT id, workspace_id, invitation_status FROM invitations")->fetchAll(\PDO::FETCH_ASSOC);
$guestsCount = (int) $db->query("SELECT COUNT(*) FROM guests WHERE is_deleted = 0")->fetchColumn();
$ceremoniesCount = (int) $db->query("SELECT COUNT(*) FROM wedding_events WHERE is_deleted = 0")->fetchColumn();
$attendanceCount = (int) $db->query("SELECT COUNT(*) FROM checkins")->fetchColumn();
$rsvpCount = (int) $db->query("SELECT COUNT(*) FROM rsvps")->fetchColumn();
$wishesCount = (int) $db->query("SELECT COUNT(*) FROM wishes")->fetchColumn();

echo "=== DATABASE RESET VERIFICATION SUMMARY ===\n\n";

echo "Users (" . count($users) . "):\n";
foreach ($users as $u) {
    echo " - Username: " . ($u['username'] ?: $u['email']) . " | Name: " . $u['display_name'] . "\n";
}

echo "\nWorkspaces (" . count($workspaces) . "):\n";
foreach ($workspaces as $w) {
    echo " - Slug: " . $w['slug'] . " | Couple: " . $w['groom_name'] . " & " . $w['bride_name'] . "\n";
}

echo "\nInvitations (" . count($invitations) . "):\n";
foreach ($invitations as $inv) {
    echo " - Workspace ID: " . $inv['workspace_id'] . " | Status: " . $inv['invitation_status'] . "\n";
}

echo "\nGuests Count: " . $guestsCount . "\n";
echo "Ceremonies Count: " . $ceremoniesCount . "\n";
echo "Attendance (Checkins) Count: " . $attendanceCount . "\n";
echo "RSVP Count: " . $rsvpCount . "\n";
echo "Guest Wishes Count: " . $wishesCount . "\n";
