<?php

namespace App\Console\Commands;

use App\Models\IamPrincipal;
use App\Models\NotificationDetails;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Exception;
use DateTime;
use App\Helpers\onesignalhelper;
use App\Models\Subscriptions;
use Illuminate\Support\Facades\Log;

class SentScheduleNotification extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:sent-schedule-notification';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'It will send Recommendation to users Based on stored Data';

    /**
     * Execute the console command.
     */


     public function handle()
     {
         $getAllPushNotifications = NotificationDetails::where('is_schedule', 1)
             ->where('is_active', 1)
             ->orderByDesc('id')
             ->get();

         $currentDateTime = new DateTime();

         foreach ($getAllPushNotifications as $inAppNotificationItem) {

             $storedDateTime = new DateTime($inAppNotificationItem->delivery_schedule);

             $currentTime = $currentDateTime->format('Y-m-d H:i');
             $storedTime = $storedDateTime->format('Y-m-d H:i');

             if ($currentTime == $storedTime) {
                 $title = $inAppNotificationItem->type;
                 $description = $inAppNotificationItem->description;
                 $imagePath = $inAppNotificationItem->image;
                 $stateIds = json_decode($inAppNotificationItem->state_ids, true);
                 $userType = $inAppNotificationItem->user_type;

                 if ($userType == 1) {
                     // Subscribed users
                     $iamPrincipals = Subscriptions::select('iam_principal_xid')
                         ->where('next_payment_date', '>=', now())
                         ->get();
                     $iamPrincipalIds = $iamPrincipals->pluck('iam_principal_xid');
                     $users = IamPrincipal::whereIn('id', $iamPrincipalIds)
                         ->where('is_active', 1)
                         ->where('notification_status', 1)
                         ->where('principal_type_xid', 3)
                         ->whereIn('state_xid', $stateIds)
                         ->get();
                 } elseif ($userType == 2) {
                     // Unsubscribed users
                     $allPrincipalIds = IamPrincipal::where('principal_type_xid', 3)->pluck('id');
                     $subscribedIds = Subscriptions::select('iam_principal_xid')
                         ->where('next_payment_date', '>=', now())
                         ->pluck('iam_principal_xid');
                     $unsubscribedIds = $allPrincipalIds->diff($subscribedIds);
                     $users = IamPrincipal::whereIn('id', $unsubscribedIds)
                         ->where('is_active', 1)
                         ->where('notification_status', 1)
                         ->whereIn('state_xid', $stateIds)
                         ->get();
                 } elseif ($userType == 3) {
                     // Both subscribed and unsubscribed users
                     $users = IamPrincipal::where('is_active', 1)
                         ->where('notification_status', 1)
                         ->where('principal_type_xid', 3)
                         ->whereIn('state_xid', $stateIds)
                         ->get();
                 }

                 foreach ($users as $user) {
                     if ($user->one_signal_player_id) {
                         OneSignalHelper::sendNotificationApi(
                             $user->one_signal_player_id,
                             $title,
                             $description,
                             'Dashboard Notification',
                             $imagePath,
                             $id = null
                         );

                         Log::info("INAPP scheduled notification sent successfully to user ID: {$user->id}");
                         onesignalhelper::StoreNotificationDetails($user->id, 'Notification', $title, $imagePath);

                         $inAppNotificationItem->is_active = 0;
                         $inAppNotificationItem->save();
                     }
                 }
             }
         }
     }

}
