<?php
declare(strict_types=1);

use App\Core\Registries\MenuRegistry;
use App\Core\Registries\PermissionRegistry;
use App\Core\EventBus;

MenuRegistry::register('cashier', [
    'label_ar'   => 'الخزينة',
    'label_en'   => 'Cashier',
    'icon'       => 'credit-card',
    'route'      => '/cashier',
    'permission' => 'cashier.view_queue',
    'parent'     => null,
    'order'      => 250,
    'children'   => [
        ['label_ar' => 'طابور الدفع', 'label_en' => 'Payment Queue', 'route' => '/cashier', 'permission' => 'cashier.view_queue', 'order' => 1],
    ],
]);

PermissionRegistry::register('cashier', [
    'cashier.view_queue'      => ['ar' => 'عرض طابور الخزينة',  'en' => 'View Cashier Queue'],
    'cashier.process_payment' => ['ar' => 'معالجة طلب دفع',     'en' => 'Process Payment Request'],
    'cashier.cancel_request'  => ['ar' => 'إلغاء طلب دفع',      'en' => 'Cancel Payment Request'],
]);

// When a payment is voided, delegate to PaymentLifecycleService for status reversion
EventBus::listen('payment.voided', function (array $data) {
    try {
        $paymentId = (int) ($data['payment_id'] ?? 0);
        $memberId = (int) ($data['member_id'] ?? 0);
        $paymentType = $data['payment_type'] ?? '';

        if ($paymentId <= 0 || $memberId <= 0) return;

        $db = \App\Core\App::getInstance()->db();
        $payment = $db->selectOne("SELECT related_entity_type, related_entity_id FROM payments WHERE id = ?", [$paymentId]);
        $entityType = $payment['related_entity_type'] ?? null;
        $entityId = (int) ($payment['related_entity_id'] ?? 0);

        \App\Modules\Payments\Services\PaymentLifecycleService::onPaymentVoided(
            $paymentId, $memberId, $paymentType, $entityType, $entityId > 0 ? $entityId : null
        );
    } catch (\Throwable $e) {
        \App\Core\Logger::error("payment.voided listener failed: " . $e->getMessage(), ['data' => $data]);
    }
});

// When a payment request is cancelled, delegate to PaymentLifecycleService
EventBus::listen('payment_request.cancelled', function (array $data) {
    try {
        \App\Modules\Payments\Services\PaymentLifecycleService::onPaymentRequestCancelled($data);
    } catch (\Throwable $e) {
        \App\Core\Logger::error("payment_request.cancelled listener failed: " . $e->getMessage(), ['data' => $data]);
    }
});

// When a payment request is completed, delegate activation to PaymentLifecycleService
EventBus::listen('payment_request.completed', function (array $data) {
    try {
        $db = \App\Core\App::getInstance()->db();
        $paymentType = $data['payment_type'] ?? '';
        $memberId = (int) ($data['member_id'] ?? 0);
        $paymentId = (int) ($data['payment_id'] ?? 0);
        $requestId = (int) ($data['request_id'] ?? 0);
        $entityType = $data['related_entity_type'] ?? null;
        $entityId = (int) ($data['related_entity_id'] ?? 0);

        if ($memberId <= 0 || $paymentId <= 0) return;

        // Membership activation (membership_fee / down_payment)
        if (in_array($paymentType, ['membership_fee', 'down_payment'], true)) {
            $requestData = [];
            if ($paymentType === 'down_payment' && $requestId > 0) {
                $request = $db->selectOne("SELECT notes FROM payment_requests WHERE id = ?", [$requestId]);
                $notesData = $request && $request['notes'] ? json_decode($request['notes'], true) : [];
                $requestData['installment_months'] = $notesData['installment_months'] ?? 30;
            }

            $result = \App\Modules\Payments\Services\PaymentLifecycleService::onMembershipPaymentCompleted(
                $memberId, $paymentId, $paymentType, $requestData
            );

            if ($result['success'] && empty($result['already_active'])) {
                $membershipNumber = $result['membership_number'] ?? null;
                EventBus::dispatch('member.activated', ['member_id' => $memberId, 'membership_number' => $membershipNumber]);
            }
        }

        // Addition fee — activate the specific dependent
        if ($paymentType === 'addition_fee' && $entityType && $entityId > 0) {
            \App\Modules\Payments\Services\PaymentLifecycleService::onAdditionFeeCompleted(
                $entityType, $entityId, $memberId, $paymentId
            );

            $receiptNumber = $data['receipt_number'] ?? '';
            if ($receiptNumber) {
                $validTables = ['spouses', 'children', 'temporary_members'];
                if (in_array($entityType, $validTables, true)) {
                    $db->update($entityType, ['fee_receipt_number' => $receiptNumber], '`id` = ?', [$entityId]);
                }
            }

            $eventMap = [
                'spouses'          => ['event' => 'spouse.fee_paid',    'key' => 'spouse_id'],
                'children'         => ['event' => 'child.fee_paid',     'key' => 'child_id'],
                'temporary_members'=> ['event' => 'temporary.fee_paid', 'key' => 'temp_id'],
            ];
            if (isset($eventMap[$entityType])) {
                $cfg = $eventMap[$entityType];
                EventBus::dispatch($cfg['event'], [$cfg['key'] => $entityId, 'member_id' => $memberId, 'payment_id' => $paymentId]);
            }
        }

        // Seasonal fee activation
        if ($paymentType === 'seasonal_fee' && $entityType === 'seasonal_memberships' && $entityId > 0) {
            $db->update('seasonal_memberships', [
                'status'     => 'active',
                'updated_at' => date('Y-m-d H:i:s'),
            ], '`id` = ?', [$entityId]);
            EventBus::dispatch('seasonal.fee_paid', ['seasonal_id' => $entityId, 'member_id' => $memberId, 'payment_id' => $paymentId]);
        }

        // Life-event fees (divorce, death, waiver, separation)
        if ($entityId > 0) {
            $tableMap = [
                'divorce_fee'    => ['table' => 'divorce_cases',    'event' => 'divorce.fee_paid',  'key' => 'case_id'],
                'death_fee'      => ['table' => 'death_cases',      'event' => 'death.fee_paid',    'key' => 'case_id'],
                'waiver_fee'     => ['table' => 'waiver_requests',  'event' => 'waiver.fee_paid',   'key' => 'waiver_id'],
                'separation_fee' => ['table' => 'transfer_requests','event' => 'transfer.fee_paid', 'key' => 'transfer_id'],
            ];
            if (isset($tableMap[$paymentType])) {
                $cfg = $tableMap[$paymentType];
                $db->update($cfg['table'], [
                    'status'     => 'fee_paid',
                    'updated_at' => date('Y-m-d H:i:s'),
                ], '`id` = ?', [$entityId]);
                EventBus::dispatch($cfg['event'], [$cfg['key'] => $entityId, 'payment_id' => $paymentId]);
            }
        }
    } catch (\Throwable $e) {
        \App\Core\Logger::error("payment_request.completed listener failed: " . $e->getMessage(), ['data' => $data]);
    }
});
