<?php
/**
 * Nexora temporal ban cleanup.
 * Ban metadata is Platform data. Game account block state is reconciled only in the realm recorded by the sanction
 * before the shared audit/ban metadata is removed.
 */

$file_name = basename(__FILE__);
$platform = Connection::Database('Platform');

$temporalBans = $platform->query_fetch("SELECT * FROM ".WEBENGINE_BANS);
if(is_array($temporalBans)) {
    foreach($temporalBans as $tempBan) {
        $banTimestamp = ((int)$tempBan['ban_days'] * 86400) + (int)$tempBan['ban_date'];
        if(time() <= $banTimestamp) continue;

        $realm = !empty($tempBan['realm']) ? NexoraRealm::validate($tempBan['realm']) : 'legacy';
        $realmDb = Connection::Database('Me_MuOnline', $realm);
        $unban = $realmDb->query(
            "UPDATE "._TBL_MI_." SET "._CLMN_BLOCCODE_." = 0 WHERE "._CLMN_USERNM_." = ?",
            array($tempBan['account_id'])
        );

        if($unban) {
            $platform->query("DELETE FROM ".WEBENGINE_BAN_LOG." WHERE account_id = ? AND realm = ?", array($tempBan['account_id'], $realm));
            $platform->query("DELETE FROM ".WEBENGINE_BANS." WHERE account_id = ? AND realm = ?", array($tempBan['account_id'], $realm));
            if(class_exists('NexoraAudit')) {
                NexoraAudit::log('ban.expired', 'account', $tempBan['account_id'], array('realm'=>$realm), array('banned'=>false), $realm);
            }
        }
    }
}

updateCronLastRun($file_name);
