<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreDesignationRequest;
use App\Http\Requests\UpdateDesignationRequest;
use App\Http\Requests\UpdateDesignationStatusRequest;
use App\Models\Designation;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class DesignationController extends Controller
{
    public function index(): Response
    {
        $designations = Designation::query()
            ->withCount(['permanentEmployees', 'temporaryEmployees'])
            ->orderByDesc('id')
            ->get()
            ->map(fn (Designation $designation): array => [
                'id' => $designation->id,
                'name' => $designation->name,
                'is_active' => $designation->is_active,
                'can_delete' => $designation->permanent_employees_count === 0
                    && $designation->temporary_employees_count === 0,
            ]);

        return Inertia::render('employees/designations/index', [
            'designations' => $designations,
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('employees/designations/create');
    }

    public function store(StoreDesignationRequest $request): RedirectResponse
    {
        Designation::query()->create([
            'name' => $request->validated('name'),
            'is_active' => true,
        ]);

        return redirect()
            ->route('employees.designations.index')
            ->with('status', __('Designation created.'));
    }

    public function edit(Designation $designation): Response
    {
        return Inertia::render('employees/designations/edit', [
            'designation' => [
                'id' => $designation->id,
                'name' => $designation->name,
                'is_active' => $designation->is_active,
            ],
        ]);
    }

    public function update(
        UpdateDesignationRequest $request,
        Designation $designation,
    ): RedirectResponse {
        $designation->fill([
            'name' => $request->validated('name'),
        ]);
        $designation->save();

        return redirect()
            ->route('employees.designations.index')
            ->with('status', __('Designation updated.'));
    }

    public function updateStatus(
        UpdateDesignationStatusRequest $request,
        Designation $designation,
    ): RedirectResponse {
        $designation->fill([
            'is_active' => $request->boolean('is_active'),
        ]);
        $designation->save();

        return redirect()
            ->route('employees.designations.index')
            ->with('status', __('Designation status updated.'));
    }

    public function destroy(Designation $designation): RedirectResponse
    {
        abort_unless(auth()->user()?->can('designations.delete'), 403);

        if (
            $designation->permanentEmployees()->exists()
            || $designation->temporaryEmployees()->exists()
        ) {
            return redirect()
                ->route('employees.designations.index')
                ->with('status', __('Cannot delete a designation that is assigned to staff.'));
        }

        $designation->delete();

        return redirect()
            ->route('employees.designations.index')
            ->with('status', __('Designation deleted.'));
    }
}
