<?php

namespace App\Modules\HR\Organization\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Organization\DTOs\BranchWriteData;
use App\Modules\HR\Organization\Models\Branch;
use App\Modules\HR\Organization\Requests\StoreBranchRequest;
use App\Modules\HR\Organization\Requests\UpdateBranchRequest;
use App\Modules\HR\Organization\Services\BranchService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class BranchPageController extends Controller
{
    public function __construct(private readonly BranchService $service) {}

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

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

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

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

    public function edit(Branch $branch): View
    {
        return view('pages.hr.organization.edit', array_merge(['record' => $branch, 'entity' => 'branch', 'route' => 'branches'], []));
    }

    public function update(UpdateBranchRequest $request, Branch $branch): RedirectResponse
    {
        $this->service->update($branch, BranchWriteData::fromValidated($request->validated()));

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

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

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