<?php
/**
 * Admin Password Update Script
 * Run this once to update the admin password, then delete this file for security
 */

session_start();
require_once __DIR__ . '/../config/database.php';

$message = '';
$success = false;

// Check if already logged in (optional security check)
// You can remove this check if you want to run it without being logged in
// if (!isset($_SESSION['admin_user_id'])) {
//     die('Access denied. Please log in first or remove this check.');
// }

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $newPassword = $_POST['password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';
    
    if (empty($email)) {
        $message = 'Email is required.';
    } elseif (empty($newPassword) || strlen($newPassword) < 6) {
        $message = 'Password must be at least 6 characters long.';
    } elseif ($newPassword !== $confirmPassword) {
        $message = 'Passwords do not match.';
    } else {
        try {
            $pdo = getDBConnection();
            
            // Check if user exists
            $stmt = $pdo->prepare("SELECT id FROM admin_users WHERE email = :email");
            $stmt->execute([':email' => $email]);
            $user = $stmt->fetch();
            
            if (!$user) {
                $message = 'User with that email not found.';
            } else {
                // Update password
                $passwordHash = password_hash($newPassword, PASSWORD_DEFAULT);
                $updateStmt = $pdo->prepare("UPDATE admin_users SET password_hash = :hash WHERE email = :email");
                $updateStmt->execute([
                    ':hash' => $passwordHash,
                    ':email' => $email
                ]);
                
                $success = true;
                $message = 'Password updated successfully! You can now log in with your new password.';
            }
        } catch (Exception $e) {
            $message = 'Error updating password: ' . $e->getMessage();
            error_log("Password update error: " . $e->getMessage());
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Update Admin Password</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #0A1828 0%, #1B4965 50%, #00B4D8 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .container {
            background: white;
            border-radius: 16px;
            padding: 40px;
            max-width: 500px;
            width: 100%;
            box-shadow: 0 20px 48px rgba(10, 24, 40, 0.2);
        }
        h1 {
            color: #0A1828;
            margin-bottom: 10px;
            font-size: 28px;
        }
        .subtitle {
            color: #64748B;
            margin-bottom: 30px;
            font-size: 14px;
        }
        .warning {
            background: #FEF3C7;
            border-left: 4px solid #F59E0B;
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 8px;
            font-size: 14px;
            color: #92400E;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #334155;
            font-weight: 500;
        }
        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #E0F2FE;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        input:focus {
            outline: none;
            border-color: #FF6B35;
            box-shadow: 0 0 0 3px rgba(255, 107, 53, 0.1);
        }
        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        .message.success {
            background: #D1FAE5;
            color: #065F46;
            border: 1px solid #10B981;
        }
        .message.error {
            background: #FEE2E2;
            color: #991B1B;
            border: 1px solid #EF4444;
        }
        button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #FF6B35 0%, #FF8C61 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 16px rgba(255, 107, 53, 0.3);
        }
        button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
        .info {
            margin-top: 20px;
            padding: 12px;
            background: #F0F9FF;
            border-radius: 8px;
            font-size: 13px;
            color: #64748B;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Update Admin Password</h1>
        <p class="subtitle">Change the password for an admin user</p>
        
        <div class="warning">
            <strong>⚠️ Security Note:</strong> Delete this file after updating your password for security reasons.
        </div>
        
        <?php if ($message): ?>
            <div class="message <?php echo $success ? 'success' : 'error'; ?>">
                <?php echo htmlspecialchars($message); ?>
            </div>
        <?php endif; ?>
        
        <?php if (!$success): ?>
        <form method="POST">
            <div class="form-group">
                <label for="email">Admin Email</label>
                <input 
                    type="email" 
                    id="email" 
                    name="email" 
                    value="admin@horizondrilling.com"
                    required 
                    autofocus
                >
            </div>
            <div class="form-group">
                <label for="password">New Password</label>
                <input 
                    type="password" 
                    id="password" 
                    name="password" 
                    required
                    minlength="6"
                    placeholder="At least 6 characters"
                >
            </div>
            <div class="form-group">
                <label for="confirm_password">Confirm New Password</label>
                <input 
                    type="password" 
                    id="confirm_password" 
                    name="confirm_password" 
                    required
                    minlength="6"
                    placeholder="Re-enter password"
                >
            </div>
            <button type="submit">Update Password</button>
        </form>
        <?php else: ?>
            <div class="info">
                <p><strong>✅ Password updated successfully!</strong></p>
                <p style="margin-top: 10px;">
                    <a href="login.php" style="color: #FF6B35; text-decoration: none; font-weight: 600;">
                        → Go to Login Page
                    </a>
                </p>
                <p style="margin-top: 15px; font-size: 12px; color: #EF4444;">
                    <strong>Remember to delete this file (update-password.php) for security!</strong>
                </p>
            </div>
        <?php endif; ?>
        
        <div class="info" style="margin-top: 20px;">
            <strong>Default admin email:</strong> admin@horizondrilling.com<br>
            <strong>Default password:</strong> admin123 (change this!)
        </div>
    </div>
</body>
</html>

