<?php
/**
 * Clean Blog Tables - Remove incomplete blog tables to allow fresh migration
 * Only run this if you're restarting the blog setup
 */

session_start();

// Check admin access
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
    http_response_code(403);
    die('Access Denied: Admin authentication required');
}

require_once 'db_config.php';

$tables_to_drop = ['blog_tag_relations', 'blogs', 'blog_tags', 'blog_categories'];

?>
<!DOCTYPE html>
<html>
<head>
    <title>Clean Blog Tables</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; background: #f5f5f5; }
        .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); }
        h1 { color: #333; }
        .status { padding: 15px; margin: 20px 0; border-radius: 4px; }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .warning { background: #fff3cd; color: #856404; border: 1px solid #ffeeba; }
        button { background: #dc3545; color: white; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; font-size: 16px; }
        button:hover { background: #c82333; }
        a { color: #007bff; text-decoration: none; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🧹 Clean Blog Tables</h1>
        
        <div class="status warning">
            <strong>⚠ WARNING:</strong> This will delete all blog-related tables.<br>
            Use this only if you need to restart the blog setup.
        </div>
        
        <?php
        
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm'])) {
            $dropped = [];
            $errors = [];
            
            foreach ($tables_to_drop as $table) {
                if ($conn->query("DROP TABLE IF EXISTS `$table`")) {
                    $dropped[] = $table;
                } else {
                    $errors[] = "Failed to drop $table: " . $conn->error;
                }
            }
            
            if (empty($errors)) {
                echo '<div class="status success">';
                echo '<strong>✓ Tables Cleaned!</strong><br>';
                echo 'Dropped tables: ' . implode(', ', $dropped) . '<br><br>';
                echo 'Now you can run the setup again:<br>';
                echo '<a href="run_blog_migration.php" style="display:inline-block;background:#007bff;color:white;padding:10px 20px;border-radius:4px;margin-top:10px;">Go to Blog Setup →</a>';
                echo '</div>';
            } else {
                echo '<div class="status error">';
                echo '<strong>Error:</strong><br>';
                foreach ($errors as $error) {
                    echo $error . '<br>';
                }
                echo '</div>';
            }
        } else {
            ?>
            
            <p>This will remove all blog tables to allow fresh setup:</p>
            
            <ul>
                <li>blog_categories</li>
                <li>blog_tags</li>
                <li>blogs</li>
                <li>blog_tag_relations</li>
            </ul>
            
            <form method="POST" onsubmit="return confirm('Are you sure? This cannot be undone.');">
                <button type="submit" name="confirm" value="1">🗑 Delete All Blog Tables</button>
            </form>
            
            <br><a href="run_blog_migration.php">← Back to Setup</a>
            
            <?php
        }
        
        ?>
    </div>
</body>
</html>
