<?php
/**
 * Enterprise HRM ERP System - Direct Admin User Creation
 * This script creates an admin user directly in the database
 * DELETE THIS FILE AFTER USE FOR SECURITY
 */

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

// Default admin credentials
$adminUsername = 'admin';
$adminEmail = 'admin@hrm.local';
$adminPassword = 'Admin@123'; // Change this after first login!
$adminFullName = 'System Administrator';
$siteId = 1;
$roleId = 1; // Super Admin

try {
    $db = getDB();
    
    // Check if admin already exists
    $stmt = $db->prepare("SELECT COUNT(*) FROM users WHERE username = ? OR email = ?");
    $stmt->execute([$adminUsername, $adminEmail]);
    if ($stmt->fetchColumn() > 0) {
        die("Admin user already exists! Username: {$adminUsername} or Email: {$adminEmail}");
    }
    
    // Create admin user
    $passwordHash = hashPassword($adminPassword);
    
    $stmt = $db->prepare("
        INSERT INTO users (site_id, username, email, password_hash, full_name, role_id, user_status)
        VALUES (?, ?, ?, ?, ?, ?, 'active')
    ");
    $stmt->execute([$siteId, $adminUsername, $adminEmail, $passwordHash, $adminFullName, $roleId]);
    
    echo "<!DOCTYPE html>
<html>
<head>
    <title>Admin User Created</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 40px; background: #f5f5f5; }
        .box { background: white; padding: 30px; border-radius: 8px; max-width: 600px; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #10b981; }
        .credentials { background: #f0f9ff; padding: 20px; border-radius: 6px; margin: 20px 0; border-left: 4px solid #3b82f6; }
        .cred-item { margin: 10px 0; font-size: 16px; }
        .cred-label { font-weight: bold; color: #1e293b; }
        .cred-value { color: #64748b; font-family: monospace; }
        .warning { background: #fef3c7; padding: 15px; border-radius: 6px; margin: 20px 0; border-left: 4px solid #f59e0b; }
        .btn { display: inline-block; padding: 12px 24px; background: #1e293b; color: white; text-decoration: none; border-radius: 6px; margin-top: 20px; }
        .btn:hover { background: #1e40af; }
    </style>
</head>
<body>
    <div class='box'>
        <h1>✓ Admin User Created Successfully!</h1>
        
        <div class='credentials'>
            <h3>Login Credentials:</h3>
            <div class='cred-item'>
                <span class='cred-label'>Username:</span>
                <span class='cred-value'> {$adminUsername}</span>
            </div>
            <div class='cred-item'>
                <span class='cred-label'>Email:</span>
                <span class='cred-value'> {$adminEmail}</span>
            </div>
            <div class='cred-item'>
                <span class='cred-label'>Password:</span>
                <span class='cred-value'> {$adminPassword}</span>
            </div>
        </div>
        
        <div class='warning'>
            <strong>⚠️ IMPORTANT SECURITY NOTICE:</strong><br>
            1. Please change the password after first login<br>
            2. Delete this file (setup/create_admin_direct.php) immediately<br>
            3. Delete setup/create_admin.php if it exists
        </div>
        
        <a href='" . BASE_URL . "/login.php' class='btn'>Go to Login Page</a>
    </div>
</body>
</html>";
    
} catch (Exception $e) {
    die("Error creating admin user: " . $e->getMessage() . "<br><br>Please check your database connection in config/config.php");
}

