<?php
    namespace Workbloom\Component\Auth\v0\Controllers;

    use Workbloom\Helpers\DataSanitizer;
    use AxiumPHP\Helpers\RequestHelper;
    use Workbloom\Component\Auth\v0\Services\AuthService;

    class AuthController {
        public function login() {
            // Recebe formulário de login
            $form = RequestHelper::getFilteredInput(form_type: INPUT_POST);

            // Define campos obrigatórios
            $required_fields = [
                'login',
                'password'
            ];

            // Validação dos campos obrigatórios
            foreach ($required_fields as $field) {
                if (empty($form[$field])) {
                    // Resposta JSON de erro
                    RequestHelper::sendJsonResponse(
                        response_code: 400, 
                        status: 'error', 
                        message: "O campo '{$field}' é obrigatório", 
                        output: []
                    );
                    return;
                }
            }

            // Chama o serviço de autenticação
            $loginService = (new AuthService())->login(
                credentials: [
                    'login' => DataSanitizer::string(value: $form['login']),
                    'password' => DataSanitizer::string(value: $form['password'])
                ]
            );

            // Resposta JSON
            RequestHelper::sendJsonResponse(
                response_code: 200, 
                status: 'success', 
                message: 'Login successful', 
                output: [
                    'data' => $form
                ]
            );
        }
    }