<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\RawMaterial\RawMaterialVariantRequest;
use App\Models\RawMaterial;
use App\Models\RawMaterialPrice;
use App\Services\Admin\Master\RawMaterial\RawMaterialVariantService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class RawMaterialVariantController extends Controller
{
    public function __construct(
        private RawMaterialVariantService $variantService,
    ) {}

    public function edit(RawMaterial $rawMaterial, RawMaterialPrice $variant): Response
    {
        return Inertia::render('admin/master/raw-material/variant/edit', [
            'variant' => $this->variantService->getForEdit($variant),
        ]);
    }

    public function update(RawMaterialVariantRequest $request, RawMaterial $rawMaterial, RawMaterialPrice $variant): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->variantService->update($variant, $request->validated()),
            'Varian berhasil diperbarui.',
            'admin.master.raw-materials.index',
            'admin.master.raw-materials.variants.edit',
            ['rawMaterial' => $rawMaterial, 'variant' => $variant]
        );
    }

    public function destroy(RawMaterial $rawMaterial, RawMaterialPrice $variant): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->variantService->destroy($rawMaterial, $variant),
            'Varian berhasil dihapus.',
            'admin.master.raw-materials.index'
        );
    }
}
