<?php
declare(strict_types=1);

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

MenuRegistry::register('documents', [
    'label_ar'   => 'المستندات',
    'label_en'   => 'Documents',
    'icon'       => 'file',
    'route'      => '/documents',
    'permission' => 'document.view',
    'parent'     => null,
    'order'      => 670,
    'children'   => [
        ['label_ar' => 'كل المستندات', 'label_en' => 'All Documents', 'route' => '/documents', 'permission' => 'document.view', 'order' => 1],
    ],
]);

PermissionRegistry::register('documents', [
    'document.upload' => ['ar' => 'رفع مستندات', 'en' => 'Upload Documents'],
    'document.view'   => ['ar' => 'عرض مستندات', 'en' => 'View Documents'],
    'document.delete' => ['ar' => 'حذف مستندات', 'en' => 'Delete Documents'],
]);

// Enrich member profile with document count
EventBus::listen('member.profile_data', function(array &$data) {
    if (isset($data['member']) && is_object($data['member']) && isset($data['member']->id)) {
        try {
            $data['documents'] = \App\Modules\Documents\Models\Document::getForMember((int) $data['member']->id);
        } catch (\Throwable $e) {
            $data['documents'] = [];
        }
    }
});