<?php

namespace App\Domains\Integration\Services;

use App\Domains\Client\Models\Client;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Models\TelepagosPayment;
use App\Domains\Purchase\Models\TelepagosQr;
use App\Domains\Purchase\Services\CheckoutService;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TelepagosWebhookService
{
    public function __construct(
        private readonly CheckoutService $checkoutService,
    ) {}

    /**
     * Handle the Telepagos webhook notification.
     *
     * @throws Exception
     */
    public function handleWebhook(Client $client, string $cashinId): void
    {
        Log::channel('telepagos')->info('Telepagos webhook received.', [
            'client_code' => $client->code,
            'cashin_id' => $cashinId,
        ]);

        $telepagosService = new TelepagosIntegrationService;
        $telepagosService->forClient($client);

        try {
            $details = $telepagosService->getCashinDetails($cashinId);

            $qrOrderId = $details['data']['qr_order_id'] ?? $details['qr_order_id'] ?? null;
            $amount = $this->normalizeAmount($details['data']['amount'] ?? $details['amount'] ?? 0);
            $operationId = $details['data']['operation_id'] ?? $details['operation_id'] ?? null;

            $paymentData = [
                'compra_id' => null,
                'matched_purchase_ids' => null,
                'cuit_buyer' => $details['data']['buyer']['cuit'] ?? $details['buyer']['cuit'] ?? null,
                'cvu_buyer' => $details['data']['buyer']['cvu'] ?? $details['buyer']['cvu'] ?? null,
                'amount' => $amount,
                'concept' => $details['data']['concept'] ?? $details['concept'] ?? null,
                'operation' => $details['data']['operation'] ?? $details['operation'] ?? null,
                'operation_id' => $details['data']['operation_id'] ?? $details['operation_id'] ?? null,
                'transaction_id' => $details['data']['transaction_id'] ?? $details['transaction_id'] ?? null,
                'qr_order_id' => $qrOrderId,
                'link_id' => $details['data']['link_id'] ?? $details['link_id'] ?? null,
            ];

            $transferenciaOperationIds = [1, 3, 11];
            $qrOperationIds = [31, 37, 47];

            $compra = null;

            if (in_array((int) $operationId, $transferenciaOperationIds, true)) {
                $cuit = $details['data']['buyer']['cuit'] ?? $details['buyer']['cuit'] ?? null;

                if (! $cuit) {
                    Log::channel('telepagos')->warning('Telepagos webhook: CUIT not found for transfer.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                    ]);

                    return;
                }

                $dni = substr($cuit, 2, -1);

                $tenantCodes = $client->tenants()->pluck('codigo');
                $purchases = Purchase::whereIn('tenant_codigo', $tenantCodes)
                    ->where('transfer_payer_dni', $dni)
                    ->whereIn('status', [
                        Purchase::STATUS_CREATED,
                        Purchase::STATUS_PENDING_PAYMENT,
                        Purchase::STATUS_IN_REVIEW,
                    ])
                    ->where('payment_method', 'transfer')
                    ->where('total', $amount)
                    ->latest()
                    ->get();

                $paymentData['matched_purchase_ids'] = $purchases->pluck('id')->all();
                $compra = $purchases->count() === 1 ? $purchases->first() : null;

                if (! $compra) {
                    if ($purchases->count() > 1) {
                        TelepagosPayment::create($paymentData);
                    }

                    Log::channel('telepagos')->warning('Telepagos webhook: Expected exactly one matching purchase.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                        'amount' => $amount,
                        'matches' => $purchases->count(),
                    ]);

                    return;
                }
            } elseif (in_array((int) $operationId, $qrOperationIds, true)) {
                if (! $qrOrderId) {
                    Log::channel('telepagos')->warning('Telepagos webhook: qr_order_id not present in provider response.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                    ]);

                    return;
                }

                $telepagosQr = TelepagosQr::where('qr_order_id', $qrOrderId)->first();

                if (! $telepagosQr) {
                    Log::channel('telepagos')->warning('Telepagos webhook: QR not found in database.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                        'qr_order_id' => $qrOrderId,
                    ]);

                    return;
                }

                $compra = $telepagosQr->compra;

                if (! $compra) {
                    Log::channel('telepagos')->warning('Telepagos webhook: Purchase not found for QR.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                        'qr_order_id' => $qrOrderId,
                    ]);

                    return;
                }

                if (! $client->tenants()->where('codigo', $compra->tenant_codigo)->exists()) {
                    Log::channel('telepagos')->warning('Telepagos webhook: Purchase does not belong to client.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                        'purchase_id' => $compra->id,
                    ]);

                    return;
                }

                if (! in_array($compra->status, [
                    Purchase::STATUS_PENDING_PAYMENT,
                ], true)) {
                    Log::channel('telepagos')->warning('Telepagos webhook: Purchase is not awaiting payment confirmation.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                        'purchase_id' => $compra->id,
                        'purchase_status' => $compra->status,
                    ]);

                    return;
                }

                $totalAmount = $this->normalizeAmount($compra->getTotalAmount());

                if ($amount !== $totalAmount) {
                    Log::channel('telepagos')->warning('Telepagos webhook: Amount mismatch.', [
                        'client_code' => $client->code,
                        'cashin_id' => $cashinId,
                        'purchase_id' => $compra->id,
                        'cashin_amount' => $amount,
                        'purchase_amount' => $totalAmount,
                    ]);

                    return;
                }
            } else {
                Log::channel('telepagos')->warning('Telepagos webhook: Unknown operation_id.', [
                    'client_code' => $client->code,
                    'cashin_id' => $cashinId,
                    'operation_id' => $operationId,
                ]);

                return;
            }

            $paymentData['compra_id'] = $compra->id;

            DB::transaction(function () use ($compra, $paymentData) {
                TelepagosPayment::create($paymentData);
                $this->checkoutService->confirmPaidPurchase($compra);
            });

            Log::channel('telepagos')->info('Telepagos webhook processed successfully.', [
                'client_code' => $client->code,
                'cashin_id' => $cashinId,
                'purchase_id' => $compra->id,
                'transaction_id' => $paymentData['transaction_id'],
            ]);
        } catch (Exception $e) {
            Log::channel('telepagos')->error('Telepagos webhook processing failed.', [
                'client_code' => $client->code,
                'cashin_id' => $cashinId,
                'error' => $e->getMessage(),
            ]);
            throw $e;
        }
    }

    protected function normalizeAmount(mixed $amount): string
    {
        return number_format((float) $amount, 2, '.', '');
    }
}
