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

echo "<h3>Checking admission_applications table structure...</h3>";

try {
    // Check if admission_applications table exists and get its structure
    $stmt = $pdo->query("SHOW COLUMNS FROM admission_applications");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h4>Current columns in admission_applications table:</h4>";
    echo "<ul>";
    foreach ($columns as $column) {
        $is_password = ($column['Field'] == 'password') ? " <strong>[PASSWORD COLUMN FOUND]</strong>" : "";
        echo "<li><strong>" . $column['Field'] . "</strong> - " . $column['Type'] . " - " . $column['Null'] . " - " . $column['Key'] . $is_password . "</li>";
    }
    echo "</ul>";
    
    // Check if password column exists
    $password_exists = false;
    foreach ($columns as $column) {
        if ($column['Field'] == 'password') {
            $password_exists = true;
            break;
        }
    }
    
    if ($password_exists) {
        echo "<p style='color: green; font-size: 1.2em;'><strong>✓ SUCCESS: The password column exists in the admission_applications table!</strong></p>";
        echo "<p>You should now be able to submit admission applications without the column error.</p>";
    } else {
        echo "<p style='color: red; font-size: 1.2em;'><strong>✗ PROBLEM: The password column is still missing from the admission_applications table.</strong></p>";
        
        // Try to add the column directly
        try {
            $pdo->exec("ALTER TABLE admission_applications ADD COLUMN password VARCHAR(255) DEFAULT '123456'");
            echo "<p style='color: green;'>✓ Password column added successfully!</p>";
        } catch (Exception $e) {
            echo "<p style='color: red;'>✗ Error adding password column: " . $e->getMessage() . "</p>";
            echo "<p>Please make sure the admission_applications table exists first.</p>";
        }
    }
    
} catch (Exception $e) {
    if (strpos($e->getMessage(), 'Unknown table') !== false) {
        echo "<p style='color: red; font-size: 1.2em;'><strong>✗ ERROR: The admission_applications table does not exist!</strong></p>";
        
        // Create the table with the correct structure
        $create_table_sql = "
        CREATE TABLE admission_applications (
            id INT AUTO_INCREMENT PRIMARY KEY,
            first_name VARCHAR(50) NOT NULL,
            last_name VARCHAR(50),
            father_name VARCHAR(100) NOT NULL,
            mother_name VARCHAR(100) NOT NULL,
            date_of_birth DATE NOT NULL,
            gender ENUM('male', 'female', 'other') NOT NULL,
            religion VARCHAR(20) NOT NULL,
            blood_group VARCHAR(10) NULL DEFAULT NULL,
            phone VARCHAR(20) NOT NULL,
            father_phone VARCHAR(20) NOT NULL,
            email VARCHAR(100),
            present_address TEXT,
            permanent_address TEXT,
            applying_for_class_id INT NOT NULL,
            previous_school VARCHAR(200),
            previous_class VARCHAR(100),
            application_no VARCHAR(20) UNIQUE,
            password VARCHAR(255) DEFAULT '123456',
            status ENUM('pending', 'approved', 'rejected', 'cancelled') DEFAULT 'pending',
            photo_path VARCHAR(255),
            applying_for_session VARCHAR(20),
            applying_for_group VARCHAR(50),
            admin_remarks TEXT,
            approved_by INT NULL,
            approved_at DATETIME NULL,
            selected_subjects TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            FOREIGN KEY (applying_for_class_id) REFERENCES classes(id)
        )";
        
        try {
            $pdo->exec($create_table_sql);
            echo "<p style='color: green;'>✓ admission_applications table created successfully with all required columns including password!</p>";
        } catch (Exception $create_error) {
            echo "<p style='color: red;'>✗ Error creating admission_applications table: " . $create_error->getMessage() . "</p>";
        }
    } else {
        echo "<p style='color: red;'>✗ Error accessing table: " . $e->getMessage() . "</p>";
    }
}
?>