<?php
declare(strict_types=1);

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

PermissionRegistry::register('achievements', [
    'achievement.view'   => ['ar' => 'عرض الإنجازات',   'en' => 'View Achievements'],
    'achievement.manage' => ['ar' => 'إدارة الإنجازات', 'en' => 'Manage Achievements'],
]);

MenuRegistry::register('achievements', [
    'label_ar'   => 'الإنجازات',
    'icon'       => 'award',
    'route'      => '/achievements',
    'permission' => 'achievement.view',
    'order'      => 420,
    'parent'     => 'sports_activities',
]);

// ─────────────────────────────────────────────────────────────
// Event Listeners for Achievement Checks
// ─────────────────────────────────────────────────────────────

// Attendance recorded → check attendance achievements
EventBus::listen('session_attendance.recorded', function (array $data) {
    try {
        $playerId = (int) ($data['player_id'] ?? 0);
        if ($playerId <= 0) return;

        \App\Modules\Achievements\Services\AchievementEngine::checkAttendanceAchievements($playerId);
    } catch (\Throwable $e) {
        Logger::error("Achievement attendance check error: " . $e->getMessage());
    }
}, 10);

// Player evaluation submitted → check performance achievements
EventBus::listen('player.evaluation_submitted', function (array $data) {
    try {
        $playerId = (int) ($data['player_id'] ?? 0);
        if ($playerId <= 0) return;

        \App\Modules\Achievements\Services\AchievementEngine::checkAndAward($playerId, 'evaluation_submitted', $data);
    } catch (\Throwable $e) {
        Logger::error("Achievement evaluation check error: " . $e->getMessage());
    }
}, 10);

// Tournament match won → check competition achievements
EventBus::listen('tournament.match_won', function (array $data) {
    try {
        $playerId = (int) ($data['player_id'] ?? 0);
        if ($playerId <= 0) return;

        \App\Modules\Achievements\Services\AchievementEngine::checkAndAward($playerId, 'tournament_win', $data);
    } catch (\Throwable $e) {
        Logger::error("Achievement tournament check error: " . $e->getMessage());
    }
}, 10);

// Fitness test recorded → check fitness achievements
EventBus::listen('player.fitness_test_recorded', function (array $data) {
    try {
        $playerId = (int) ($data['player_id'] ?? 0);
        if ($playerId <= 0) return;

        \App\Modules\Achievements\Services\AchievementEngine::checkAndAward($playerId, 'fitness_test', $data);
    } catch (\Throwable $e) {
        Logger::error("Achievement fitness check error: " . $e->getMessage());
    }
}, 10);

// Injury recovered → check recovery achievements
EventBus::listen('player.injury_recovered', function (array $data) {
    try {
        $playerId = (int) ($data['player_id'] ?? 0);
        if ($playerId <= 0) return;

        \App\Modules\Achievements\Services\AchievementEngine::checkAndAward($playerId, 'injury_recovered', $data);
    } catch (\Throwable $e) {
        Logger::error("Achievement recovery check error: " . $e->getMessage());
    }
}, 10);
