<?php

namespace App\Modules\HR\Organization\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Organization\DTOs\CostCenterWriteData;
use App\Modules\HR\Organization\Models\CostCenter;
use App\Modules\HR\Organization\Requests\StoreCostCenterRequest;
use App\Modules\HR\Organization\Requests\UpdateCostCenterRequest;
use App\Modules\HR\Organization\Services\CostCenterService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class CostCenterPageController extends Controller
{
    public function __construct(private readonly CostCenterService $service) {}

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

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

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

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

    public function edit(CostCenter $cost_center): View
    {
        return view('pages.hr.organization.edit', array_merge(['record' => $cost_center, 'entity' => 'cost_center', 'route' => 'cost-centers'], []));
    }

    public function update(UpdateCostCenterRequest $request, CostCenter $cost_center): RedirectResponse
    {
        $this->service->update($cost_center, CostCenterWriteData::fromValidated($request->validated()));

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

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

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