<?php
define('SECURE_ACCESS', true);
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';

try {
    $db = getDBConnection(true);
    
    // Check if admin user exists
    $stmt = $db->prepare("SELECT id FROM users WHERE role = 'admin'");
    $stmt->execute();
    $admin = $stmt->fetch();
    
    $admin_pass = password_hash('Prash@0281', PASSWORD_DEFAULT);
    $email = 'admin@reneneteducare.com';
    
    if ($admin) {
        $upd = $db->prepare("UPDATE users SET email = ?, password = ? WHERE id = ?");
        $upd->execute([$email, $admin_pass, $admin['id']]);
        echo "Admin credentials updated successfully in active database!\n";
    } else {
        // Create new admin
        $ins = $db->prepare("INSERT INTO users (username, email, password, role, status, employee_code) VALUES (?, ?, ?, 'admin', 'active', ?)");
        $ins->execute(['admin', $email, $admin_pass, 'EPPAD1234567890']);
        echo "Admin credentials created successfully in active database!\n";
    }
} catch (PDOException $e) {
    echo "Database error: " . $e->getMessage() . "\n";
}
