<?php

namespace App\Console\Commands;

use App\Jobs\ProcessTestNotification;
use Illuminate\Console\Command;
use App\Libs\Services\FcmService;
use App\Services\UserDataService;
use Exception;

class TestFcmMessages extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'contemporanea:test-fcm-message';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Tests the sending of an FCM message to a specific device';
    protected FcmService $fcmService;
    protected UserDataService $userDataService;
    protected $topic      = 'new-exhibitions'; //ProcessTestNotification::TOPIC;


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(FcmService $fcmService, UserDataService $userDataService)
    {
        parent::__construct();
        $this->fcmService      = $fcmService;
        $this->userDataService = $userDataService;
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        if (!FcmService::areFcmNotificationsActive()) {
            $this->error('FCM messaging is not active');
            return;
        }

        if ($this->confirm('Do you want to provide an email to access the possible device tokens?')) {

            $email  = $this->ask('Please provide the user email.');
            $tokens = $this->userDataService->getUserDeviceTokensByEmail($email);
            if (!empty($tokens)) {
                $this->info('Copy a token to use, you will be asked for it in the next step.');
                $this->table(
                    ['Tokens'],
                    [$tokens]
                );
            } else {
                $this->info('No tokens were found for the provided email');
                return 0;
            }
        }

        $deviceToken = $this->ask('Please provide the token to send the message to. An empty value will abort the operation');

        if (empty($deviceToken)) {
            $this->info('Aborted');
            return;
        }

	try {
		$this->info('subscribing');
            //make sute that the token is subscribed to this topic
		$resSubscribe = $this->fcmService->subscribeTokenToTopic($deviceToken, $this->topic);
$this->info('subscribed',  $resSubscribe);
		if ($resSubscribe) {
			$this->info('going to send message');
                ProcessTestNotification::dispatch();
                $this->info('An attempt was made to send a test message to the token: ' . $deviceToken);


                $this->info('Please whait a while for the test topic unscubscription process...');
                $bar = $this->output->createProgressBar(10);
                $bar->start();

                for ($i = 1; $i <= 10; $i++) {
                    sleep(1);
                    $bar->advance();
                }


                $bar->finish();
                $this->newLine();
                //unsubscribe from the topic since this is supposed to be a one shot notification
                $this->info('Unsubscribing from the topic since this is supposed to be a one shot notification');

                $this->fcmService->unsubscribeTokenFromTopic($deviceToken, $this->topic);
            }
        } catch (Exception $ex) {
            $this->error("An erro occurred: " . $ex->getMessage());
        }
    }
}
