<?php
date_default_timezone_set('Asia/Dhaka');
$date_time = date('Y-m-d g:i:sA');
$date = date('Y-m-d');
$day = date('d');
$todayDay = $day;
$lastDayOfMonth = date('t'); // এই মাসের shes din

$m = date('m');
$y = date('Y');

require(__DIR__ . '/services/Model.php');
$obj = new Model();

$adminMobile = $obj->getSettingValue('mobile', 'mobile');
if (strpos($adminMobile, '88') !== 0) {
    $adminMobile = '88' . $adminMobile;
}

$totaluser = 0;
$disconnectCount = 0;

$isMikrotikDisconnect = $obj->getSettingValue('mikrotikDisconCronJob', 'mikrotikDisconCronJob');
$cronSmsDay = $obj->getSettingValue('cronSmsSend', 'cronSmsSend');
$mikDisDay = $obj->getSettingValue('MikDisconday', 'MikDisconday');

if (isset($mikDisDay) && $mikDisDay == 'cronday') {
    $dd = $todayDay;
} else {
    $dd = $mikDisDay;
}

if (isset($isMikrotikDisconnect) && $isMikrotikDisconnect == 'active') {
    $agWhereCon = "";
    $MikDisconBillStatusRes = $obj->getSettingValue('MikDisconBillStatus', 'MikDisconBillStatus');
    if (isset($MikDisconBillStatusRes) && $MikDisconBillStatusRes == 'inc_par_paid') {
        $agWhereCon = "AND customer_billing.dueadvance > 0";
    } else {
        $agWhereCon = "AND tbl_agent.bill_status = '0' AND customer_billing.dueadvance > 0";
    }

    foreach ($obj->view_all('mikrotik_user') as $mikrotiks) {
        $individualMikDisconUsers = 0;
        $mikrotikId = $mikrotiks['id'];

        $agents = $obj->raw_sql(
            "SELECT * 
            FROM tbl_agent 
            LEFT JOIN customer_billing ON tbl_agent.ag_id = customer_billing.agid
            WHERE tbl_agent.ag_status = '1' 
            AND (
                tbl_agent.mikrotik_disconnect = '$todayDay'
                OR (tbl_agent.mikrotik_disconnect > '$lastDayOfMonth' AND '$todayDay' = '$lastDayOfMonth')
            )
            AND tbl_agent.deleted_at IS NULL 
            AND tbl_agent.mikrotik_id='" . $mikrotiks['id'] . "' 
            $agWhereCon
            ORDER BY `ag_id` ASC"
        );
        $totaluser += count($agents);

        if ($obj->checkConnection($mikrotikId)) {
            foreach ($agents as $agent) {
                $obj->disableSingleSecret($mikrotikId, $agent['ip']);
                $disconnectCount++;
                $individualMikDisconUsers++;
            }

            $obj->insertData("cron_job_logs", [
                'type' => 'mikrotik',
                'mikrotik_ip' => $mikrotiks['mik_ip'],
                'total_disconnect' => $individualMikDisconUsers,
                'description' => $individualMikDisconUsers <= 0 ? 'No user found to disconnect.' : '',
                'status' => 'success',
                'date' => date('Y-m-d')
            ]);
        } else {
            $mikrotikIP = isset($mikrotiks['mik_ip']) ? $mikrotiks['mik_ip'] : '';
            $obj->insertData("cron_job_logs", [
                'type' => 'mikrotik',
                'description' => 'Mikrotik is not connected',
                'mikrotik_ip' => $mikrotikIP,
                'status' => 'failed',
                'date' => date('Y-m-d')
            ]);

            if (!empty($adminMobile)) {
                $msg = "আপনার সফটওয়্যার থেকে  {$mikrotikIP} ডিজেবল আছে। এজন্যে এই  ip  এর কাস্টমার গুলো ডিসকানেক্ট  হয়নি।";
                $obj->sendsms($adminMobile, $msg);
            }
        }
    }

    if ($disconnectCount > 0) {
        $total_dis = "আজকে  {$disconnectCount} জন কাস্টমারের লাইন অফ হইসে।";
        $obj->sendsms($adminMobile, $total_dis);
    }
}

// ================= SMS Reminder =================
$smsInfo = $obj->rawSqlSingle("SELECT * FROM sms WHERE status = '6'");
if (isset($smsInfo['smshead']) && $smsInfo['smshead'] == 'active') {
    $smsArray = [];
    $smsI = 0;

    $allcustomerforsms = $obj->raw_sql(
        "SELECT * 
        FROM tbl_agent
        LEFT JOIN customer_billing ON tbl_agent.ag_id = customer_billing.agid
        WHERE tbl_agent.ag_status = '1' 
          AND tbl_agent.bill_status = '0' 
          AND tbl_agent.deleted_at IS NULL
          AND customer_billing.dueadvance > 0
          AND (
            tbl_agent.mikrotik_disconnect IN (DAY(CURDATE() + INTERVAL '$cronSmsDay' DAY))
            OR (
                tbl_agent.mikrotik_disconnect > '$lastDayOfMonth'
                AND DAY(CURDATE() + INTERVAL '$cronSmsDay' DAY) = '$lastDayOfMonth'
            )
          )
        ORDER BY ag_id ASC"
    );

    foreach ($allcustomerforsms as $agent) {
        $sms_b = isset($smsInfo['smsbody']) ? $smsInfo['smsbody'] : NULL;
        $totalDueAmount = $obj->rawSqlSingle("SELECT dueadvance FROM customer_billing WHERE agid = '$agent[ag_id]'")['dueadvance'];

        $var_replacement = [
            '{CUSTOMER_NAME}' => $agent['ag_name'],
            '{DUE_AMOUNT}' => $totalDueAmount,
            '{CUSTOMER_ID}' => $agent['cus_id'],
            '{IP_ADDRESS}' => $agent['ip'],
            '{PACKAGE_NAME}' =>  $obj->rawSqlSingle("SELECT package_name FROM tbl_package WHERE net_speed = '$agent[mb]'")['package_name'],
            '{MONTHLY_BILL}' => $agent['taka'],
        ];

        $sms_b = strtr($sms_b, $var_replacement);

        if ($agent['ag_mobile_no']) {
            $mobilenum = "88" . $agent['ag_mobile_no'];
            $smsArray[$smsI++] = ["to" => $mobilenum, "message" => $sms_b];
        }
    }

    $smsSuccess = $obj->sms_send($smsArray);
    $smsCountArray = count($smsArray);

    if ($smsSuccess === true) {
        $obj->insertData("cron_job_logs", [
            'type' => 'sms',
            'total_sms_sent' => $smsCountArray,
            'status' => 'success',
            'date' => date('Y-m-d')
        ]);
        } else {
    
        // Default description
        $description = 'No user found to send sms.';
    
        // SMS failed
        if ($smsCountArray > 0) {
    
            // Balance not enough
            if (
                isset($smsSuccess['response_code']) &&
                $smsSuccess['response_code'] == '1007'
            ) {
    
                $description = 'SMS Error Code: 1007';
    
            } else {
    
                $description = isset($smsSuccess['response_code'])
                    ? 'SMS Error Code: ' . $smsSuccess['response_code']
                    : 'SMS Sending Failed!';
            }
        }
    
        $obj->insertData("cron_job_logs", [
            'type' => 'sms',
            'total_sms_sent' => $smsCountArray,
            'status' => $smsCountArray > 0 ? 'failed' : 'success',
            'description' => $description,
            'date' => date('Y-m-d')
        ]);
    }
}

?>