<?php

namespace Config;

use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\FrameworkException;
use CodeIgniter\HotReloader\HotReloader;
use App\Listeners\SchoolEventListener;
use App\Listeners\AttendanceConsequenceListener;
use App\Listeners\WhatsappInviteListener;
use App\Listeners\BelowSixtyEmailListener;
use App\Listeners\DecisionEmailListener;

// Create an instance so we can use $this->emailService like your other handlers
$waListener = new WhatsappInviteListener(service('emailService'));

$listener = new SchoolEventListener();
/*
 * --------------------------------------------------------------------
 * Application Events
 * --------------------------------------------------------------------
 * Events allow you to tap into the execution of the program without
 * modifying or extending core files. This file provides a central
 * location to define your events, though they can always be added
 * at run-time, also, if needed.
 *
 * You create code that can execute by subscribing to events with
 * the 'on()' method. This accepts any form of callable, including
 * Closures, that will be executed when the event is triggered.
 *
 * Example:
 *      Events::on('create', [$myInstance, 'myMethod']);
 */

Events::on('pre_system', static function () {
    if (ENVIRONMENT !== 'testing') {
        if (ini_get('zlib.output_compression')) {
            throw FrameworkException::forEnabledZlibOutputCompression();
        }

        while (ob_get_level() > 0) {
            ob_end_flush();
        }

        ob_start(static fn($buffer) => $buffer);
    }

    /*
     * --------------------------------------------------------------------
     * Debug Toolbar Listeners.
     * --------------------------------------------------------------------
     * If you delete, they will no longer be collected.
     */
    if (CI_DEBUG && ! is_cli()) {
        Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
        Services::toolbar()->respond();
        // Hot Reload route - for framework use on the hot reloader.
        if (ENVIRONMENT === 'development') {
            Services::routes()->get('__hot-reload', static function () {
                (new HotReloader())->run();
            });
        }
    }
});

// Register the delete unverified user event
Events::on('delete_unverified_user', function ($user) {
    (new SchoolEventListener())->handleDeleteUnverifiedUser($user);
});


// User Events
Events::on('userRegistered', [$listener, 'handleStudentRegistered']);
Events::on('userProfileUpdated', [$listener, 'handleUserProfileUpdated']);
Events::on('userDeactivated', [$listener, 'handleUserDeactivated']);


// Students enrollment Events
Events::on('studentRegistered', [$listener, 'handleStudentRegistered']);
Events::on('admissionUnderReview', [$listener, 'handleAdmissionUnderReview']);
Events::on('paymentPending', [$listener, 'handlePaymentPending']);
Events::on('studentEnrolled', [$listener, 'handleStudentEnrolled']);
Events::on('withdrawUnderReview', [$listener, 'handleWithdrawUnderReview']);
Events::on('refundPending', [$listener, 'handleRefundPending']);
Events::on('withdrawn', [$listener, 'handleWithdrawn']);
Events::on('waitlist', [$listener, 'handleWaitlist']);
Events::on('denied',   [$listener, 'handleDenied']);


// Score Events
Events::on('scoresPosted', [$listener, 'handleScoresPosted']);
Events::on('finalScoreReleased', [$listener, 'handleFinalScoreReleased']);


// Attendance Events
Events::on('studentMarkedAbsent', [$listener, 'handleStudentMarkedAbsent']);


// Payment Events
Events::on('paymentReceived', [$listener, 'handlePaymentReceived']);
Events::on('paymentMissed', [$listener, 'handlePaymentMissed']);


// Schedule and Messaging
Events::on('classScheduleUpdated', [$listener, 'handleClassScheduleUpdated']);
Events::on('newMessageReceived', [$listener, 'handleNewMessageReceived']);
Events::on('systemAnnouncementPosted', [$listener, 'handleSystemAnnouncementPosted']);

// Account registration
Events::on('user_registered', [$listener, 'handleNewAccountAdded']);

//Custom Notification
Events::on('customNotification', [$listener, 'handleCustomNotification']);

//Extra charge
Events::on('extraCharge', [$listener, 'handleExtraCharge']);

Events::on('attendance.follow_up',   [AttendanceConsequenceListener::class, 'followUp']);
Events::on('attendance.final_warning', [AttendanceConsequenceListener::class, 'finalWarning']);
Events::on('attendance.dismissal',   [AttendanceConsequenceListener::class, 'dismissal']);
Events::on('below60.email', [BelowSixtyEmailListener::class, 'handle']);
Events::on('below60.decision_email', [DecisionEmailListener::class, 'handle']);

//Whatsapp Event listener
Events::on('whatsapp_invites.send', [$waListener, 'handle']);
