<?php

namespace App\Http\Controllers\Admin\Master;

use App\Enums\RawMaterialUnit;
use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\RawMaterialRequest;
use App\Http\Requests\Admin\ToggleStatusRequest;
use App\Models\RawMaterial;
use App\Services\Master\RawMaterialService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class RawMaterialController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly RawMaterialService $rawMaterialService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);
        $isActive = $request->string('is_active')->toString();
        $stockStatus = $request->string('stock_status')->toString();

        return Inertia::render('admin/master/raw-materials/Index', [
            'rawMaterials' => $this->rawMaterialService->paginateForIndex($tableQuery, $isActive, $stockStatus),
            'filters' => $this->dataTableFilters($tableQuery, [
                'is_active' => $isActive,
                'stock_status' => $stockStatus,
            ]),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/master/raw-materials/Create', [
            'units' => RawMaterialUnit::selectOptions(),
        ]);
    }

    public function store(RawMaterialRequest $request): RedirectResponse
    {
        $this->rawMaterialService->create($request->validated(), $request->user());

        $this->flashSuccess('Bahan baku berhasil diajukan dan menunggu verifikasi owner.');

        return redirect()->route('admin.master.raw_materials.index');
    }

    public function edit(RawMaterial $rawMaterial): Response
    {
        return Inertia::render('admin/master/raw-materials/Edit', [
            'units' => RawMaterialUnit::selectOptions(),
            'rawMaterial' => $this->rawMaterialService->findForEdit($rawMaterial),
        ]);
    }

    public function update(RawMaterialRequest $request, RawMaterial $rawMaterial): RedirectResponse
    {
        $this->rawMaterialService->update($rawMaterial, $request->validated(), $request->user());

        $this->flashSuccess('Perubahan bahan baku berhasil diajukan dan menunggu verifikasi owner.');

        return redirect()->route('admin.master.raw_materials.index');
    }

    public function destroy(Request $request, RawMaterial $rawMaterial): RedirectResponse
    {
        $this->rawMaterialService->delete($rawMaterial, $request->user());

        $this->flashSuccess('Penghapusan bahan baku berhasil diajukan dan menunggu verifikasi owner.');

        return redirect()->route('admin.master.raw_materials.index');
    }

    public function toggleStatus(ToggleStatusRequest $request, RawMaterial $rawMaterial): RedirectResponse
    {
        $this->rawMaterialService->toggleStatus($rawMaterial, $request->validated(), $request->user());

        $this->flashSuccess('Perubahan status bahan baku berhasil diajukan dan menunggu verifikasi owner.');

        return back();
    }
}
