<?php

namespace App\Console\Commands;

use App\Http\Controllers\CronController;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Throwable;

class RunDeviceDisconnectAllCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cron:run-device-disconnect-all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run device disconnect-all cron logic from CLI';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('Starting device disconnect-all cron');
        Log::info('CLI device disconnect-all cron started');

        try {
            app(CronController::class)->device_wise_disconnect_all();
            $this->info('Completed device disconnect-all cron');
            Log::info('CLI device disconnect-all cron completed');
            return self::SUCCESS;
        } catch (Throwable $e) {
            $this->error('Device disconnect-all cron failed: '.$e->getMessage());
            Log::error('CLI device disconnect-all cron failed: '.$e->getMessage());
            return self::FAILURE;
        }
    }
}
