<?php
require_once __DIR__ . '/../src/Database.php';
require_once __DIR__ . '/../src/Auth.php';

$auth = new Auth();
$loginError = '';
$registerSuccess = '';
$isRegisterMode = isset($_GET['register']);

// Handle login
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$isRegisterMode) {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username && $password) {
        if ($auth->login($username, $password)) {
            header('Location: /ussd/admin/dashboard.php');
            exit;
        } else {
            $loginError = 'Invalid username or password';
        }
    } else {
        $loginError = 'Please fill in all fields';
    }
}

// Handle registration
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $isRegisterMode) {
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';
    $fullName = trim($_POST['full_name'] ?? '');
    $registrationError = '';

    if (!$username || !$email || !$password || !$confirmPassword) {
        $registrationError = 'All fields are required';
    } elseif ($password !== $confirmPassword) {
        $registrationError = 'Passwords do not match';
    } elseif (strlen($password) < 6) {
        $registrationError = 'Password must be at least 6 characters';
    } elseif ($auth->userExists($username)) {
        $registrationError = 'Username already exists';
    } elseif ($auth->emailExists($email)) {
        $registrationError = 'Email already exists';
    } else {
        if ($auth->register($username, $email, $password, $fullName)) {
            $registerSuccess = 'Registration successful! You can now login.';
            $isRegisterMode = false;
        } else {
            $registrationError = 'Registration failed. Please try again.';
        }
    }
}

// Redirect if already logged in
if ($auth->isLoggedIn()) {
    header('Location: /ussd/admin/dashboard.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $isRegisterMode ? 'Register' : 'Login'; ?> - USSD System</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gradient-to-br from-blue-600 to-purple-700 min-h-screen flex items-center justify-center">
    <div class="w-full max-w-md">
        <!-- Login Form -->
        <?php if (!$isRegisterMode): ?>
        <div class="bg-white rounded-lg shadow-2xl p-8">
            <div class="text-center mb-8">
                <h1 class="text-3xl font-bold text-gray-800">USSD System</h1>
                <p class="text-gray-500 mt-2">Admin Portal</p>
            </div>

            <?php if ($loginError): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                <?php echo htmlspecialchars($loginError); ?>
            </div>
            <?php endif; ?>

            <?php if ($registerSuccess): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">
                <?php echo htmlspecialchars($registerSuccess); ?>
            </div>
            <?php endif; ?>

            <form method="POST" class="space-y-4">
                <div>
                    <label class="block text-gray-700 font-semibold mb-2">Username</label>
                    <input type="text" name="username" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Enter your username">
                </div>

                <div>
                    <label class="block text-gray-700 font-semibold mb-2">Password</label>
                    <input type="password" name="password" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Enter your password">
                </div>

                <button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 rounded-lg transition duration-200">
                    Login
                </button>
            </form>

            <div class="mt-6 text-center">
                <p class="text-gray-600">Don't have an account? 
                    <a href="?register=1" class="text-blue-600 hover:text-blue-800 font-semibold">Register here</a>
                </p>
            </div>
        </div>
        <?php else: ?>
        <!-- Registration Form -->
        <div class="bg-white rounded-lg shadow-2xl p-8">
            <div class="text-center mb-8">
                <h1 class="text-3xl font-bold text-gray-800">Create Account</h1>
                <p class="text-gray-500 mt-2">USSD System Registration</p>
            </div>

            <?php if (isset($registrationError)): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                <?php echo htmlspecialchars($registrationError); ?>
            </div>
            <?php endif; ?>

            <form method="POST" class="space-y-4">
                <div>
                    <label class="block text-gray-700 font-semibold mb-2">Full Name</label>
                    <input type="text" name="full_name" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Enter your full name">
                </div>

                <div>
                    <label class="block text-gray-700 font-semibold mb-2">Email</label>
                    <input type="email" name="email" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Enter your email">
                </div>

                <div>
                    <label class="block text-gray-700 font-semibold mb-2">Username</label>
                    <input type="text" name="username" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Choose a username">
                </div>

                <div>
                    <label class="block text-gray-700 font-semibold mb-2">Password</label>
                    <input type="password" name="password" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Enter a password">
                </div>

                <div>
                    <label class="block text-gray-700 font-semibold mb-2">Confirm Password</label>
                    <input type="password" name="confirm_password" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Confirm your password">
                </div>

                <button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 rounded-lg transition duration-200">
                    Create Account
                </button>
            </form>

            <div class="mt-6 text-center">
                <p class="text-gray-600">Already have an account? 
                    <a href="/login.php" class="text-blue-600 hover:text-blue-800 font-semibold">Login here</a>
                </p>
            </div>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>
