<?php

namespace App\Console\Commands;

use App\Jobs\RefreshPlatformTokensJob;
use Illuminate\Console\Command;

class RefreshPlatformTokensCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'platforms:refresh-tokens
                            {--connection= : Optional specific connection ID to refresh}
                            {--days=10 : Number of days before expiration to trigger refresh}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Refresh expiring OAuth access tokens for active ad platform connections';

    /**
     * Execute the console command.
     */
    public function handle(): int
    {
        $connectionId = $this->option('connection') ? (int) $this->option('connection') : null;
        $days = (int) $this->option('days');

        $this->info("Dispatching platform token refresh (threshold: {$days} days)...");

        RefreshPlatformTokensJob::dispatchSync($connectionId, $days);

        $this->info('Platform token refresh completed.');

        return self::SUCCESS;
    }
}
