<?php

namespace App\Modules\HR\Organization\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Departments\Repositories\DepartmentRepository;
use App\Modules\HR\Organization\DTOs\SectionWriteData;
use App\Modules\HR\Organization\Models\Section;
use App\Modules\HR\Organization\Requests\StoreSectionRequest;
use App\Modules\HR\Organization\Requests\UpdateSectionRequest;
use App\Modules\HR\Organization\Services\SectionService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class SectionPageController extends Controller
{
    public function __construct(private readonly SectionService $service, private readonly DepartmentRepository $departments) {}

    public function index(\Illuminate\Http\Request $request): View
    {
        return view('pages.hr.organization.index', ['records' => $this->service->paginateForIndex($request), 'entity' => 'section', 'route' => 'sections']);
    }

    public function create(): View
    {
        return view('pages.hr.organization.create', array_merge(['entity' => 'section', 'route' => 'sections'], ['departmentOptions' => $this->departments->listForSelect()]));
    }

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

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

    public function edit(Section $section): View
    {
        return view('pages.hr.organization.edit', array_merge(['record' => $section, 'entity' => 'section', 'route' => 'sections'], ['departmentOptions' => $this->departments->listForSelect()]));
    }

    public function update(UpdateSectionRequest $request, Section $section): RedirectResponse
    {
        $this->service->update($section, SectionWriteData::fromValidated($request->validated()));

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

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

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