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

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

// Check if password column exists in admission_applications table
try {
    $stmt = $pdo->query("SHOW COLUMNS FROM admission_applications LIKE 'password'");
    $column_exists = $stmt->fetch();

    if ($column_exists) {
        echo "<p style='color: green;'>✓ Password column already exists in admission_applications table.</p>";
    } else {
        echo "<p style='color: red;'>✗ Password column is missing from admission_applications table.</p>";
        
        // Add the password column to the table
        $pdo->exec("ALTER TABLE admission_applications ADD COLUMN password VARCHAR(255) DEFAULT '123456'");
        
        echo "<p style='color: green;'>✓ Password column added successfully to admission_applications table.</p>";
        echo "<p>Column added with default value '123456' for existing records.</p>";
    }
    
    echo "<p style='color: green;'>✅ Database fix completed! You should now be able to submit admission applications without the 'Unknown column' error.</p>";
    
} catch (Exception $e) {
    if (strpos($e->getMessage(), 'Unknown table') !== false) {
        echo "<p style='color: red;'>✗ The admission_applications table does not exist.</p>";
        
        // Create the admission_applications table with proper 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.</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 checking table: " . $e->getMessage() . "</p>";
    }
}
?>