<?php

namespace App\Modules\HR\Departments\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Departments\DTOs\DepartmentWriteData;
use App\Modules\HR\Departments\Models\Department;
use App\Modules\HR\Departments\Requests\StoreDepartmentRequest;
use App\Modules\HR\Departments\Requests\UpdateDepartmentRequest;
use App\Modules\HR\Departments\Services\DepartmentService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class DepartmentPageController extends Controller
{
    public function __construct(
        private readonly DepartmentService $service,
    ) {}

    public function index(Request $request): View
    {
        return view('pages.hr.departments.index', [
            'departments' => $this->service->paginateForIndex($request),
        ]);
    }

    public function create(): View
    {
        return view('pages.hr.departments.create', [
            'parentOptions' => $this->service->parentOptions(),
        ]);
    }

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

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

    public function edit(Department $department): View
    {
        return view('pages.hr.departments.edit', [
            'department' => $department,
            'parentOptions' => $this->service->parentOptions($department->id),
        ]);
    }

    public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse
    {
        $this->service->update($department, DepartmentWriteData::fromValidated($request->validated()));

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

    public function destroy(Department $department): RedirectResponse
    {
        $this->service->destroy($department);

        return redirect()
            ->route('hr.departments.index')
            ->with('status', __('departments.flash.deleted'));
    }
}
