<?php
// Test page for admission publish dates functionality
require_once 'config/database.php';

// Ensure table exists
try {
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS admission_publish_dates (
            id INT AUTO_INCREMENT PRIMARY KEY,
            start_date DATE NOT NULL,
            end_date DATE NOT NULL,
            academic_year VARCHAR(10) NOT NULL,
            is_active TINYINT(1) DEFAULT 0,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ");
    echo "Table 'admission_publish_dates' created or already exists.<br>";
} catch (Exception $e) {
    echo "Error creating table: " . $e->getMessage() . "<br>";
}

// Insert test data
try {
    // First, deactivate any existing active records
    $stmt = $pdo->prepare("UPDATE admission_publish_dates SET is_active = 0 WHERE is_active = 1");
    $stmt->execute();
    
    // Check if record already exists (by academic year)
    $stmt = $pdo->prepare("SELECT id FROM admission_publish_dates WHERE academic_year = ?");
    $stmt->execute([date('Y') . '-' . (date('Y') + 1)]);
    $existing = $stmt->fetch();
    
    if ($existing) {
        // Update existing record
        $stmt = $pdo->prepare("
            UPDATE admission_publish_dates 
            SET start_date = ?, end_date = ?, is_active = ?, updated_at = ?
            WHERE academic_year = ?
        ");
        $result = $stmt->execute([
            date('Y-m-d'),
            date('Y-m-d', strtotime('+30 days')),
            1,
            date('Y-m-d H:i:s'),
            date('Y') . '-' . (date('Y') + 1)
        ]);
    } else {
        // Insert new record
        $stmt = $pdo->prepare("
            INSERT INTO admission_publish_dates (start_date, end_date, academic_year, is_active) 
            VALUES (?, ?, ?, ?)
        ");
        $result = $stmt->execute([
            date('Y-m-d'),
            date('Y-m-d', strtotime('+30 days')),
            date('Y') . '-' . (date('Y') + 1),
            1
        ]);
    }
    
    if ($result) {
        echo "Test data inserted/updated successfully.<br>";
    } else {
        echo "Failed to insert/update test data.<br>";
    }
} catch (Exception $e) {
    echo "Error inserting/updating test data: " . $e->getMessage() . "<br>";
}

// Retrieve and display data
try {
    $stmt = $pdo->query("SELECT * FROM admission_publish_dates ORDER BY id DESC LIMIT 1");
    $dates = $stmt->fetch();
    
    if ($dates) {
        echo "<h3>Current Admission Dates:</h3>";
        echo "Start Date: " . $dates['start_date'] . "<br>";
        echo "End Date: " . $dates['end_date'] . "<br>";
        echo "Academic Year: " . $dates['academic_year'] . "<br>";
        echo "Is Active: " . ($dates['is_active'] ? 'Yes' : 'No') . "<br>";
    } else {
        echo "No admission dates found.<br>";
    }
} catch (Exception $e) {
    echo "Error retrieving data: " . $e->getMessage() . "<br>";
}
?>