<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\ProductBomService;
use Illuminate\Http\Request;

class ProductBomItemController extends Controller
{
    public function __construct(private ProductBomService $service) {}

    // GET /products/{id}/bom/items
    public function index(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->index($id, $request->all());
        }, 'BOM 항목 목록');
    }

    // POST /products/{id}/bom/items/bulk
    public function bulkUpsert(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->bulkUpsert($id, $request->input('items', []));
        }, 'BOM 일괄 업서트');
    }

    // PATCH /products/{id}/bom/items/{item}
    public function update(int $id, int $item, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $item, $request) {
            return $this->service->update($id, $item, $request->all());
        }, 'BOM 항목 수정');
    }

    // DELETE /products/{id}/bom/items/{item}
    public function destroy(int $id, int $item)
    {
        return ApiResponse::handle(function () use ($id, $item) {
            $this->service->destroy($id, $item);

            return 'success';
        }, 'BOM 항목 삭제');
    }

    // POST /products/{id}/bom/items/reorder
    public function reorder(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            $this->service->reorder($id, $request->input('items', []));

            return 'success';
        }, 'BOM 정렬 변경');
    }

    // GET /products/{id}/bom/summary
    public function summary(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->summary($id);
        }, 'BOM 요약');
    }

    // GET /products/{id}/bom/validate
    public function validateBom(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->validateBom($id);
        }, 'BOM 유효성 검사');
    }

    /**
     * POST /api/v1/products/{id}/bom
     * BOM 구성 저장 (기존 전체 삭제 후 재등록)
     */
    public function replace(Request $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            // 서비스에서 트랜잭션 처리 + 예외는 글로벌 핸들러로
            return $this->service->replaceBom($id, $request->all());
        }, __('message.bom.creat'));
    }

    /** 특정 제품 BOM에서 사용 중인 카테고리 목록 */
    public function listCategories(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->listCategoriesForProduct($id);
        }, __('message.bom.fetch'));
    }

    /** 테넌트 전역 카테고리 추천(히스토리) */
    public function suggestCategories(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $q = $request->query('q');
            $limit = (int) ($request->query('limit', 20));

            return $this->service->listCategoriesForTenant($q, $limit);
        }, __('message.bom.fetch'));
    }

    /** Bom Tree */
    public function tree(Request $request, int $id)
    {
        return ApiResponse::handle(
            function () use ($request, $id) {
                return $this->service->tree($request, $id);
            }, __('message.bom.fetch')
        );
    }
}
