<?php

namespace App\Console\Commands;

use App\Models\Offerwall;
use App\Support\OfferCurator;
use App\Support\ProviderOfferImporter;
use Illuminate\Console\Command;

class SyncProviderOffers extends Command
{
    protected $signature = 'offers:sync {--provider=}';

    protected $description = 'Sync real offer cards from configured provider offer APIs';

    public function handle(ProviderOfferImporter $importer, OfferCurator $curator): int
    {
        $query = Offerwall::query()->orderBy('name');

        if ($provider = $this->option('provider')) {
            $query->where('name', 'like', '%'.$provider.'%');
        }

        $query->get()->each(function (Offerwall $offerwall) use ($importer) {
            $result = $importer->sync($offerwall);
            $this->line($offerwall->name.': '.$result['message']);
        });

        $summary = $curator->curate();
        $this->info("Curation complete: {$summary['hot']} hot, {$summary['featured']} featured.");

        return 0;
    }
}
