<?php
require_once 'db.php';

$username = 'admin';
$password = 'imators2024';
$role = 'admin';

$hashed_password = password_hash($password, PASSWORD_DEFAULT);

try {
    $stmt = $pdo->prepare("INSERT INTO `imators-users` (username, password, role) VALUES (?, ?, ?)");
    $result = $stmt->execute([$username, $hashed_password, $role]);
    
    if ($result) {
        echo "User created successfully!\n";
        echo "Username: $username\n";
        echo "Password: $password\n";
        echo "Role: $role\n";
        echo "Generated SQL: INSERT INTO `imators-users` (username, password, role) VALUES ('$username', '$hashed_password', '$role');\n";
    } else {
        echo "Failed to create user\n";
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage() . "\n";
    
    if ($e->getCode() == 23000) {
        echo "User already exists. Updating password instead.\n";
        
        try {
            $stmt = $pdo->prepare("UPDATE `imators-users` SET password = ? WHERE username = ?");
            $result = $stmt->execute([$hashed_password, $username]);
            
            if ($result) {
                echo "Password updated successfully!\n";
                echo "Username: $username\n";
                echo "New password: $password\n";
            } else {
                echo "Failed to update password\n";
            }
        } catch (PDOException $e2) {
            echo "Error during update: " . $e2->getMessage() . "\n";
        }
    }
}