<?php

namespace App\Domains\Staff\Controllers;

use App\Domains\Staff\Requests\StoreStaffRequest;
use App\Domains\Staff\Requests\UpdateStaffRequest;
use App\Domains\Staff\Resources\StaffResource;
use App\Domains\Staff\Services\StaffService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Symfony\Component\HttpFoundation\Response;

class AdminAppStaffController extends Controller
{
    public function __construct(private readonly StaffService $staffService) {}

    public function index(Request $request): AnonymousResourceCollection
    {
        return StaffResource::collection($this->staffService->list(
            $request->user()->tenant()->firstOrFail(),
            $request->string('search')->trim()->toString() ?: null,
        ));
    }

    public function store(StoreStaffRequest $request): StaffResource
    {
        return StaffResource::make($this->staffService->create(
            $request->user()->tenant()->firstOrFail(),
            $request->validated(),
        ));
    }

    public function update(UpdateStaffRequest $request, int $staff): StaffResource
    {
        return StaffResource::make($this->staffService->update(
            $request->user()->tenant()->firstOrFail(),
            $staff,
            $request->validated(),
        ));
    }

    public function destroy(Request $request, int $staff): Response
    {
        $this->staffService->delete($request->user()->tenant()->firstOrFail(), $staff);

        return response()->noContent();
    }
}
