<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../connection.php';
require_once __DIR__ . '/../controllers/AuthController.php';

// Initialize auth controller
$authController = new AuthController($conn);

// Get request method and input data
$method = $_SERVER['REQUEST_METHOD'];
$data = json_decode(file_get_contents('php://input'), true);

// Handle admin login API
if ($method === 'POST') {
    if (!empty($data['username']) && !empty($data['password']) && !empty($data['company_id'])) {
        $response = $authController->adminLogin(
            $data['username'],
            $data['password'],
            $data['company_id']
        );
        
        // Set appropriate HTTP status code based on response
        if ($response['status'] === 'error') {
            http_response_code(401); // Unauthorized
        }
        
        echo json_encode($response);
    } else {
        http_response_code(400); // Bad Request
        echo json_encode([
            'status' => 'error', 
            'message' => 'Username, password, and company ID are required'
        ]);
    }
} else {
    http_response_code(405); // Method Not Allowed
    echo json_encode([
        'status' => 'error', 
        'message' => 'Only POST method is allowed'
    ]);
}
?>
