<?php
require_once '../admin/admin-config.php';

// Create admin table if it doesn't exist
$create_admin_table = "CREATE TABLE IF NOT EXISTS `admin` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `admin_name` varchar(50) NOT NULL UNIQUE,
  `admin_password` varchar(255) NOT NULL,
  `admin_email` varchar(100) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `last_login` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";

if (mysqli_query($link, $create_admin_table)) {
    echo "✅ Admin table created successfully\n";
} else {
    echo "❌ Error creating admin table: " . mysqli_error($link) . "\n";
}

// Check if admin exists
$check_admin = "SELECT COUNT(*) as count FROM admin WHERE admin_name = 'admin'";
$result = mysqli_query($link, $check_admin);
$row = mysqli_fetch_assoc($result);

if ($row['count'] == 0) {
    // Create default admin user
    $admin_name = 'admin';
    $admin_password = password_hash('admin123', PASSWORD_DEFAULT);
    
    $insert_admin = "INSERT INTO admin (admin_name, admin_password, admin_email) VALUES (?, ?, ?)";
    $stmt = mysqli_prepare($link, $insert_admin);
    
    if ($stmt) {
        $admin_email = 'admin@shubank.co';
        mysqli_stmt_bind_param($stmt, 'sss', $admin_name, $admin_password, $admin_email);
        
        if (mysqli_stmt_execute($stmt)) {
            echo "✅ Default admin user created successfully\n";
            echo "📧 Username: admin\n";
            echo "🔑 Password: admin123\n";
        } else {
            echo "❌ Error creating admin user: " . mysqli_error($link) . "\n";
        }
        
        mysqli_stmt_close($stmt);
    }
} else {
    echo "✅ Admin user already exists\n";
}

echo "\n🎉 Admin setup complete!\n";
echo "🌐 Access admin panel at: http://localhost/A+/01/admin/admin-login.php\n";
?>