<?php

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

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ItemMaster\IndependentBomItemStoreRequest;
use App\Http\Requests\ItemMaster\ItemBomItemStoreRequest;
use App\Http\Requests\ItemMaster\ItemBomItemUpdateRequest;
use App\Services\ItemMaster\ItemBomItemService;

class ItemBomItemController extends Controller
{
    public function __construct(
        protected ItemBomItemService $service,
    ) {}

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

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

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

    /**
     * BOM 항목 수정
     */
    public function update(int $id, ItemBomItemUpdateRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update($id, $request->validated());
        }, __('message.updated'));
    }

    /**
     * BOM 항목 삭제
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->destroy($id);

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