<?php

namespace App\Console\Commands;

use App\Support\TicketStatus;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class AutoCloseResolvedTickets extends Command
{
    protected $signature = 'tickets:auto-close-resolved';

    protected $description = 'Automatically close resolved tickets after 24 hours of inactivity.';

    public function handle(): int
    {
        $cutoff = now()->subDay();

        $updated = DB::table('tickets')
            ->where('status', TicketStatus::RESOLVED)
            ->where('updated_at', '<=', $cutoff)
            ->update([
                'status' => TicketStatus::CLOSED,
                'updated_at' => now(),
            ]);

        $this->info("Auto-closed {$updated} resolved ticket(s).");

        return self::SUCCESS;
    }
}
