<?php

namespace App\Console\Commands;

use App\Domain\Training\Models\Enrollment;
use Illuminate\Console\Command;

class DeactivateExpiredEnrollments extends Command
{
    protected $signature = 'enrollments:deactivate-expired';
    protected $description = 'Set active enrollments past their expiry date to expired status';

    public function handle(): int
    {
        $expired = Enrollment::where('status', 'active')
            ->whereNotNull('end_date')
            ->where('end_date', '<', now()->toDateString())
            ->get();

        $count = 0;
        foreach ($expired as $enrollment) {
            $enrollment->update(['status' => 'expired']);
            $count++;
        }

        $this->info("Deactivated {$count} expired enrollments.");
        return self::SUCCESS;
    }
}
