<?php

namespace App\Http\Controllers\Web;

use App\Models\BirthCode;
use App\Http\Controllers\Controller;
use App\Services\Notification\NotificationService;

class CronController extends Controller
{
    public function cobonBithDay()
    {
        if(date('m-d') == '01-01')
        {
            $startingDate = date('Y').'-1-1';
            $endingDate = date('Y').'-12-31';
            $startingDate = strtotime($startingDate);
            $endingDate = strtotime($endingDate);
            
            for ($currentDate = $startingDate; $currentDate <= $endingDate; $currentDate += (86400)) 
            {
                $date = date('Y-m-d', $currentDate);
                $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                $charactersLength = strlen($characters);
                $code = '';
                for ($i = 0; $i < 20; $i++) 
                {
                    $code .= $characters[random_int(0, $charactersLength - 1)];
                }
                
                BirthCode::create([
                    'code'=>$code,
                    'date'=>$date
                    ]);
            }   
        }
    }
    
    public function sendNotificationBirth()
    {
        $users = User::whereMonth('date_of_birth',date('m'))->whereDay('date_of_birth',date('d'))->cursor();
        $status = 'birthDay';
        $code = BirthCode::whereDate('date',date('Y-m-d'))->first()->code;
        foreach($users as $user)
        {
            (new NotificationService)->sendNotification($status,$user->id,null,null,null,null,null,$code);
        }
    }
    
    
    

}
