<?php

namespace App\Console\Commands;

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

class RunGeneralCronTask extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cron:run-general {task : payment-clear|daily-report|ssl-payment-check}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run general cron tasks from CLI';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $task = (string) $this->argument('task');
        $controller = app(CronController::class);

        $this->info("Starting general cron task: {$task}");
        Log::info("CLI general cron task started: {$task}");

        try {
            switch ($task) {
                case 'payment-clear':
                    $controller->payment_clear();
                    break;
                case 'daily-report':
                    $controller->daily_report();
                    break;
                case 'ssl-payment-check':
                    $controller->ssl_comm_admin_payment_check();
                    break;
                default:
                    $this->error('Invalid task. Use: payment-clear, daily-report, ssl-payment-check');
                    return self::FAILURE;
            }

            $this->info("Completed general cron task: {$task}");
            Log::info("CLI general cron task completed: {$task}");
            return self::SUCCESS;
        } catch (Throwable $e) {
            $this->error('General cron task failed: '.$e->getMessage());
            Log::error("CLI general cron task failed ({$task}): ".$e->getMessage());
            return self::FAILURE;
        }
    }
}
