<?php

namespace App\Http\Controllers\Admin\Manage;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\StokOpnameAutoSaveRequest;
use App\Http\Requests\Admin\Manage\StokOpnameRejectRequest;
use App\Http\Requests\Admin\Manage\StokOpnameRequest;
use App\Http\Requests\Admin\Manage\StokOpnameSubmitRequest;
use App\Http\Requests\Admin\Manage\StokOpnameVerifyRequest;
use App\Models\StokOpname;
use App\Services\Manage\StokOpnameService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class StokOpnameController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly StokOpnameService $stokOpnameService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);

        return Inertia::render('admin/manage/stok-opnames/Index', [
            'stokOpnames' => $this->stokOpnameService->paginateForIndex($tableQuery),
            'filters' => $this->dataTableFilters($tableQuery),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/manage/stok-opnames/Create', [
            'catalog' => $this->stokOpnameService->catalogItems(),
        ]);
    }

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

        $this->flashCreated('Stok Opname');

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

    public function edit(StokOpname $stokOpname): Response
    {
        return Inertia::render('admin/manage/stok-opnames/Edit', [
            'stokOpname' => $this->stokOpnameService->findForEdit($stokOpname),
            'catalog' => $this->stokOpnameService->catalogItems(),
        ]);
    }

    public function update(StokOpnameRequest $request, StokOpname $stokOpname): RedirectResponse
    {
        $this->stokOpnameService->update($stokOpname, $request->validated());

        $this->flashUpdated('Stok Opname');

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

    public function destroy(StokOpname $stokOpname): RedirectResponse
    {
        $this->stokOpnameService->delete($stokOpname);

        $this->flashDeleted('Stok Opname');

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

    public function autoSave(StokOpnameAutoSaveRequest $request): JsonResponse
    {
        $stokOpname = $this->stokOpnameService->autoSave($request->validated(), $request->user());

        return response()->json([
            'stok_opname_id' => $stokOpname->id,
            'message' => 'Draft tersimpan otomatis.',
        ]);
    }

    public function submit(StokOpnameSubmitRequest $request, StokOpname $stokOpname): RedirectResponse
    {
        $this->stokOpnameService->submit($stokOpname, $request->user());

        $this->flashSuccess('Stok opname berhasil diajukan untuk verifikasi.');

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

    public function verify(StokOpnameVerifyRequest $request, StokOpname $stokOpname): RedirectResponse
    {
        $this->stokOpnameService->verify(
            $stokOpname,
            $request->user(),
            $request->validated('verification_notes'),
        );

        $this->flashSuccess('Stok opname berhasil diverifikasi dan stok telah disesuaikan.');

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

    public function reject(StokOpnameRejectRequest $request, StokOpname $stokOpname): RedirectResponse
    {
        $this->stokOpnameService->reject(
            $stokOpname,
            $request->user(),
            $request->validated('reason'),
        );

        $this->flashSuccess('Stok opname berhasil ditolak.');

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