<?php

namespace App\Console\Commands;

use App\Helpers\Notification;
use App\UsersNotification;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('Task schedule run successfully from reservations reminder sms');

        date_default_timezone_set('Asia/Riyadh');
//        date_default_timezone_set('Africa/Cairo');

        $date = Carbon::now()->addDays(2)->format('Y-m-d');

        $reservations = DB::table('hotel_reservations')
            ->where('hotel_reservations.status', '=', 'confirmed')
            ->whereNull('hotel_reservations.deleted_at')
            ->whereNotNull('hotel_reservations.user_id')
            ->where('hotel_reservations.going_date', '=', $date)
            ->join('users', 'users.id', '=', 'hotel_reservations.user_id')
            ->join('hotels', 'hotels.id', '=', 'hotel_reservations.hotel_id')
            ->whereNull('hotels.deleted_at')
            ->join('hotel_descriptions as hotel_desc', 'hotels.id', '=', 'hotel_desc.hotel_id')
            ->where('hotel_desc.language_id', currentLanguage()->id)
            ->select([
                'hotel_reservations.id',
                'hotel_reservations.going_date',
                'users.name as user_name',
                'users.id as user_id',
                'users.device_token',
                'hotel_desc.name as hotel_name'
            ])->cursor();

        foreach ($reservations as $reservation) {
            $title = __('hotel_reservations.reservation_success_title');
            $title = str_replace('@@', $reservation->hotel_name, $title);
            $message = __('hotel_reservations.reservation_success_message');
            $message = str_replace_array('@@', [$reservation->user_name, $reservation->hotel_name, $reservation->going_date], $message);

            $userNotification = [
                'title' => $title,
                'message' => $message,
                'user_id' => $reservation->user_id,
                'reservation_id' => $reservation->id,
                'notification_type' => 'hotel',
            ];
            UsersNotification::create($userNotification);

            try{
                $notificationData['notification'] = [
                    'title' => $title,
                    'body' => $message,
                    'page_id' => $reservation->id,
                    'type' => 'hotel',
                    'vibrate' => 1,
                    'priority' => 'high',
                    'visibility'=> 1,
                    'foreground'=> true,
                    'no-cache' => 1,
                    'force-start' => 1,
                    'sound' => 'default'
                ];
                $target['to'] = $reservation->device_token['token'];
                Notification::send($notificationData, $target);
            } catch (\Exception $exception){}
        }
    }
}
