<?php

namespace App\Console\Commands;

use App\Models\DeviceToken;
use Illuminate\Console\Command;

class PruneStaleDeviceTokens extends Command
{
    protected $signature = 'device-tokens:prune-stale
        {--days=180 : Number of days to retain device tokens since last refresh}';

    protected $description = 'Prune stale Expo push device tokens.';

    public function handle(): int
    {
        $days = filter_var($this->option('days'), FILTER_VALIDATE_INT, [
            'options' => ['min_range' => 1],
        ]);

        if ($days === false) {
            $this->error('The --days option must be an integer greater than zero.');

            return self::FAILURE;
        }

        $deleted = DeviceToken::query()
            ->where('updated_at', '<', now()->subDays($days))
            ->delete();

        $this->info("{$deleted} stale device token(s) pruned.");

        return self::SUCCESS;
    }
}
