<?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();

        if ($expired->isEmpty()) {
            $this->info('No enrollments past their end date.');
            return self::SUCCESS;
        }

        // Log only — never auto-remove participants. Only admin action can cancel/expire.
        $this->info("Found {$expired->count()} enrollment(s) past end_date (no action taken — admin must handle manually):");
        foreach ($expired as $enrollment) {
            $name = $enrollment->participant?->person?->name_ar ?? "#{$enrollment->participant_id}";
            $group = $enrollment->group?->name_ar ?? "#{$enrollment->training_group_id}";
            $this->line("  - {$name} in {$group} (end_date: {$enrollment->end_date->format('Y-m-d')})");
        }

        return self::SUCCESS;
    }
}
