<?php

namespace App\Http\Controllers\Admin\Manage\Cutting;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\CuttingDraftCombinationRequest;
use App\Http\Requests\Admin\Manage\CuttingDraftMaterialRequest;
use App\Http\Requests\Admin\Manage\CuttingDraftResultRequest;
use App\Http\Requests\Admin\Manage\CuttingQuickCreateProductRequest;
use App\Models\CuttingResult;
use App\Models\RawMaterialPrice;
use App\Services\Manage\CuttingService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CuttingDraftItemController extends Controller
{
    public function __construct(
        private readonly CuttingService $cuttingService,
    ) {}

    public function storeMaterial(CuttingDraftMaterialRequest $request): JsonResponse
    {
        $item = $this->cuttingService->syncDraftMaterial($request->validated(), $request->user());

        return response()->json(['item' => $item]);
    }

    public function storeResult(CuttingDraftResultRequest $request): JsonResponse
    {
        $item = $this->cuttingService->syncDraftResult($request->validated(), $request->user());

        return response()->json(['item' => $item]);
    }

    public function destroyMaterial(Request $request, RawMaterialPrice $rawMaterialPrice): JsonResponse
    {
        $this->cuttingService->removeDraftMaterial($request->user(), $rawMaterialPrice);

        return response()->json(['ok' => true]);
    }

    public function destroyResult(Request $request, CuttingResult $cuttingResult): JsonResponse
    {
        $this->cuttingService->removeDraftResult($request->user(), $cuttingResult);

        return response()->json(['ok' => true]);
    }

    public function storeCombination(CuttingDraftCombinationRequest $request): JsonResponse
    {
        $items = $this->cuttingService->syncDraftCombination($request->validated(), $request->user());

        return response()->json(['items' => $items]);
    }

    public function destroyCombination(Request $request, int $combinationId): JsonResponse
    {
        $this->cuttingService->removeDraftCombination($request->user(), $combinationId);

        return response()->json(['ok' => true]);
    }

    public function updateCombinationResult(Request $request, int $combinationId): JsonResponse
    {
        $result = $request->input('material_result');

        $this->cuttingService->updateDraftCombinationResult($request->user(), $combinationId, $result);

        return response()->json(['ok' => true]);
    }

    public function quickCreateProduct(CuttingQuickCreateProductRequest $request): JsonResponse
    {
        $product = $this->cuttingService->quickCreateProduct($request->validated());

        return response()->json(['product' => $product]);
    }
}
