<?php

use WHMCS\Session;
use Carbon\Carbon;
use WHMCS\Database\Capsule;

$hook = array(
    'hook' => 'DailyCronJob',
    'function' => 'suiten_DailyCronJob'
);

if (!function_exists('suiten_DailyCronJob')) {

	function suiten_DailyCronJob($DailyObject) {

		
		$global_module_name = 'SUITEN Whatsapp API';

        foreach (WHMCS\Database\Capsule::table('s10wa_config')->WHERE('suiten_Nama_Modul', $global_module_name)->get() as $modulwa_config) {
            $whatsapp_retention = $modulwa_config->suiten_logretention;
        }

		// logactivity( 'SUITEN_WAMLog: Housekeeping START');

		if($whatsapp_retention>0) {
		
			try {


				$deleteCount = Capsule::table('s10wa_loginlist')
				->where('lastlogin', '<', Carbon::now()->subDays($whatsapp_retention))
				->delete();

				logactivity( 'SUITEN_WAMLog: ' . $result . ' Housekeeping LastLogin = '.$deleteCount);

				$deleteCount = Capsule::table('s10wa_sendmsg')
				->where('s10wa_date', '<', Carbon::now()->subDays($whatsapp_retention))
				->delete();

				logactivity( 'SUITEN_WAMLog: ' . $result . ' Housekeeping SendMsg = '.$deleteCount);

				return $hook;
		
			} catch (\Exception $e) {

				$result = 'MySQL Error: ' . $e;
				exit();

			}		
		
		}

		return $hook;


	}

}

return $hook;