<?php

namespace App\Console\Commands;

use App\Models\SelfPaced\Enrollment;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class DeregisterExpiredEnrollmentsCommand extends Command
{
    protected $signature = 'self-paced:deregister-expired';

    protected $description = 'Deregister enrollments whose access window has expired';

    public function handle(): int
    {
        $count = Enrollment::query()
            ->whereIn('status', ['active', 'completed'])
            ->whereNotNull('content_access_expires_at')
            ->where('content_access_expires_at', '<', now())
            ->update([
                'status' => 'deregistered',
                'updated_at' => now(),
            ]);

        $this->info("Deregistered $count expired enrollments");

        return Command::SUCCESS;
    }
}
