<?php
require_once __DIR__ . '/../Services/AuthService.php';

class AuthController {
    public static function login() {
        $input = json_decode(file_get_contents('php://input'), true);
        $username = $input['username'] ?? null;
        $password = $input['password'] ?? null;

        $result = AuthService::authenticate($username, $password);
        if ($result['success']) {
            http_response_code(200);
            echo json_encode([
                'success' => true,
                'token' => $result['token'],
                'role' => $result['role'],
                'message' => 'Login erfolgreich.'
            ]);
        } else {
            http_response_code(401);
            echo json_encode([
                'success' => false,
                'error' => [
                    'code' => 'AUTH_FAILED',
                    'message' => $result['message']
                ]
            ]);
        }
    }
}
