<?php

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

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ItemMaster\IndependentSectionStoreRequest;
use App\Http\Requests\ItemMaster\ItemSectionStoreRequest;
use App\Http\Requests\ItemMaster\ItemSectionUpdateRequest;
use App\Http\Requests\ItemMaster\ReorderRequest;
use App\Services\ItemMaster\ItemSectionService;
use Illuminate\Http\Request;

class ItemSectionController extends Controller
{
    public function __construct(private ItemSectionService $service) {}

    /**
     * 독립 섹션 목록 조회
     *
     * GET /api/v1/item-master/sections
     */
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $isTemplate = $request->has('is_template')
                ? filter_var($request->query('is_template'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
                : null;

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

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

    /**
     * 섹션 복제
     *
     * POST /api/v1/item-master/sections/{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/sections/{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/pages/{pageId}/sections
     */
    public function store(int $pageId, ItemSectionStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($pageId, $request) {
            return $this->service->store($pageId, $request->validated());
        }, __('message.created'));
    }

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

    /**
     * 섹션 삭제 (Soft Delete)
     *
     * DELETE /api/v1/item-master/sections/{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/pages/{pageId}/sections/reorder
     */
    public function reorder(int $pageId, ReorderRequest $request)
    {
        return ApiResponse::handle(function () use ($pageId, $request) {
            $this->service->reorder($pageId, $request->validated()['items']);

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