<?php
/**
 * Authentication API Endpoints
 */

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../classes/Database.php';
require_once __DIR__ . '/../classes/Auth.php';
require_once __DIR__ . '/middleware.php';

// Handle preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

$method = $_SERVER['REQUEST_METHOD'];
$auth = new Auth();

switch ($method) {
    case 'POST':
        $data = getJsonBody();
        validateRequired($data, ['email_or_phone', 'password']);
        
        try {
            $result = $auth->login($data['email_or_phone'], $data['password']);
            sendResponse($result);
        } catch (Exception $e) {
            sendError($e->getMessage(), 401);
        }
        break;
        
    default:
        sendError('Method not allowed', 405);
        break;
}
?>
