<?php
require_once 'config/database.php';

// Get all active classes
$stmt = $pdo->query('SELECT id, name, section FROM classes WHERE status = "active"');
$classes = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo 'Found ' . count($classes) . ' active classes' . "\n";

$current_date = date('Y-m-d');
$next_month = date('Y-m-d', strtotime('+30 days'));
$current_year = date('Y');
$academic_year = $current_year . '-' . ($current_year + 1);

foreach ($classes as $class) {
    // Check if this class already has active admission dates
    $stmt = $pdo->prepare('SELECT COUNT(*) FROM admission_publish_dates WHERE class_id = ? AND is_active = 1');
    $stmt->execute([$class['id']]);
    $count = $stmt->fetchColumn();
    
    if ($count == 0) {
        // Insert admission dates for this class
        $stmt = $pdo->prepare('INSERT INTO admission_publish_dates (start_date, end_date, academic_year, class_id, is_active) VALUES (?, ?, ?, ?, 1)');
        $stmt->execute([$current_date, $next_month, $academic_year, $class['id']]);
        
        echo 'Added admission dates for class: ' . $class['name'] . ($class['section'] ? ' - ' . $class['section'] : '') . "\n";
    } else {
        echo 'Admission dates already exist for class: ' . $class['name'] . ($class['section'] ? ' - ' . $class['section'] : '') . "\n";
    }
}

// Also make sure there's a global admission setting if no class-specific dates exist for some classes
echo "\nAdmission dates have been set up for all active classes.\n";
?>