<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\PositionReorderRequest;
use App\Http\Requests\PositionRequest;
use App\Services\PositionService;

class PositionController extends Controller
{
    public function __construct(private PositionService $service) {}

    /**
     * GET /v1/positions
     */
    public function index(PositionRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->validated());
        }, __('message.fetched'));
    }

    /**
     * POST /v1/positions
     */
    public function store(PositionRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.created'));
    }

    /**
     * GET /v1/positions/{id}
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.fetched'));
    }

    /**
     * PUT /v1/positions/{id}
     */
    public function update(int $id, PositionRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update($id, $request->validated());
        }, __('message.updated'));
    }

    /**
     * DELETE /v1/positions/{id}
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroy($id);
        }, __('message.deleted'));
    }

    /**
     * PUT /v1/positions/reorder
     */
    public function reorder(PositionReorderRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->reorder($request->validated()['items']);
        }, __('message.reordered'));
    }
}
