<?php

namespace App\Console\Commands;

use App\Models\DataGadai;
use App\Models\JobLog;
use App\Models\Level;
use App\Models\CronjobCheck;
use Illuminate\Console\Command;
use  App\Models\Customer;
use  App\Models\Notification;
use Carbon\Carbon;
use Google_Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\PromoPoint;
use App\Models\VoucherPromoPoint;

class CronJobLog3 extends Command
{

    protected $signature = 'job:notifikasi3';

    protected $description = 'Save job ke table job log untuk info benefit';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        try {
            $this->info(Carbon::now()->format('Y-m-d H:i:s').' => Start Processing Notification 3');
            $tanggal_sekarang = Carbon::now()->format('Y-m-d');
            $job_check = DB::table('cronjob_check')->where('keterangan', 'INSERT NOTIFIKASI 3')->first();
			//$promo_point = PromoPoint::where('tgl_selesai_penukaran_point', '<', $tanggal_sekarang)->where('status', 'Aktif')->get();
            if ($job_check->status == 0) {
                DB::table('cronjob_check')->where('keterangan', 'INSERT NOTIFIKASI 3')->update([
                    'status' => 1,
                    'start_at' => Carbon::now()
                ]);

                $benefitCustomer = Customer::all();
                

                foreach ($benefitCustomer as $bn){

                    $level = Level::where('point', '<=', $bn->level_point)
                        ->orderByDesc('point')
                        ->first();

                    if ($level->coupon_id != null){
                        $tahunDanBulan = now()->addMonth()->format('Y-m');
                        $hasil = Carbon::createFromFormat('Y-m-d H:i:s', $tahunDanBulan . '-' . 1 . ' ' . '00:00:00');
                        $job = JobLog::where('customer_id', $bn->customer_id)->where('coupon_id', '!=', null)->where('level_id', $level->level_id)->first();
                        if (empty($job)){
                            $this->info(Carbon::now()->format('Y-m-d H:i:s').' => Processing Benefit For Customer = '.$bn->customer_id.' and Coupon ID = '. $level->coupon_id);
                            $jobCreate = JobLog::create([
                                'coupon_id' => $level->coupon_id,
                                'customer_id' => $bn->customer_id,
                                'created_who' => 'cronjob',
                                'status' => 0,
                                'count_sending' => 0,
                                'tgl_proses' => $hasil,
                                'level_id' => $level->level_id,
                                'tipe_proses' => 'Reward Voucher'
                            ]);
                            $this->info('Job Reward Voucher berhasil dibuat, ID: '. $jobCreate->id);
                            Log::info('Job Reward Voucher Dibuat, ID: '. $jobCreate->id);
                        } else {
                            $now = Carbon::now();
                            if ($job->status == 0){
                                $job->update([
                                    'status' => 0,
                                    'coupon_id' => $level->coupon_id,
                                    'level_id' => $level->level_id,
                                    'updated_who' => 'cronjob'
                                ]);
                                if ($job->tgl_proses < $now){
                                    $job->update([
                                        'status' => 0,
                                        'coupon_id' => $level->coupon_id,
                                        'updated_who' => 'cronjob',
                                        'tgl_proses' => $hasil,
                                    ]);
                                }
                            }
                        }
                    }
                }
            }

        $this->info(Carbon::now()->format('Y-m-d H:i:s').' => Finish Processing Notification 3');
        }catch (Exception $e){
            Log::error($e->getMessage());
            Log::error($e->getTraceAsString());

            $this->info($e->getTraceAsString());
        } finally{
            $job_check = DB::table('cronjob_check')->where('keterangan', 'INSERT NOTIFIKASI 3')->first();

            DB::table('cronjob_check')->where('keterangan', 'INSERT NOTIFIKASI 3')->update([
                'status' => 0,
                'start_at' => null
            ]);
        }
    }
}
