<?php

namespace App\Http\Controllers\Admin\Manage;

use App\Enums\CuttingStatus;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\CuttingRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Cutting;
use App\Services\Admin\Manage\CuttingService;
use App\Services\Admin\Master\RawMaterial\RawMaterialVariantService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class CuttingController extends Controller
{
    public function __construct(
        private CuttingService $service,
        private RawMaterialVariantService $rawMaterialVariantService,
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/manage/cutting/index', [
            'cuttings' => $this->service->paginated(
                ...$request->validatedWithDefaults(),
                filters: $request->only(['product_name', 'status']),
            ),
            'filters' => $request->only(['product_name', 'status']),
            'filterOptions' => [
                'statusOptions' => CuttingStatus::toSelect(),
                'productNames' => $this->service->getProductNames(),
            ],
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/manage/cutting/create', [
            'rawMaterials' => $this->rawMaterialVariantService->getForCutting(),
        ]);
    }

    public function show(Cutting $cutting): Response
    {
        return Inertia::render('admin/manage/cutting/show', [
            'cutting' => $this->service->getForShow($cutting),
        ]);
    }

    public function share(Cutting $cutting): Response
    {
        return Inertia::render('admin/manage/cutting/show', [
            'cutting' => $this->service->getForShow($cutting),
        ]);
    }

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

    public function edit(Cutting $cutting): Response
    {
        return Inertia::render('admin/manage/cutting/edit', [
            'cutting' => $this->service->getForEdit($cutting),
            'rawMaterials' => $this->rawMaterialVariantService->getForCutting(),
        ]);
    }

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

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