<?php

namespace App\Http\Controllers\Admin\Manage;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\StokOpnameRequest;
use App\Http\Requests\Admin\Manage\StokOpnameVerifyRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\StokOpname;
use App\Services\Admin\Manage\StokOpnameService;
use App\Services\Admin\Master\Product\ProductVariantService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

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

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

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

    public function store(StokOpnameRequest $request): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->store($request->validated()),
            'Stok opname berhasil ditambahkan.',
            'admin.manage.stok-opnames.index',
            'admin.manage.stok-opnames.create'
        );
    }

    public function edit(StokOpname $stokOpname): Response
    {
        return Inertia::render('admin/manage/stok-opname/edit', [
            'stokOpname' => $this->service->getForEdit($stokOpname),
            'products' => $this->productVariantService->getForStokOpname(),
        ]);
    }

    public function update(StokOpnameRequest $request, StokOpname $stokOpname): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->update($stokOpname, $request->validated()),
            'Stok opname berhasil diperbarui.',
            'admin.manage.stok-opnames.index',
            'admin.manage.stok-opnames.edit',
            ['stokOpname' => $stokOpname]
        );
    }

    public function destroy(StokOpname $stokOpname): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->destroy($stokOpname),
            'Stok opname berhasil dihapus.',
            'admin.manage.stok-opnames.index'
        );
    }

    public function submit(StokOpname $stokOpname): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->submit($stokOpname),
            'Stok opname berhasil disubmit.',
            'admin.manage.stok-opnames.index'
        );
    }

    public function verify(StokOpnameVerifyRequest $request, StokOpname $stokOpname): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->verify($stokOpname, $request->validated()),
            'Stok opname berhasil diverifikasi.',
            'admin.manage.stok-opnames.index'
        );
    }

    public function reject(StokOpname $stokOpname): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->reject($stokOpname),
            'Stok opname ditolak.',
            'admin.manage.stok-opnames.index'
        );
    }

    public function cancel(StokOpname $stokOpname): RedirectResponse
    {
        return $this->handleAction(
            fn () => $this->service->cancel($stokOpname),
            'Stok opname berhasil dibatalkan.',
            'admin.manage.stok-opnames.index'
        );
    }

    public function items(StokOpname $stokOpname): JsonResponse
    {
        return response()->json([
            'items' => $this->service->getItems($stokOpname),
        ]);
    }
}
