<?php

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

use App\Enums\ProductStockQuality;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\OrderDraftItemRequest;
use App\Http\Requests\Admin\Manage\OrderDraftResyncPricesRequest;
use App\Models\ProductVariant;
use App\Services\Manage\OrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OrderDraftItemController extends Controller
{
    public function __construct(
        private readonly OrderService $orderService,
    ) {}

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

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

    public function destroy(Request $request, ProductVariant $productVariant): JsonResponse
    {
        $stockQuality = ProductStockQuality::from(
            $request->query('stock_quality', ProductStockQuality::GOOD->value),
        );

        $this->orderService->removeDraftItem($request->user(), $productVariant, $stockQuality);

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

    public function resyncPrices(OrderDraftResyncPricesRequest $request): JsonResponse
    {
        $items = $this->orderService->resyncDraftPrices(
            $request->user(),
            $request->validated('price_type'),
        );

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