<?php
declare(strict_types=1);

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

MenuRegistry::register('seasonal', [
    'label_ar'   => 'العضوية الموسمية',
    'label_en'   => 'Seasonal Memberships',
    'icon'       => 'calendar',
    'route'      => '/seasonal',
    'permission' => 'temp.view',
    'parent'     => null,
    'order'      => 385,
    'children'   => [],
]);

EventBus::listen('member.profile_data', function (array &$data) {
    if (isset($data['member']) && $data['member']) {
        $memberId = is_object($data['member']) ? (int) $data['member']->id : (int) ($data['member']['id'] ?? 0);
        if ($memberId > 0) {
            try {
                $db = \App\Core\App::getInstance()->db();
                if ($db->tableExists('seasonal_memberships')) {
                    $data['seasonal'] = $db->select(
                        "SELECT s.*, b.name_ar as branch_name FROM seasonal_memberships s JOIN branches b ON b.id = s.branch_id WHERE s.member_id = ? AND s.is_archived = 0 ORDER BY s.start_date DESC",
                        [$memberId]
                    );
                }
            } catch (\Throwable $e) {}
        }
    }
});