<?php
// ONE-TIME LOGIN FIX — DELETE AFTER USE
require_once __DIR__ . '/includes/config.php';

$done = false;
$newPass = 'admin123';
$hash = password_hash($newPass, PASSWORD_DEFAULT);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newPass = trim($_POST['password'] ?? 'admin123');
    $hash = password_hash($newPass, PASSWORD_BCRYPT, ['cost'=>10]);
    
    $db = db();
    
    // Create table if missing
    $db->query("CREATE TABLE IF NOT EXISTS admin_users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) NOT NULL UNIQUE,
        password VARCHAR(255) NOT NULL,
        full_name VARCHAR(100),
        email VARCHAR(100),
        role ENUM('superadmin','editor') DEFAULT 'superadmin',
        last_login DATETIME,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");
    
    // Update or insert admin
    $st = $db->prepare("SELECT id FROM admin_users WHERE username='admin' LIMIT 1");
    $st->execute();
    $exists = $st->get_result()->fetch_assoc();
    
    if ($exists) {
        $upd = $db->prepare("UPDATE admin_users SET password=?, email='cyconsultancybd@gmail.com', role='superadmin' WHERE username='admin'");
        $upd->bind_param('s', $hash);
        $upd->execute();
    } else {
        $ins = $db->prepare("INSERT INTO admin_users (username,password,full_name,email,role) VALUES ('admin',?,'CY Admin','cyconsultancybd@gmail.com','superadmin')");
        $ins->bind_param('s', $hash);
        $ins->execute();
    }
    $done = true;
}
?>
<!DOCTYPE html>
<html>
<head><title>Fix Admin Login</title>
<style>
body{font-family:Arial,sans-serif;max-width:480px;margin:60px auto;padding:20px;background:#f3f4f6}
.card{background:#fff;border-radius:12px;padding:32px;box-shadow:0 4px 20px rgba(0,0,0,.1)}
h2{color:#1a56db;margin-bottom:20px}
label{display:block;font-size:13px;font-weight:600;margin-bottom:6px;color:#374151}
input{width:100%;padding:10px;border:1.5px solid #e5e7eb;border-radius:8px;font-size:14px;margin-bottom:16px;box-sizing:border-box}
button{width:100%;padding:12px;background:#1a56db;color:#fff;border:none;border-radius:8px;font-size:15px;font-weight:700;cursor:pointer}
.ok{background:#d1fae5;border:1px solid #6ee7b7;color:#065f46;border-radius:8px;padding:16px;margin-bottom:16px}
.warn{background:#fef3c7;border:1px solid #fcd34d;color:#92400e;border-radius:8px;padding:12px;font-size:12px;margin-top:16px}
code{background:#f3f4f6;padding:2px 8px;border-radius:4px;font-family:monospace}
</style>
</head>
<body>
<div class="card">
<h2>🔑 Fix Admin Login</h2>
<?php if ($done): ?>
  <div class="ok">
    ✅ <strong>Done!</strong> Password set successfully.<br>
    Username: <code>admin</code> | Password: <code><?=htmlspecialchars($_POST['password']??'admin123')?></code><br><br>
    <a href="login.php" style="color:#1a56db;font-weight:600">→ Go to Login Page</a>
  </div>
  <div class="warn">⚠️ <strong>Delete this file now!</strong><br>Path: <code>cy-php/admin/fix-login.php</code></div>
<?php else: ?>
  <p style="color:#6b7280;margin-bottom:20px;font-size:14px">This will reset the admin password directly using PHP's <code>password_hash()</code> on your server.</p>
  <form method="POST">
    <label>New Password</label>
    <input type="text" name="password" value="admin123" required>
    <button type="submit">Reset Password Now</button>
  </form>
  <div class="warn">⚠️ Delete this file after use!</div>
<?php endif; ?>
</div>
</body>
</html>
