<?php

namespace App\Modules\HR\Leave\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Leave\DTOs\LeaveTypeWriteData;
use App\Modules\HR\Leave\Models\LeaveType;
use App\Modules\HR\Leave\Requests\StoreLeaveTypeRequest;
use App\Modules\HR\Leave\Requests\UpdateLeaveTypeRequest;
use App\Modules\HR\Leave\Services\LeaveTypeService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class LeaveTypesPageController extends Controller
{
    public function __construct(
        private readonly LeaveTypeService $service,
    ) {}

    public function index(): View
    {
        $this->authorize('viewAny', LeaveType::class);

        return view('pages.hr.leave.types.index', [
            'types' => $this->service->list(),
        ]);
    }

    public function create(): View
    {
        $this->authorize('create', LeaveType::class);

        return view('pages.hr.leave.types.create');
    }

    public function store(StoreLeaveTypeRequest $request): RedirectResponse
    {
        $this->service->store(LeaveTypeWriteData::fromValidated($request->validated()));

        return redirect()
            ->route('hr.leave.types.index')
            ->with('status', __('leave.types.flash.created'));
    }

    public function edit(LeaveType $leaveType): View
    {
        $this->authorize('update', $leaveType);

        return view('pages.hr.leave.types.edit', ['type' => $leaveType]);
    }

    public function update(UpdateLeaveTypeRequest $request, LeaveType $leaveType): RedirectResponse
    {
        $this->service->update($leaveType, LeaveTypeWriteData::fromValidated($request->validated()));

        return redirect()
            ->route('hr.leave.types.index')
            ->with('status', __('leave.types.flash.updated'));
    }

    public function deactivate(LeaveType $leaveType): RedirectResponse
    {
        $this->authorize('deactivate', $leaveType);
        $this->service->deactivate($leaveType);

        return redirect()
            ->route('hr.leave.types.index')
            ->with('status', __('leave.types.flash.deactivated'));
    }
}
