<?php

namespace App\Http\Controllers\Auth;

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

class DenuncianteLoginController extends Controller
{
    public function showLogin()
    {
        return view('auth.denunciante-login');
    }

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

        $credentials = [
            'username' => $request->username,
            'password' => $request->password,
        ];

        if (Auth::attempt($credentials, $request->boolean('remember'))) {
            $request->session()->regenerate();

            if (auth()->user()->debe_cambiar_password) {
                return redirect()->route('denunciante.password.cambiar');
            }

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

        return back()
            ->withInput()
            ->withErrors([
                'username' => 'Las credenciales ingresadas no son válidas.',
            ]);
    }

    public function logout(Request $request)
    {
        Auth::logout();

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

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