<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\CategoryField\CategoryFieldStoreRequest;
use App\Http\Requests\CategoryField\CategoryFieldUpdateRequest;
use App\Services\CategoryFieldService;
use Illuminate\Http\Request;

class CategoryFieldController extends Controller
{
    public function __construct(private CategoryFieldService $service) {}

    // GET /categories/{id}/fields
    public function index(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->index($id, $request->all());
        }, __('message.category_field.fetched'));
    }

    // POST /categories/{id}/fields
    public function store(int $id, CategoryFieldStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->store($id, $request->validated());
        }, __('message.category_field.created'));
    }

    // GET /categories/fields/{field}
    public function show(int $field)
    {
        return ApiResponse::handle(function () use ($field) {
            return $this->service->show($field);
        }, __('message.category_field.fetched'));
    }

    // PATCH /categories/fields/{field}
    public function update(int $field, CategoryFieldUpdateRequest $request)
    {
        return ApiResponse::handle(function () use ($field, $request) {
            return $this->service->update($field, $request->validated());
        }, __('message.category_field.updated'));
    }

    // DELETE /categories/fields/{field}
    public function destroy(int $field)
    {
        return ApiResponse::handle(function () use ($field) {
            $this->service->destroy($field);

            return 'success';
        }, __('message.category_field.deleted'));
    }

    // POST /categories/{id}/fields/reorder
    public function reorder(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            $this->service->reorder($id, $request->input());

            return 'success';
        }, __('message.category_field.reordered'));
    }

    // PUT /categories/{id}/fields/bulk-upsert
    public function bulkUpsert(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->bulkUpsert($id, $request->input('items', []));
        }, __('message.category_field.bulk_upsert'));
    }
}
