<?php
declare(strict_types=1);

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

MenuRegistry::register('pricing', [
    'label_ar'   => 'التسعير',
    'label_en'   => 'Pricing',
    'icon'       => 'banknote',
    'route'      => '/pricing',
    'permission' => 'pricing.view',
    'parent'     => null,
    'order'      => 850,
    'children'   => [
        ['label_ar' => 'لوحة التسعير',       'label_en' => 'Pricing Dashboard',   'route' => '/pricing',                    'permission' => 'pricing.view',                  'order' => 1],
        ['label_ar' => 'عروض مجلس الإدارة',  'label_en' => 'Board Offers',        'route' => '/pricing/board-offers',       'permission' => 'pricing.board_offers.view',     'order' => 2],
        ['label_ar' => 'الخصومات الخاصة',    'label_en' => 'Special Discounts',   'route' => '/pricing/special-discounts',  'permission' => 'pricing.special_discounts.view', 'order' => 3],
        ['label_ar' => 'الخصومات اللائحية',  'label_en' => 'Regulatory Discounts','route' => '/pricing/regulatory-discounts','permission' => 'pricing.regulatory_discounts.view', 'order' => 4],
    ],
]);

PermissionRegistry::register('pricing', [
    'pricing.view'                     => ['ar' => 'عرض لوحة التسعير',       'en' => 'View Pricing Dashboard'],
    'pricing.edit'                     => ['ar' => 'تعديل الأسعار والرسوم',   'en' => 'Edit Prices & Fees'],
    'pricing.board_offers.view'        => ['ar' => 'عرض عروض مجلس الإدارة',  'en' => 'View Board Offers'],
    'pricing.board_offers.create'      => ['ar' => 'إنشاء عرض مجلس إدارة',   'en' => 'Create Board Offer'],
    'pricing.board_offers.edit'        => ['ar' => 'تعديل عروض مجلس الإدارة', 'en' => 'Edit Board Offers'],
    'pricing.special_discounts.view'   => ['ar' => 'عرض الخصومات الخاصة',    'en' => 'View Special Discounts'],
    'pricing.special_discounts.create' => ['ar' => 'إنشاء خصم خاص',          'en' => 'Create Special Discount'],
    'pricing.special_discounts.edit'   => ['ar' => 'تعديل الخصومات الخاصة',  'en' => 'Edit Special Discounts'],
    'pricing.regulatory_discounts.view'    => ['ar' => 'عرض الخصومات اللائحية',    'en' => 'View Regulatory Discounts'],
    'pricing.regulatory_discounts.create'  => ['ar' => 'إنشاء خصم لائحي',          'en' => 'Create Regulatory Discount'],
    'pricing.regulatory_discounts.edit'    => ['ar' => 'تعديل الخصومات اللائحية',  'en' => 'Edit Regulatory Discounts'],
    'pricing.regulatory_discounts.approve' => ['ar' => 'اعتماد طلبات الخصم اللائحي','en' => 'Approve Regulatory Discount Applications'],
]);

// When a member is activated, check if their discount includes free subscription bonus
EventBus::listen('member.activated', function (array $data) {
    try {
        $memberId = (int) ($data['member_id'] ?? 0);
        if ($memberId <= 0) return;

        $freeYears = \App\Modules\Pricing\Services\SpecialDiscountService::getBonusFreeYears($memberId);
        if ($freeYears > 0) {
            \App\Modules\Pricing\Services\SpecialDiscountService::applyFreeSubscriptionBonus($memberId, $freeYears);
        }
    } catch (\Throwable $e) {
        \App\Core\Logger::error("pricing: member.activated bonus check failed: " . $e->getMessage(), ['data' => $data]);
    }
}, 50);
