<?php

namespace App\Http\Controllers\INSURANCE;

use Illuminate\Http\Request;
use App\Models\INSURANCE\Insurer;
use Illuminate\Contracts\View\View;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\INSURANCE\StoreInsurerRequest;
use App\Http\Requests\INSURANCE\UpdateInsurerRequest;

class InsurerController extends Controller
{
    public function index(): View
    {
        $insurers = Insurer::orderBy('short_name', 'asc')->get();
        return view('backend.insurance.insurers.index', compact('insurers'));
    }

    public function create(): View
    {
        $readonly = null;
        return view('backend.insurance.insurers.dashboard', [
            'insurer' => null,
            'readonly' => $readonly
        ]);
    }

    public function store(StoreInsurerRequest $request): RedirectResponse
    {
        $data = $request->validated();
        $insurer = Insurer::create($data);

        return redirect()->route('osiguranje.insurers.edit', $insurer)
            ->with('status', 'Success');
    }


    public function edit(Insurer $insurer): View
    {
        $insurer->load(['commissionDefaults']);
        $readonly = null;
        return view('backend.insurance.insurers.dashboard', compact('insurer', 'readonly'));
    }

    public function update(UpdateInsurerRequest $request, Insurer $insurer): RedirectResponse
    {
        $data = $request->validated();
        $insurer->update($data);
        return redirect()->route('osiguranje.insurers.edit', $insurer)
            ->with('status', 'Success');
    }


    public function show(Insurer $insurer)
    {
        return view('backend.insurance.insurers.show', compact('insurer'));
    }
}
