<?php
header('Content-Type: application/json');

$email = isset($_POST['email']) ? trim((string) $_POST['email']) : '';
$password = isset($_POST['password']) ? (string) $_POST['password'] : '';

if ($email === '' || $password === '') {
    http_response_code(400);
    echo json_encode([
        'success' => false,
        'message' => 'Email and password are required.',
    ]);
    exit;
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    http_response_code(422);
    echo json_encode([
        'success' => false,
        'message' => 'Please provide a valid email address.',
    ]);
    exit;
}

// Demo credentials:
// Email: admin@lms.com
// Password: admin123
if ($email === 'admin@lms.com' && $password === 'admin123') {
    echo json_encode([
        'success' => true,
        'message' => 'Welcome back! Login successful.',
    ]);
    exit;
}

http_response_code(401);
echo json_encode([
    'success' => false,
    'message' => 'Invalid credentials. Use admin@lms.com / admin123 for demo.',
]);
