<?php

namespace App\Http\Controllers\Ajustador;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class AjustadorAuthController extends Controller
{
    public function showLoginForm()
    {
        return Inertia::render('Ajustador/Login', [
            'title' => 'Iniciar Sesión - Ajustador',
        ]);
    }

    public function login(Request $request)
    {
        $credentials = $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if (Auth::guard('ajustador')->attempt($credentials, $request->boolean('remember'))) {
            $request->session()->regenerate();
            $request->session()->forget('url.intended');

            return redirect()->route('ajustador.dashboard');
        }

        return back()->withErrors([
            'email' => 'Las credenciales proporcionadas no coinciden con nuestros registros.',
        ])->onlyInput('email');
    }

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

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

        return redirect()->route('ajustador.login');
    }
}
