<?php

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

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\StockVerifyRequest;
use App\Models\Cutting;
use App\Services\Manage\StockService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class StockController extends Controller
{
    use FlashesEntityMessage;

    public function __construct(
        private readonly StockService $stockService,
    ) {}

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

        return Inertia::render('admin/manage/stocks/Index', [
            'pendingCuttings' => $this->stockService->getPendingVerificationCuttings($user),
            'pendingApprovalCuttings' => $this->stockService->getPendingApprovalCuttings($user),
        ]);
    }

    public function verify(StockVerifyRequest $request, Cutting $cutting): RedirectResponse
    {
        $this->stockService->submitVerification(
            $cutting,
            $request->user(),
            $request->validated('verification_note'),
            $request->validated('results'),
            $request->validated('result_prices'),
        );

        $this->flashSuccess('Verifikasi berhasil diajukan. Menunggu persetujuan owner.');

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