<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Models\Dealer;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class DealerLoginController extends Controller
{
    public function show(string $code): View
    {
        $dealer = Dealer::query()
            ->where('code', $code)
            ->where('is_active', true)
            ->firstOrFail();

        return view('auth.login-dealer', [
            'dealer' => $dealer,
        ]);
    }

    public function store(LoginRequest $request, string $code): RedirectResponse
    {
        $dealer = Dealer::query()
            ->where('code', $code)
            ->where('is_active', true)
            ->firstOrFail();

        // LoginRequest::authenticate will validate credentials and apply
        // dealer scoping using the route parameter.
        $request->authenticate();
        $request->session()->regenerate();

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

