<?php
/**
 * Enterprise HRM ERP System - Petty Cash Migration Runner
 * Run this script to create/update Petty Cash database tables
 */

require_once __DIR__ . '/../../includes/init.php';

// Check if user is admin
if (!hasAdminAccess()) {
    die('Access denied. Admin privileges required.');
}

$errors = [];
$success = [];

try {
    $db = getDB();
    $db->beginTransaction();
    
    // Read SQL file
    $sqlFile = __DIR__ . '/petty_cash_schema.sql';
    if (!file_exists($sqlFile)) {
        throw new Exception("SQL file not found: {$sqlFile}");
    }
    
    $sql = file_get_contents($sqlFile);
    
    // Remove comments and split by semicolon
    $sql = preg_replace('/--.*$/m', '', $sql);
    $statements = array_filter(array_map('trim', explode(';', $sql)));
    
    foreach ($statements as $statement) {
        if (empty($statement)) continue;
        
        try {
            $db->exec($statement);
            $success[] = "Executed: " . substr($statement, 0, 50) . "...";
        } catch (PDOException $e) {
            // Ignore "table already exists" errors
            if (strpos($e->getMessage(), 'already exists') === false) {
                $errors[] = "Error: " . $e->getMessage() . " (Statement: " . substr($statement, 0, 50) . "...)";
            }
        }
    }
    
    $db->commit();
    
} catch (Exception $e) {
    if (isset($db) && $db->inTransaction()) {
        $db->rollBack();
    }
    $errors[] = "Migration failed: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Petty Cash Migration</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 40px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            max-width: 800px;
            margin: 0 auto;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
        }
        .success {
            color: #28a745;
            padding: 10px;
            background: #d4edda;
            border: 1px solid #c3e6cb;
            border-radius: 4px;
            margin: 10px 0;
        }
        .error {
            color: #dc3545;
            padding: 10px;
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            border-radius: 4px;
            margin: 10px 0;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #4a90e2;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Petty Cash Database Migration</h1>
        
        <?php if (!empty($success)): ?>
        <div class="success">
            <strong>✓ Migration completed successfully!</strong>
            <ul>
                <?php foreach ($success as $msg): ?>
                <li><?php echo htmlspecialchars($msg); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($errors)): ?>
        <div class="error">
            <strong>⚠ Errors occurred:</strong>
            <ul>
                <?php foreach ($errors as $error): ?>
                <li><?php echo htmlspecialchars($error); ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
        
        <a href="<?php echo BASE_URL; ?>/modules/petty_cash/list.php" class="btn">Go to Petty Cash Module</a>
    </div>
</body>
</html>
