<?php

namespace App\Console\Commands;

use App\Services\AffiliateService;
use Illuminate\Console\Command;

class ProcessPendingAffiliateCommissionsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'affiliates:process-commissions';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Process pending affiliate commissions that have passed the delay period';

    /**
     * Execute the console command.
     */
    public function handle(AffiliateService $service): int
    {
        $this->info('Processing pending affiliate commissions...');

        try {
            $count = $service->processApprovedCommissions();

            $this->info("Processed {$count} commission(s).");

            return Command::SUCCESS;
        } catch (\Exception $e) {
            $this->error("Failed to process commissions: {$e->getMessage()}");

            return Command::FAILURE;
        }
    }
}
