<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CleanupExpiredMailboxes extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mailboxes:cleanup';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Cleanup expired mailboxes and their associated data';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $mailboxes = \App\Models\Mailbox::where('expires_at', '<=', now())->get();
        $count = $mailboxes->count();

        foreach ($mailboxes as $mailbox) {
            $mailbox->delete(); // Triggers soft-delete and the 'deleted' event listener
        }

        $this->info("Cleaned up {$count} expired mailboxes.");
    }
}
