<?php

namespace App\Console\Commands;

use App\Models\Audit\AuditLog;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class PruneAuditLogs extends Command
{
    protected $signature = 'audit:prune {--days=}';

    protected $description = 'Delete audit logs older than given days (default: config(audit.retention_days)).';

    public function handle(): int
    {
        $days = (int) ($this->option('days') ?? config('audit.retention_days', 395));
        $cutoff = Carbon::now()->subDays($days);

        $count = AuditLog::query()->where('created_at', '<', $cutoff)->delete();
        $this->info("Pruned {$count} audit log rows older than {$days} days.");

        return self::SUCCESS;
    }
}
