<?php

require_once __DIR__ . '/Include/Config.php';
require_once __DIR__ . '/Include/PageInit.php';

use ChurchCRM\Authentication\AuthenticationManager;
use Propel\Runtime\Propel;

// Security: user must be admin
AuthenticationManager::redirectHomeIfNotAdmin();

try {
    $pdo = Propel::getConnection();
    
    // Read the SQL file
    $sqlFile = __DIR__ . '/mysql/expense-schema.sql';
    if (!file_exists($sqlFile)) {
        die("SQL file not found: $sqlFile");
    }
    
    $sql = file_get_contents($sqlFile);
    
    // Split by semicolon and execute each statement
    $statements = array_filter(array_map('trim', explode(';', $sql)));
    
    $createdTables = 0;
    $insertedRecords = 0;
    
    foreach ($statements as $statement) {
        if (empty($statement)) {
            continue;
        }
        
        if (strpos($statement, 'CREATE TABLE') !== false) {
            $createdTables++;
        } elseif (strpos($statement, 'INSERT') !== false) {
            $insertedRecords += 10; // Approximate count
        }
        
        $pdo->exec($statement);
    }
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Expense Module Setup</title>
        <style>
            body { font-family: Arial; margin: 20px; }
            .success { background: #d4edda; color: #155724; padding: 15px; border-radius: 5px; }
            .info { background: #d1ecf1; color: #0c5460; padding: 15px; border-radius: 5px; margin-top: 15px; }
            h2 { color: #333; }
            a { display: inline-block; margin-top: 15px; padding: 10px 15px; background: #007bff; color: white; text-decoration: none; border-radius: 3px; }
        </style>
    </head>
    <body>
        <h2>✓ Expense Module Setup Complete!</h2>
        
        <div class="success">
            <strong>Database tables created successfully!</strong>
        </div>
        
        <div class="info">
            <h3>What was created:</h3>
            <ul>
                <li><strong>expense_exp</strong> - Main expenses table (stores all expense records)</li>
                <li><strong>expense_category_cat</strong> - Expense categories table with 10 default categories:
                    <ul>
                        <li>Utilities</li>
                        <li>Salaries & Wages</li>
                        <li>Office Supplies</li>
                        <li>Maintenance & Repairs</li>
                        <li>Programs & Events</li>
                        <li>Travel & Transportation</li>
                        <li>Professional Services</li>
                        <li>Insurance</li>
                        <li>Education & Training</li>
                        <li>Miscellaneous</li>
                    </ul>
                </li>
            </ul>
        </div>
        
        <div class="info">
            <h3>You can now:</h3>
            <ul>
                <li>Go to <a href="/ExpenseEditor.php">Expense Entry</a> to record new expenses</li>
                <li>Go to <a href="/ExpenseList.php">Expense List</a> to view and manage expenses</li>
                <li>Go to <a href="/ExpenseCategoryEditor.php">Category Management</a> to add custom categories</li>
            </ul>
        </div>
        
        <a href="/finance/">← Back to Finance</a>
    </body>
    </html>
    <?php
    
} catch (\Exception $e) {
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Setup Error</title>
        <style>
            body { font-family: Arial; margin: 20px; }
            .error { background: #f8d7da; color: #721c24; padding: 15px; border-radius: 5px; }
            h2 { color: #721c24; }
        </style>
    </head>
    <body>
        <h2>✗ Setup Failed</h2>
        
        <div class="error">
            <strong>Error:</strong> <?= htmlspecialchars($e->getMessage()) ?>
            <pre><?= htmlspecialchars($e->getTraceAsString()) ?></pre>
        </div>
        
        <p><a href="javascript:history.back()">← Go Back</a></p>
    </body>
    </html>
    <?php
}
