<?php

namespace Modules\Inventory\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Modules\Core\Helpers\ApiResponse;
use Modules\Core\Http\Requests\UpdateStatusRequest;
use Modules\Inventory\DTO\ProductUnitConversionFilter;
use Modules\Inventory\Http\Requests\Product\ProductUnitConversionRequest;
use Modules\Inventory\Http\Requests\Product\ReorderProductUnitConversionRequest;
use Modules\Inventory\Models\InventorySetting;
use Modules\Inventory\Models\Product;
use Modules\Inventory\Models\ProductUnitConversion;
use Modules\Inventory\Services\UnitConversionService;
use Modules\Inventory\Transformers\ProductUnitConversionResource;

class ProductUnitConversionController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        protected UnitConversionService $unitConversionService
    ){}

    /**
     * Store a newly created resource in storage.
     */
    public function store(ProductUnitConversionRequest $request, Product $product)
    {
        $this->authorize('create', Product::class);

        $conversion = $this->unitConversionService->store($product, $request->validated());

        return (new ProductUnitConversionResource($conversion))->additional([
            'status' => true,
            'message' => 'Product unit conversion created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show(Product $product, ProductUnitConversion $conversion)
    {
        $this->authorize('view', $product);

        $conversion = $this->unitConversionService->show($product, $conversion);

        return (new ProductUnitConversionResource($conversion))->additional([
            'status' => true,
            'message' => 'Product unit conversion retrieved successfully.'
        ]);
    }


    /**
     * Update the specified resource in storage.
     */

    public function update(ProductUnitConversionRequest $request, Product $product, ProductUnitConversion $conversion)
    {
        $this->authorize('update', $product);

        $conversion = $this->unitConversionService->update(
            $product,
            $conversion,
            $request->validated()
        );

        return (new ProductUnitConversionResource($conversion))->additional([
            'status' => true,
            'message' => 'Product unit conversion updated successfully.'
        ]);
    }

    public function reorder(ReorderProductUnitConversionRequest $request, Product $product)
    {
        $this->authorize('update', $product);

        $this->unitConversionService->reorder($request->validated()['ids']);

        return ApiResponse::successResponse('Product unit conversion order updated successfully.');
    }
}
