<?php

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

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ItemMaster\ItemPageStoreRequest;
use App\Http\Requests\ItemMaster\ItemPageUpdateRequest;
use App\Services\ItemMaster\ItemPageService;
use Illuminate\Http\Request;

class ItemPageController extends Controller
{
    public function __construct(private ItemPageService $service) {}

    /**
     * 페이지 목록 조회 (섹션/필드 중첩 포함)
     *
     * GET /api/v1/item-master/pages?item_type=FG
     */
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $itemType = $request->input('item_type');

            return $this->service->index($itemType);
        }, __('message.fetched'));
    }

    /**
     * 페이지 생성
     *
     * POST /api/v1/item-master/pages
     */
    public function store(ItemPageStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.created'));
    }

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

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

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