<?php
declare(strict_types=1);

use App\Core\Registries\MenuRegistry;
use App\Core\Registries\PermissionRegistry;
use App\Core\EventBus;
use App\Modules\Scheduling\Services\ConflictDetectionService;

MenuRegistry::register('scheduling', [
    'label_ar'   => 'الجدولة الذكية',
    'label_en'   => 'Smart Scheduling',
    'icon'       => 'calendar-clock',
    'route'      => '/scheduling',
    'permission' => 'scheduling.view',
    'parent'     => null,
    'order'      => 155,
    'children'   => [
        ['label_ar' => 'لوحة الجدولة',    'label_en' => 'Dashboard',        'route' => '/scheduling',           'permission' => 'scheduling.view',            'order' => 1],
        ['label_ar' => 'القواعد',          'label_en' => 'Rules',            'route' => '/scheduling/rules',     'permission' => 'scheduling.manage_rules',    'order' => 2],
        ['label_ar' => 'التعارضات',        'label_en' => 'Conflicts',        'route' => '/scheduling/conflicts', 'permission' => 'scheduling.view',            'order' => 3],
        ['label_ar' => 'طلبات التغيير',    'label_en' => 'Change Requests',  'route' => '/scheduling/requests',  'permission' => 'scheduling.manage_requests', 'order' => 4],
    ],
]);

PermissionRegistry::register('scheduling', [
    'scheduling.view'            => ['ar' => 'عرض الجدولة',           'en' => 'View Scheduling'],
    'scheduling.manage_rules'    => ['ar' => 'إدارة قواعد الجدولة',   'en' => 'Manage Scheduling Rules'],
    'scheduling.resolve'         => ['ar' => 'حل التعارضات',          'en' => 'Resolve Conflicts'],
    'scheduling.manage_requests' => ['ar' => 'إدارة طلبات التغيير',   'en' => 'Manage Change Requests'],
]);

EventBus::listen('session.created', function (array $data) {
    if (empty($data['session_id'])) return;
    $db = \App\Core\App::getInstance()->db();
    $session = $db->selectOne("SELECT * FROM training_sessions WHERE id = ?", [(int) $data['session_id']]);
    if (!$session) return;

    $proposed = [
        'coach_id'    => $session['coach_id'] ? (int) $session['coach_id'] : null,
        'facility_id' => $session['facility_id'] ? (int) $session['facility_id'] : null,
        'date'        => $session['session_date'],
        'start_time'  => $session['start_time'],
        'end_time'    => $session['end_time'],
        'session_id'  => (int) $session['id'],
        'exclude_session_id' => (int) $session['id'],
    ];

    $conflicts = ConflictDetectionService::detectConflicts($proposed);
    foreach ($conflicts as $conflict) {
        ConflictDetectionService::logConflict($conflict, $session['session_date']);
    }
}, 50);

EventBus::listen('session.rescheduled', function (array $data) {
    if (empty($data['session_id'])) return;
    $db = \App\Core\App::getInstance()->db();
    $session = $db->selectOne("SELECT * FROM training_sessions WHERE id = ?", [(int) $data['session_id']]);
    if (!$session) return;

    $proposed = [
        'coach_id'    => $session['coach_id'] ? (int) $session['coach_id'] : null,
        'facility_id' => $session['facility_id'] ? (int) $session['facility_id'] : null,
        'date'        => $session['session_date'],
        'start_time'  => $session['start_time'],
        'end_time'    => $session['end_time'],
        'session_id'  => (int) $session['id'],
        'exclude_session_id' => (int) $session['id'],
    ];

    $conflicts = ConflictDetectionService::detectConflicts($proposed);
    foreach ($conflicts as $conflict) {
        ConflictDetectionService::logConflict($conflict, $session['session_date']);
    }
}, 50);

EventBus::listen('reservation.created', function (array $data) {
    if (empty($data['reservation_id'])) return;
    $db = \App\Core\App::getInstance()->db();
    $res = $db->selectOne("SELECT * FROM reservations WHERE id = ?", [(int) $data['reservation_id']]);
    if (!$res) return;

    $proposed = [
        'facility_id' => $res['facility_id'] ? (int) $res['facility_id'] : null,
        'date'        => $res['reservation_date'],
        'start_time'  => $res['start_time'],
        'end_time'    => $res['end_time'],
        'exclude_reservation_id' => (int) $res['id'],
    ];

    $conflicts = ConflictDetectionService::checkFacilityOverlap($proposed);
    foreach ($conflicts as $conflict) {
        ConflictDetectionService::logConflict($conflict, $res['reservation_date']);
    }
}, 50);
