<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Voucher;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\JsonResponse;

class VoucherController extends Controller
{
    public function index(): Response
    {
        $vouchers = Voucher::latest()->get();

        return Inertia::render('CafePOS/Vouchers/Index', [
            'vouchers' => $vouchers,
        ]);
    }

    public function store(Request $request): RedirectResponse
    {
        $request->validate([
            'code' => ['required', 'string', 'max:50', 'unique:vouchers,code'],
            'type' => ['required', 'string', 'in:nominal,percent'],
            'value' => ['required', 'numeric', 'min:0'],
            'max_discount' => ['nullable', 'numeric', 'min:0'],
            'min_purchase' => ['required', 'numeric', 'min:0'],
            'expires_at' => ['nullable', 'date'],
            'usage_limit' => ['nullable', 'integer', 'min:1'],
            'is_active' => ['required', 'boolean'],
        ]);

        Voucher::create($request->only([
            'code', 'type', 'value', 'max_discount', 'min_purchase',
            'expires_at', 'usage_limit', 'is_active'
        ]));

        return redirect()->back()->with('success', 'Voucher berhasil dibuat.');
    }

    public function update(Request $request, Voucher $voucher): RedirectResponse
    {
        $request->validate([
            'code' => ['required', 'string', 'max:50', 'unique:vouchers,code,' . $voucher->id],
            'type' => ['required', 'string', 'in:nominal,percent'],
            'value' => ['required', 'numeric', 'min:0'],
            'max_discount' => ['nullable', 'numeric', 'min:0'],
            'min_purchase' => ['required', 'numeric', 'min:0'],
            'expires_at' => ['nullable', 'date'],
            'usage_limit' => ['nullable', 'integer', 'min:1'],
            'is_active' => ['required', 'boolean'],
        ]);

        $voucher->update($request->only([
            'code', 'type', 'value', 'max_discount', 'min_purchase',
            'expires_at', 'usage_limit', 'is_active'
        ]));

        return redirect()->back()->with('success', 'Voucher berhasil diperbarui.');
    }

    public function destroy(Voucher $voucher): RedirectResponse
    {
        $voucher->delete();
        return redirect()->back()->with('success', 'Voucher berhasil dihapus.');
    }

    public function verify(Request $request): JsonResponse
    {
        $request->validate([
            'code' => ['required', 'string'],
            'subtotal' => ['required', 'numeric', 'min:0'],
        ]);

        $voucher = Voucher::where('code', $request->code)->first();

        if (!$voucher) {
            return response()->json([
                'valid' => false,
                'message' => 'Kode voucher tidak ditemukan.',
            ]);
        }

        if (!$voucher->isValidForPurchase((float)$request->subtotal)) {
            return response()->json([
                'valid' => false,
                'message' => 'Kupon voucher tidak valid, kadaluarsa, melebihi kuota, atau total belanja belum memenuhi minimum.',
            ]);
        }

        return response()->json([
            'valid' => true,
            'voucher' => $voucher,
        ]);
    }
}
