<?php

namespace App\Http\Controllers\Admin\Manage;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\ApproveVerificationRequest;
use App\Http\Requests\Admin\Manage\RejectVerificationRequest;
use App\Models\Cutting;
use App\Models\OwnerVerificationRequest;
use App\Services\Manage\OwnerVerificationService;
use App\Services\Manage\StockService;
use Illuminate\Http\RedirectResponse;

class OwnerVerificationController extends Controller
{
    use FlashesEntityMessage;

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

    public function approveCutting(ApproveVerificationRequest $request, Cutting $cutting): RedirectResponse
    {
        $this->stockService->approveVerification(
            $cutting,
            $request->user(),
            $request->validated('approval_note'),
        );

        $this->flashSuccess('Verifikasi cutting berhasil disetujui. Stok produk telah ditambahkan ke toko.');

        return back(302);
    }

    public function rejectCutting(RejectVerificationRequest $request, Cutting $cutting): RedirectResponse
    {
        $this->stockService->rejectVerification(
            $cutting,
            $request->user(),
            $request->validated('reason'),
        );

        $this->flashSuccess('Verifikasi cutting berhasil ditolak.');

        return back(302);
    }

    public function approveRequest(
        ApproveVerificationRequest $request,
        OwnerVerificationRequest $ownerVerificationRequest,
    ): RedirectResponse {
        $this->ownerVerificationService->approveRequest(
            $ownerVerificationRequest,
            $request->user(),
            $request->validated('approval_note'),
        );

        $this->flashSuccess('Verifikasi berhasil disetujui.');

        return back(302);
    }

    public function rejectRequest(
        RejectVerificationRequest $request,
        OwnerVerificationRequest $ownerVerificationRequest,
    ): RedirectResponse {
        $this->ownerVerificationService->rejectRequest(
            $ownerVerificationRequest,
            $request->user(),
            $request->validated('reason'),
        );

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

        return back(302);
    }
}
