<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ModelSet\CloneModelSetRequest;
use App\Http\Requests\ModelSet\CreateModelSetRequest;
use App\Http\Requests\ModelSet\UpdateModelSetRequest;
use App\Services\ModelSet\ModelSetService;
use Illuminate\Http\Request;

class ModelSetController extends Controller
{
    protected ModelSetService $modelSetService;

    public function __construct(ModelSetService $modelSetService)
    {
        $this->modelSetService = $modelSetService;
    }

    /**
     * 모델셋 목록 조회
     */
    public function index(Request $request)
    {
        $modelSets = $this->modelSetService->getModelSets($request->validated());

        return ApiResponse::success([
            'model_sets' => $modelSets,
        ], __('message.fetched'));
    }

    /**
     * 모델셋 상세 조회
     */
    public function show($id)
    {
        $modelSet = $this->modelSetService->getModelSetDetail($id);

        return ApiResponse::success([
            'model_set' => $modelSet,
        ], __('message.fetched'));
    }

    /**
     * 새로운 모델셋 생성
     */
    public function store(CreateModelSetRequest $request)
    {
        $modelSet = $this->modelSetService->createModelSet($request->validated());

        return ApiResponse::success([
            'model_set' => $modelSet,
        ], __('message.created'));
    }

    /**
     * 모델셋 수정
     */
    public function update(UpdateModelSetRequest $request, $id)
    {
        $modelSet = $this->modelSetService->updateModelSet($id, $request->validated());

        return ApiResponse::success([
            'model_set' => $modelSet,
        ], __('message.updated'));
    }

    /**
     * 모델셋 삭제
     */
    public function destroy($id)
    {
        $this->modelSetService->deleteModelSet($id);

        return ApiResponse::success([], __('message.deleted'));
    }

    /**
     * 모델셋 복제
     */
    public function clone(CloneModelSetRequest $request, $id)
    {
        $newModelSet = $this->modelSetService->cloneModelSet($id, $request->validated());

        return ApiResponse::success([
            'model_set' => $newModelSet,
        ], __('message.model_set.cloned'));
    }

    /**
     * 모델셋의 카테고리별 필드 구조 조회
     */
    public function getCategoryFields($id)
    {
        $fields = $this->modelSetService->getModelSetCategoryFields($id);

        return ApiResponse::success([
            'category_fields' => $fields,
        ], __('message.fetched'));
    }

    /**
     * 모델셋의 BOM 템플릿 목록
     */
    public function getBomTemplates($id)
    {
        $templates = $this->modelSetService->getModelSetBomTemplates($id);

        return ApiResponse::success([
            'bom_templates' => $templates,
        ], __('message.fetched'));
    }

    /**
     * 모델셋 기반 견적 파라미터 조회
     */
    public function getEstimateParameters($id, Request $request)
    {
        $parameters = $this->modelSetService->getEstimateParameters($id, $request->all());

        return ApiResponse::success([
            'parameters' => $parameters,
        ], __('message.fetched'));
    }

    /**
     * 모델셋 기반 BOM 계산
     */
    public function calculateBom($id, Request $request)
    {
        $result = $this->modelSetService->calculateModelSetBom($id, $request->all());

        return ApiResponse::success($result, __('message.calculated'));
    }
}
