<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Log;
use Knox\AFT\AFT;
use App\Models\MpesaLogs;
use App\Models\Discount;
use App\Models\WithdrawalLogs;
use App\Models\Sambaza;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
    //  Send Every Friday;
      //   $schedule->call(function () {
      //       $airtime=Discount::where('name','airtime')->first();
      //       $customers=MpesaLogs::select('phone')->groupBy('phone')->get();
      //       foreach($customers as $c){
      //            $message='Get '.$airtime->rate.'% discount on your Safaricom Airtime purchase on SwapKredo. Call or text 0798621000 for details. Visit: https://www.swapkredo.com/safaricom T&C Apply';
      //           AFT::sendMessage($c->phone, $message,'BIMONDI');
      //       }
      //  })->weeklyOn(5, '14:00');
    // //   Send EveryDay;
    //     $schedule->call(function () {
    //         // $airtime=Discount::where('name','airtime')->first();
    //         $customers=MpesaLogs::select('phone')->groupBy('phone')->get();
    //         foreach($customers as $c){
    //              $message='SWAP@75% TODAY. Take advantage of our SHUJAA PROMO to convert airtime to Mpesa at 75% rate TODAY. Visit https://www.swapkredo.com/mpesa T&Cs apply.';
    //             AFT::sendMessage($c->phone, $message,'BIMONDI');
    //         }
    //    })->dailyAt('13:00');


     //  Send Every Month;
    //     $schedule->call(function () {
    //       $data=DB::table('users')->update(['password_changed'=>false]);
    //         // $customers=WithdrawalLogs::select('phone')->groupBy('phone')->get();
    //         // foreach($customers as $c){
    //         //      $message="DON'T LET YOUR BONGA POINTS EXPIRE. Swap your Bonga Points for Mpesa @0.18 BOB per point. Minimum points to transfer is 100. Visit: https://www.swapkredo.com/bonga";
    //         //     AFT::sendMessage($c->phone, $message,'BIMONDI');
    //         // }
    //    })->monthlyOn(1, '21:00');

       //  Send Every Hour;
      //  $schedule->call(function () {
      //   $data=MpesaLogs::where('status','PENDING')->orWhere('status','CANCELLED')->delete();
      // })->hourly();

      $schedule->call(function () {
        $data=DB::table('sambazas')->update(['balance' => 10000]);
    })->dailyAt('03:00');

       $schedule->call(function () {
        $active=Discount::where('name','receiving_no')->first();
        $new=Sambaza::where('number','!=',$active->rate)->where('status',true)->inRandomOrder()->first();
        $updateActive=Discount::where('name','receiving_no')->update(['rate'=>$new->number]);
      })->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
