<?php

namespace App\Modules\HR\Organization\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Organization\DTOs\WorkLocationWriteData;
use App\Modules\HR\Organization\Models\WorkLocation;
use App\Modules\HR\Organization\Repositories\BranchRepository;
use App\Modules\HR\Organization\Requests\StoreWorkLocationRequest;
use App\Modules\HR\Organization\Requests\UpdateWorkLocationRequest;
use App\Modules\HR\Organization\Services\WorkLocationService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class WorkLocationPageController extends Controller
{
    public function __construct(private readonly WorkLocationService $service, private readonly BranchRepository $branches) {}

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

    public function create(): View
    {
        return view('pages.hr.organization.create', array_merge(['entity' => 'work_location', 'route' => 'work-locations'], ['branchOptions' => $this->branches->listForSelect()]));
    }

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

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

    public function edit(WorkLocation $work_location): View
    {
        return view('pages.hr.organization.edit', array_merge(['record' => $work_location, 'entity' => 'work_location', 'route' => 'work-locations'], ['branchOptions' => $this->branches->listForSelect()]));
    }

    public function update(UpdateWorkLocationRequest $request, WorkLocation $work_location): RedirectResponse
    {
        $this->service->update($work_location, WorkLocationWriteData::fromValidated($request->validated()));

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

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

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