<?php
/**
 * Database Setup Script
 * Run this ONCE to create the site_settings table
 */

include_once('../includes/config.php');

// Read and execute SQL file
$sql = file_get_contents(__DIR__ . '/sql/site_settings.sql');

// Split into individual statements
$statements = array_filter(array_map('trim', explode(';', $sql)));

$successCount = 0;
$errorCount = 0;
$errors = [];

foreach ($statements as $statement) {
    if (empty($statement))
        continue;

    if (mysqli_query($con, $statement)) {
        $successCount++;
    } else {
        $errorCount++;
        $errors[] = mysqli_error($con);
    }
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Setup Site Settings - Awesome Mall</title>
    <link href="../__admin__/css/admin-professional.css" rel="stylesheet" />
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
</head>

<body>
    <div class="container mt-5">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h3><i class="fas fa-database me-2"></i>Site Settings Database Setup</h3>
            </div>
            <div class="card-body">
                <?php if ($errorCount == 0): ?>
                    <div class="alert alert-success">
                        <i class="fas fa-check-circle me-2"></i>
                        <strong>Success!</strong> Site settings table created successfully.
                        <p class="mb-0 mt-2">✅ Executed
                            <?php echo $successCount; ?> SQL statement(s)
                        </p>
                    </div>
                <?php else: ?>
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle me-2"></i>
                        <strong>Completed with warnings</strong>
                        <p class="mb-0">✅ Success:
                            <?php echo $successCount; ?> | ⚠️ Errors:
                            <?php echo $errorCount; ?>
                        </p>
                        <?php if (!empty($errors)): ?>
                            <hr>
                            <strong>Errors:</strong>
                            <ul class="mb-0">
                                <?php foreach ($errors as $error): ?>
                                    <li>
                                        <?php echo htmlentities($error); ?>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>

                <div class="mt-4">
                    <h5>Default Settings Created:</h5>
                    <ul>
                        <li><strong>Store Name:</strong> Awesome Mall</li>
                        <li><strong>Currency:</strong> GHS (₵) - Ghana Cedis</li>
                        <li><strong>Support Email:</strong> support@awesomemall.com</li>
                        <li><strong>Support Phone:</strong> +233 123 456 789</li>
                        <li><strong>Tax Rate:</strong> 0%</li>
                        <li><strong>Shipping Fee:</strong> ₵0.00</li>
                    </ul>
                </div>

                <div class="mt-4">
                    <a href="../__admin__/settings.php" class="btn btn-primary">
                        <i class="fas fa-cog me-2"></i>Go to Settings Page
                    </a>
                    <a href="../__admin__/dashboard.php" class="btn btn-outline-primary">
                        <i class="fas fa-home me-2"></i>Go to Dashboard
                    </a>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
