<?php

namespace App\Http\Controllers\Api\Marketing;

use App\Http\Controllers\Controller;
use App\Models\GiftVoucher;
use App\Services\GiftVouchers\GiftVoucherCheckoutService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class GiftVoucherCheckoutController extends Controller
{
    public function __construct(private readonly GiftVoucherCheckoutService $checkout) {}

    public function tiers(): JsonResponse
    {
        return response()->json([
            'data' => $this->checkout->tiers(),
        ]);
    }

    public function store(Request $request): JsonResponse
    {
        $data = $this->validate($request, [
            'tier_key' => ['required', 'string'],
            'purchaser_email' => ['required', 'email:rfc,dns'],
            'recipient_email' => ['nullable', 'email:rfc,dns'],
            'recipient_name' => ['nullable', 'string', 'max:191'],
            'message' => ['nullable', 'string', 'max:500'],
            'success_url' => ['nullable', 'url'],
            'return_url' => ['nullable', 'url'],
        ]);

        $checkout = $this->checkout->create($data);

        if (! $checkout['checkout_url']) {
            throw ValidationException::withMessages([
                'tier_key' => __('Unable to create Paddle checkout.'),
            ]);
        }

        return response()->json($checkout);
    }

    public function show(Request $request): JsonResponse
    {
        $data = $request->validate([
            'checkout_id' => ['nullable', 'string', 'required_without_all:transaction_id,code'],
            'transaction_id' => ['nullable', 'string', 'required_without_all:checkout_id,code'],
            'code' => ['nullable', 'string', 'required_without_all:checkout_id,transaction_id'],
        ]);

        $voucherQuery = GiftVoucher::query();

        if (! empty($data['checkout_id'])) {
            $voucherQuery->where('paddle_checkout_id', $data['checkout_id']);
        }

        if (! empty($data['transaction_id'])) {
            $voucherQuery->orWhere('paddle_transaction_id', $data['transaction_id']);
        }

        if (! empty($data['code'])) {
            $voucherQuery->orWhere('code', strtoupper($data['code']));
        }

        $voucher = $voucherQuery->latest()->firstOrFail();

        return response()->json([
            'data' => [
                'code' => $voucher->code,
                'amount' => (float) $voucher->amount,
                'currency' => $voucher->currency,
                'expires_at' => optional($voucher->expires_at)->toIso8601String(),
                'recipient_name' => $voucher->recipient_name,
                'recipient_email' => $voucher->recipient_email,
                'purchaser_email' => $voucher->purchaser_email,
                'status' => $voucher->status,
                'redeemed_at' => optional($voucher->redeemed_at)->toIso8601String(),
                'refunded_at' => optional($voucher->refunded_at)->toIso8601String(),
            ],
        ]);
    }
}
