<?php
/**
 * Setup Password Hash Generator
 * Run this file once to generate a password hash for your admin account
 * 
 * HOW TO USE:
 * 1. Upload this file to your server
 * 2. Visit it in your browser: https://yourdomain.com/api/setup_password.php
 * 3. Enter your desired password
 * 4. Copy the generated hash
 * 5. Update config.php with the hash
 * 6. DELETE this file for security
 */

$generated_hash = '';
$password = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
    $password = $_POST['password'];
    $generated_hash = password_hash($password, PASSWORD_BCRYPT);
    
    // Update database
    if (isset($_POST['update_db']) && $_POST['update_db'] === '1') {
        try {
            require_once 'config.php';
            require_once 'includes/database.php';
            
            $db = Database::getInstance()->getConnection();
            $stmt = $db->prepare("UPDATE admin_users SET password_hash = ? WHERE email = 'admin@epictravel.com'");
            $stmt->execute([$generated_hash]);
            
            $message = "✅ Password updated in database successfully!";
        } catch (Exception $e) {
            $error = "❌ Database update failed: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Password Setup - Epic Travel</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1 {
            color: #0891b2;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }
        input[type="password"],
        input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        button {
            background: #0891b2;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background: #0e7490;
        }
        .result {
            background: #f0f9ff;
            border: 1px solid #0891b2;
            padding: 15px;
            border-radius: 4px;
            margin-top: 20px;
            word-break: break-all;
        }
        .warning {
            background: #fef3c7;
            border: 1px solid #f59e0b;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
        }
        .success {
            background: #d1fae5;
            border: 1px solid #059669;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            color: #065f46;
        }
        .error {
            background: #fee2e2;
            border: 1px solid #dc2626;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            color: #991b1b;
        }
        .checkbox-group {
            margin-top: 10px;
        }
        code {
            background: #f3f4f6;
            padding: 2px 6px;
            border-radius: 3px;
            font-family: monospace;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔐 Admin Password Setup</h1>
        
        <div class="warning">
            <strong>⚠️ Security Warning:</strong> Delete this file after use!
        </div>
        
        <?php if (isset($message)): ?>
            <div class="success"><?php echo $message; ?></div>
        <?php endif; ?>
        
        <?php if (isset($error)): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <form method="POST">
            <div class="form-group">
                <label for="password">Enter Admin Password:</label>
                <input type="password" id="password" name="password" required
                       placeholder="Enter a strong password" value="<?php echo htmlspecialchars($password); ?>">
            </div>
            
            <div class="checkbox-group">
                <label>
                    <input type="checkbox" name="update_db" value="1">
                    Update password in database (requires config.php to be configured)
                </label>
            </div>
            
            <button type="submit">Generate Hash</button>
        </form>
        
        <?php if ($generated_hash): ?>
            <div class="result">
                <strong>Generated Password Hash:</strong><br>
                <code><?php echo $generated_hash; ?></code>
                
                <h3>Next Steps:</h3>
                <ol>
                    <li>Copy the hash above</li>
                    <li>Open <code>config.php</code></li>
                    <li>Find the line with <code>ADMIN_PASSWORD_HASH</code></li>
                    <li>Replace the placeholder with your hash</li>
                    <li><strong>DELETE this file immediately!</strong></li>
                </ol>
                
                <h3>Or run this SQL:</h3>
                <code style="display:block; padding:10px; margin-top:10px;">
                    UPDATE admin_users SET password_hash = '<?php echo $generated_hash; ?>' WHERE email = 'admin@epictravel.com';
                </code>
            </div>
        <?php endif; ?>
        
        <div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd; color: #666; font-size: 12px;">
            Epic Travel & Expeditions | admin@epictravel.com
        </div>
    </div>
</body>
</html>
