<?php
require_once __DIR__ . '/app/autoload.php';

use App\Core\Database;

$db = Database::getInstance()->getConnection();

echo "Checking for admin user...\n";

$stmt = $db->prepare("SELECT * FROM users WHERE username = 'admin'");
$stmt->execute();
$user = $stmt->fetch();

if (!$user) {
    echo "Creating default admin user...\n";
    $pass = password_hash('123456', PASSWORD_DEFAULT);
    // Role admin, permissions empty (admin has all access by check logic)
    $stmt = $db->prepare("INSERT INTO users (name, username, password, role, permissions) VALUES (?, ?, ?, ?, ?)");
    $stmt->execute(['Administrador', 'admin', $pass, 'admin', json_encode([])]);
    echo "Admin user created successfully.\n";
} else {
    echo "Admin user already exists.\n";
    // Reset password just in case
    $pass = password_hash('123456', PASSWORD_DEFAULT);
    $db->prepare("UPDATE users SET password = ? WHERE username = 'admin'")->execute([$pass]);
    echo "Admin password reset to '123456'.\n";
}
