<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Offers\Console\Commands;

use Illuminate\Console\Command;
use Maestro\Core\Modules\Offers\Enums\OfferStatus;
use Maestro\Core\Modules\Offers\Models\Offer;

class SyncOfferExpiryCommand extends Command
{
    protected $signature = 'maestro:sync-offer-expiry';

    protected $description = 'Mark sent/viewed offers as expired when their valid_until date has passed.';

    public function handle(): int
    {
        $count = Offer::query()
            ->whereIn('status', [OfferStatus::Sent->value, OfferStatus::Viewed->value])
            ->whereNotNull('valid_until')
            ->whereDate('valid_until', '<', today())
            ->update(['status' => OfferStatus::Expired->value]);

        $this->components->info("Marked {$count} offer(s) as expired.");

        return self::SUCCESS;
    }
}
