<?php
session_start();
require_once 'db_config.php';

// Check if user is admin
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
    echo '<div style="padding:20px; background:#fff3cd; border-radius:4px;"><strong>Error:</strong> Admin access required.</div>';
    exit;
}

if (!$conn) {
    die('Database connection failed');
}

// Check if blog_categories table exists
$result = $conn->query("SHOW TABLES LIKE 'blog_categories'");
if (!$result || $result->num_rows === 0) {
    echo '<div style="padding:20px; background:#f8d7da; border-radius:4px;"><strong>Error:</strong> blog_categories table does not exist. Run the migration first.</div>';
    exit;
}

// Update categories
$success = false;

// Delete existing categories
$conn->query("DELETE FROM blog_categories");

// Insert new categories
$stmt = $conn->prepare("INSERT INTO blog_categories (name, slug, description) VALUES (?, ?, ?)");
if ($stmt) {
    $categories = [
        ['Clothing', 'clothing', 'Clothing articles, reviews, and guides'],
        ['Fashion', 'fashion', 'Fashion trends, tips, and inspiration']
    ];
    
    foreach ($categories as $cat) {
        $name = $cat[0];
        $slug = $cat[1];
        $description = $cat[2];
        $stmt->bind_param('sss', $name, $slug, $description);
        $stmt->execute();
    }
    $stmt->close();
    $success = true;
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Update Blog Categories</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            padding: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
        }
        h1 {
            color: #333;
            margin-top: 0;
        }
        .back-link {
            display: inline-block;
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            text-decoration: none;
            margin-top: 20px;
        }
        .back-link:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Update Blog Categories</h1>
        <?php if ($success): ?>
            <div class="success">
                <strong>✓ Success!</strong><br>
                Blog categories have been updated to:<br><br>
                • Clothing<br>
                • Fashion
            </div>
        <?php endif; ?>
        <a href="admin_dashboard.php" class="back-link">← Back to Dashboard</a>
    </div>
</body>
</html>
