<?php

namespace App\Http\Controllers\Admin\Master\Product;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\Product\ProductVariantRequest;
use App\Http\Requests\Admin\Master\Product\TransferStockRequest;
use App\Models\Product;
use App\Models\ProductVariant;
use App\Services\Admin\Master\Product\ProductVariantService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class ProductVariantController extends Controller
{
    public function __construct(
        private ProductVariantService $variantService,
    ) {}

    public function edit(Product $product, ProductVariant $variant): Response
    {
        return Inertia::render('admin/master/product/variant/edit', [
            'variant' => $this->variantService->getForEdit($variant),
        ]);
    }

    public function update(ProductVariantRequest $request, Product $product, ProductVariant $variant): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->variantService->update($variant, $request->validated()),
            'Varian berhasil diperbarui.',
            'admin.master.products.index',
            'admin.master.products.variants.edit',
            ['product' => $product, 'variant' => $variant]
        );
    }

    public function destroy(Product $product, ProductVariant $variant): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->variantService->destroy($product, $variant),
            'Varian berhasil dihapus.',
            'admin.master.products.index'
        );
    }

    public function transferStock(TransferStockRequest $request, Product $product, ProductVariant $variant): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->variantService->transferStock($variant, $request->validated()),
            'Transfer stok berhasil dilakukan.',
            'admin.master.products.index'
        );
    }
}
