<?php 
include 'header.php';

// Redirect if already logged in
if (isset($_SESSION['user_id'])) {
    if ($_SESSION['role'] === 'admin') {
        header('Location: /admin/index.php');
    } elseif ($_SESSION['role'] === 'artist') {
        header('Location: /artist/index.php');
    } elseif ($_SESSION['role'] === 'customer') {
        header('Location: /customer/index.php');
    } else {
        header('Location: index.php');
    }
    exit();
}
?>

<div class="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
  <div class="max-w-md w-full bg-white p-8 rounded-xl shadow-lg space-y-6">
    <div class="text-center">
      <h2 class="text-3xl font-extrabold text-ucf-charcoal">Login to Your Account</h2>
      <p class="mt-2 text-sm text-gray-500">Welcome back! Please enter your credentials.</p>
    </div>

    <?php
    if (isset($_GET['status'])) {
        if ($_GET['status'] == 'reg_success') {
            echo '<div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 rounded-md"><p class="font-bold">Registration Successful!</p><p>You can now log in.</p></div>';
        } elseif ($_GET['status'] == 'reset_success') {
            echo '<div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 rounded-md"><p class="font-bold">Password Reset!</p><p>Your password has been updated successfully.</p></div>';
        }
    }

    if (isset($_GET['error'])) {
        $error = $_GET['error'];
        if ($error === 'empty') {
            echo '<div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded-md"><p class="font-bold">Missing Fields</p><p>Please fill in both username/email and password.</p></div>';
        } elseif ($error === 'invalid') {
            echo '<div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded-md"><p class="font-bold">Invalid Login</p><p>Incorrect username/email or password.</p></div>';
        } elseif ($error === 'not_verified') {
            echo '<div class="bg-yellow-100 border-l-4 border-yellow-500 text-yellow-800 p-4 rounded-md"><p class="font-bold">Account Not Verified</p><p>Please check your email for the verification link or OTP.</p></div>';
        }
    }
    ?>

    <form class="space-y-6" action="login_handler.php" method="POST">
      <div class="space-y-4">
        <div>
          <label for="email_or_username" class="block text-sm font-medium text-gray-700 mb-1">Username or Email</label>
          <input id="email_or_username" name="email_or_username" type="text" required
                 class="appearance-none rounded-md relative block w-full px-4 py-3 border border-gray-300 placeholder-gray-400 text-gray-900 focus:outline-none focus:ring-ucf-green focus:border-ucf-green sm:text-sm"
                 placeholder="Enter your username or email">
        </div>
        <div>
          <label for="password" class="block text-sm font-medium text-gray-700 mb-1">Password</label>
          <input id="password" name="password" type="password" required
                 class="appearance-none rounded-md relative block w-full px-4 py-3 border border-gray-300 placeholder-gray-400 text-gray-900 focus:outline-none focus:ring-ucf-green focus:border-ucf-green sm:text-sm"
                 placeholder="Enter your password">
        </div>
      </div>

      <div class="flex items-center justify-between">
        <div class="text-sm">
          <a href="forgot-password.php" class="font-medium text-ucf-green hover:text-ucf-green-dark">
            Forgot your password?
          </a>
        </div>
      </div>

      <div>
        <button type="submit"
                class="group relative w-full flex justify-center py-3 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-ucf-green hover:bg-ucf-green-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-ucf-green">
          Log In
        </button>
      </div>
    </form>

    <div class="text-center mt-4">
      <p class="text-gray-600 text-sm">
        Don’t have an account?
        <a href="register.php" class="font-semibold text-ucf-green hover:text-ucf-green-dark">
          Sign Up
        </a>
      </p>
    </div>
  </div>
</div>

<?php include 'footer.php'; ?>
