<?php

namespace App\Console\Commands;

use App\Models\PushNotification;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use App\Models\User;

class PushNotificationCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'notification:send';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'For delete last notification';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $dt = Carbon::now('Asia/Dhaka');
        $currentDate = $dt->toDateString();
        $currentTime = $dt->hour.':'.$dt->minute;

        //To get single notification message with date and time...
        $data = PushNotification::where('sending_date', $currentDate)->where('sending_time', $currentTime)->where('status', false)->first();

        if($data != null){
            $title = $data->notification_title;
            $message = $data->notification_message;

            // For Notification message...
            $tagRemovalNotificationMessage = strip_tags($message);
            $originalNotificationMessage = preg_replace("/\s|&nbsp;/"," ",$tagRemovalNotificationMessage);
            
            PushNotification::pushNotificationSend($title, $originalNotificationMessage);

            //To update status...
            $data->status = true;
            $data->save();
        }
    }
}
