<?php

namespace App\Http\Controllers\Admin\Manage\Restock;

use App\Enums\Permission;
use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\RestockRequest;
use App\Models\Restock;
use App\Services\Manage\RestockService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class RestockController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly RestockService $restockService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);
        $tableQuery['search_id'] = $request->string('search_id')->trim()->toString();

        return Inertia::render('admin/manage/restocks/Index', [
            'restocks' => $this->restockService->paginateForIndex($tableQuery),
            'filters' => $this->dataTableFilters($tableQuery, [
                'search_id' => $tableQuery['search_id'],
            ]),
        ]);
    }

    public function create(Request $request): Response
    {
        $user = $request->user();

        $this->restockService->clearDraftItemsForUser($user);

        return Inertia::render('admin/manage/restocks/Create', [
            'productCatalog' => $this->restockService->productCatalog(user: $user),
            'draftItems' => [],
        ]);
    }

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

        if ($request->user()->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            $this->flashCreated('Restock');
        } else {
            $this->flashSuccess('Restock berhasil diajukan dan menunggu verifikasi owner.');
        }

        return redirect()->route('admin.manage.restocks.index');
    }

    public function edit(Restock $restock): Response
    {
        return Inertia::render('admin/manage/restocks/Edit', [
            'restock' => $this->restockService->findForEdit($restock),
            'productCatalog' => $this->restockService->productCatalog($restock),
        ]);
    }

    public function update(RestockRequest $request, Restock $restock): RedirectResponse
    {
        $this->restockService->update($restock, $request->validated(), $request->user());

        if ($request->user()->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            $this->flashUpdated('Restock');
        } else {
            $this->flashSuccess('Perubahan restock berhasil diajukan dan menunggu verifikasi owner.');
        }

        return redirect()->route('admin.manage.restocks.index');
    }

    public function destroy(Request $request, Restock $restock): RedirectResponse
    {
        $this->restockService->delete($restock, $request->user());

        if ($request->user()->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
            $this->flashDeleted('Restock');
        } else {
            $this->flashSuccess('Penghapusan restock berhasil diajukan dan menunggu verifikasi owner.');
        }

        return redirect()->route('admin.manage.restocks.index');
    }
}
