<?php

namespace App\Http\Controllers\Admin\Manage;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\RestockRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Restock;
use App\Services\Admin\Manage\RestockService;
use App\Services\Admin\Master\Product\ProductVariantService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class RestockController extends Controller
{
    public function __construct(
        private RestockService $service,
        private ProductVariantService $productVariantService,
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/manage/restock/index', [
            'restocks' => $this->service->paginated(
                ...$request->validatedWithDefaults(),
            ),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/manage/restock/create', [
            'products' => $this->productVariantService->getForRestock(),
        ]);
    }

    public function store(RestockRequest $request): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->store($request->validated()),
            'Restock berhasil ditambahkan.',
            'admin.manage.restocks.index',
            'admin.manage.restocks.create'
        );
    }

    public function edit(Restock $restock): Response
    {
        return Inertia::render('admin/manage/restock/edit', [
            'restock' => $this->service->getForEdit($restock),
            'products' => $this->productVariantService->getForRestock(),
        ]);
    }

    public function update(RestockRequest $request, Restock $restock): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->update($restock, $request->validated()),
            'Restock berhasil diperbarui.',
            'admin.manage.restocks.index',
            'admin.manage.restocks.edit',
            ['restock' => $restock]
        );
    }

    public function destroy(Restock $restock): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->destroy($restock),
            'Restock berhasil dihapus.',
            'admin.manage.restocks.index'
        );
    }
}
