<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;

/**
 * BatchScheduleController — Frontend batch schedule page
 */
class BatchScheduleController extends Controller
{
    /**
     * Show upcoming batch schedules listing.
     */
    public function index(): void
    {
        $batchModel = $this->model('BatchSchedule');

        // Auto-update statuses based on dates
        try {
            $batchModel->autoUpdateStatuses();
        } catch (\Throwable $e) {
            // Ignore
        }

        // Get course filter
        $courseFilter = (int) ($_GET['course'] ?? 0);

        if ($courseFilter > 0) {
            $batches = $batchModel->getByCourse($courseFilter);
        } else {
            $batches = $batchModel->getUpcoming(50);
        }

        // Get courses for filter dropdown
        $courses = [];
        try {
            $courseModel = $this->model('Course');
            $courses = $courseModel->where('active', 1);
        } catch (\Throwable $e) {
            // Ignore
        }

        $this->view('batch/index', [
            'batches'       => $batches,
            'courses'       => $courses,
            'courseFilter'  => $courseFilter,
            'currentPage'   => 'batch-schedule',
            'title'         => 'Upcoming Batches — Gmind Institute',
        ]);
    }
}
