<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\CuttingDraftMaterialRequest;
use App\Http\Requests\Admin\Manage\CuttingDraftResultRequest;
use App\Models\ProductVariant;
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, ProductVariant $productVariant): JsonResponse
    {
        $this->cuttingService->removeDraftResult($request->user(), $productVariant);

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