<?php
/**
 * Clear all finance data: receipts, budgets, transactions, pledges, funds.
 * Use this before a complete finance overhaul. Table structures are kept.
 * Requires admin or system_admin. Run from browser: .../install/clear-finance-data.php
 */

require_once __DIR__ . '/../config/app.php';
require_once __DIR__ . '/../includes/session-bootstrap.php';
nazareth_bootstrap_session_cookie();
session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/auth.php';

requireAuth();
if (!isAdmin() && !isSystemAdmin()) {
    header('Location: ' . BASE_PATH . '/');
    exit;
}

$done = false;
$error = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['confirm']) && $_POST['confirm'] === '1') {
    try {
        $pdo->exec('SET FOREIGN_KEY_CHECKS = 0');

        $tables = ['receipts', 'budgets', 'transactions', 'pledges', 'funds'];
        foreach ($tables as $table) {
            try {
                $pdo->exec("TRUNCATE TABLE `$table`");
            } catch (PDOException $e) {
                if (strpos($e->getMessage(), 'exist') === false) {
                    throw $e;
                }
                // Table doesn't exist; skip
            }
        }

        $pdo->exec('SET FOREIGN_KEY_CHECKS = 1');
        $done = true;
    } catch (PDOException $e) {
        $pdo->exec('SET FOREIGN_KEY_CHECKS = 1');
        $error = $e->getMessage();
    }
}

$pageTitle = 'Clear finance data';
require_once __DIR__ . '/../includes/header.php';
?>

<div class="header">
    <h1>Clear all finance data</h1>
    <div class="header-actions">
        <a href="<?= BASE_PATH ?>/finance" class="btn btn-secondary">← Finance</a>
        <a href="<?= BASE_PATH ?>/logout">Logout</a>
    </div>
</div>

<?php if ($error): ?>
<div class="alert" style="background: #fef2f2; border-left: 4px solid #ef4444; padding: 1rem; margin-bottom: 1rem; border-radius: 4px;">
    <?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>

<?php if ($done): ?>
<div class="card" style="margin-bottom: 1rem;">
    <div class="card-body">
        <p><strong>All finance data has been cleared.</strong></p>
        <p>Emptied: receipts, budgets, transactions, pledges, funds. Table structures are unchanged so you can rebuild funds and start fresh.</p>
        <a href="<?= BASE_PATH ?>/finance" class="btn">Go to Finance</a>
    </div>
</div>
<?php else: ?>
<div class="card">
    <div class="card-body">
        <p>This will <strong>permanently delete</strong> all:</p>
        <ul>
            <li>Receipts</li>
            <li>Budgets</li>
            <li>Transactions (offerings, etc.)</li>
            <li>Pledges</li>
            <li>Funds (types/categories)</li>
        </ul>
        <p>Table structures are kept so you can add new funds and start over. This cannot be undone.</p>
        <form method="post">
            <input type="hidden" name="confirm" value="1">
            <button type="submit" class="btn" style="background: #dc2626;">Clear all finance data</button>
            <a href="<?= BASE_PATH ?>/finance" class="btn btn-secondary">Cancel</a>
        </form>
    </div>
</div>
<?php endif; ?>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
