<?php

namespace App\Services\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class AuthenticatedSessionService
{
    /**
     * @return array<string, string>
     */
    public function validateCredentials(Request $request): array
    {
        return $request->validate([
            'cedula' => ['required', 'string'],
            'password' => ['required', 'string'],
        ]);
    }

    /**
     * @param array<string, string> $credentials
     */
    public function attemptLogin(array $credentials, bool $remember): void
    {
        if (! Auth::attempt($credentials, $remember)) {
            throw ValidationException::withMessages([
                'cedula' => 'Las credenciales no son validas.',
            ]);
        }
    }

    public function logout(Request $request): void
    {
        Auth::guard('web')->logout();

        $request->session()->invalidate();
        $request->session()->regenerateToken();
    }
}
