<?php
/**
 * Script para crear/resetear admin
 */

require_once __DIR__ . '/../config/bootstrap.php';

use App\Services\Database;
use App\Services\SecurityService;

$db = Database::getInstance();
$security = new SecurityService();

// Datos del admin
$email = 'admin@sistema.com';
$password = 'Admin123!';
$nombre = 'Administrador';

// Generar hash
$passwordHash = $security->hashPassword($password);

echo "====================================\n";
echo "CREAR/ACTUALIZAR ADMINISTRADOR\n";
echo "====================================\n\n";

try {
    // Verificar si existe
    $existingAdmin = $db->fetchOne(
        "SELECT id FROM admins WHERE email = :email",
        [':email' => $email]
    );

    if ($existingAdmin) {
        // Actualizar
        $db->update(
            'admins',
            [
                'password' => $passwordHash,
                'nombre' => $nombre,
                'activo' => 1
            ],
            'email = :email',
            [':email' => $email]
        );

        echo "✓ Admin actualizado exitosamente\n\n";
    } else {
        // Crear nuevo
        $db->insert('admins', [
            'email' => $email,
            'password' => $passwordHash,
            'nombre' => $nombre,
            'activo' => 1
        ]);

        echo "✓ Admin creado exitosamente\n\n";
    }

    echo "CREDENCIALES:\n";
    echo "Email: $email\n";
    echo "Password: $password\n\n";

    echo "Hash generado:\n";
    echo "$passwordHash\n\n";

    echo "Puedes iniciar sesión en:\n";
    echo "http://localhost/electronica/public/admin/login.php\n\n";

} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
    echo "\nAsegúrate de que:\n";
    echo "1. La base de datos 'contratos_db' existe\n";
    echo "2. Has importado el schema SQL\n";
    echo "3. Las credenciales en .env son correctas\n";
}
