<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\PurchaseDraftItemRequest;
use App\Http\Requests\Admin\Manage\PurchaseNewVariantRequest;
use App\Models\RawMaterialPrice;
use App\Services\Manage\PurchaseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PurchaseDraftItemController extends Controller
{
    public function __construct(
        private readonly PurchaseService $purchaseService,
    ) {}

    public function store(PurchaseDraftItemRequest $request): JsonResponse
    {
        $item = $this->purchaseService->syncDraftItem($request->validated(), $request->user());

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

    public function storeNewVariant(PurchaseNewVariantRequest $request): JsonResponse
    {
        $price = $this->purchaseService->createVariantAndDraft($request->validated(), $request->user());

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

    public function destroy(Request $request, RawMaterialPrice $rawMaterialPrice): JsonResponse
    {
        $this->purchaseService->removeDraftItem($request->user(), $rawMaterialPrice);

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