<?php

namespace App\Modules\HR\Organization\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Organization\DTOs\JobGradeWriteData;
use App\Modules\HR\Organization\Models\JobGrade;
use App\Modules\HR\Organization\Requests\StoreJobGradeRequest;
use App\Modules\HR\Organization\Requests\UpdateJobGradeRequest;
use App\Modules\HR\Organization\Services\JobGradeService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class JobGradePageController extends Controller
{
    public function __construct(private readonly JobGradeService $service) {}

    public function index(): View
    {
        return view('pages.hr.organization.index', ['records' => $this->service->list(), 'entity' => 'job_grade', 'route' => 'job-grades']);
    }

    public function create(): View
    {
        return view('pages.hr.organization.create', array_merge(['entity' => 'job_grade', 'route' => 'job-grades'], []));
    }

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

        return redirect()->route('hr.job-grades.index')->with('status', __('organization.flash.created'));
    }

    public function edit(JobGrade $job_grade): View
    {
        return view('pages.hr.organization.edit', array_merge(['record' => $job_grade, 'entity' => 'job_grade', 'route' => 'job-grades'], []));
    }

    public function update(UpdateJobGradeRequest $request, JobGrade $job_grade): RedirectResponse
    {
        $this->service->update($job_grade, JobGradeWriteData::fromValidated($request->validated()));

        return redirect()->route('hr.job-grades.index')->with('status', __('organization.flash.updated'));
    }

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

        return back()->with('status', __('organization.flash.deactivated'));
    }
}
