<?php

namespace App\Http\Controllers\Api\V1\ItemMaster;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ItemMaster\IndependentFieldStoreRequest;
use App\Http\Requests\ItemMaster\ItemFieldStoreRequest;
use App\Http\Requests\ItemMaster\ItemFieldUpdateRequest;
use App\Http\Requests\ItemMaster\ReorderRequest;
use App\Services\ItemMaster\ItemFieldService;

class ItemFieldController extends Controller
{
    public function __construct(private ItemFieldService $service) {}

    /**
     * 독립 필드 목록 조회
     *
     * GET /api/v1/item-master/fields
     */
    public function index()
    {
        return ApiResponse::handle(function () {
            return $this->service->index();
        }, __('message.fetched'));
    }

    /**
     * 독립 필드 생성 (섹션 연결 없음)
     *
     * POST /api/v1/item-master/fields
     */
    public function storeIndependent(IndependentFieldStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->storeIndependent($request->validated());
        }, __('message.created'));
    }

    /**
     * 필드 복제
     *
     * POST /api/v1/item-master/fields/{id}/clone
     */
    public function clone(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->clone($id);
        }, __('message.created'));
    }

    /**
     * 필드 사용처 조회
     *
     * GET /api/v1/item-master/fields/{id}/usage
     */
    public function getUsage(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->getUsage($id);
        }, __('message.fetched'));
    }

    /**
     * 필드 생성 (섹션에 연결)
     *
     * POST /api/v1/item-master/sections/{sectionId}/fields
     */
    public function store(int $sectionId, ItemFieldStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($sectionId, $request) {
            return $this->service->store($sectionId, $request->validated());
        }, __('message.created'));
    }

    /**
     * 필드 수정
     *
     * PUT /api/v1/item-master/fields/{id}
     */
    public function update(int $id, ItemFieldUpdateRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update($id, $request->validated());
        }, __('message.updated'));
    }

    /**
     * 필드 삭제 (Soft Delete)
     *
     * DELETE /api/v1/item-master/fields/{id}
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->destroy($id);

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

    /**
     * 필드 순서 변경
     *
     * PUT /api/v1/item-master/sections/{sectionId}/fields/reorder
     */
    public function reorder(int $sectionId, ReorderRequest $request)
    {
        return ApiResponse::handle(function () use ($sectionId, $request) {
            $this->service->reorder($sectionId, $request->validated()['items']);

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