<?php
header('Content-Type: text/plain');
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/db.php';

try {
    $db = DB();
    $appNumber = 'AFFTEST' . time();
    
    echo "Attempting to insert test record in live DB...\n";
    
    $res = $db->insert('affiliation_applications', [
        'application_number'         => $appNumber,
        'institution_name'           => 'Test Institution',
        'institution_type'           => 'college',
        'established_year'           => 2020,
        'registration_number'        => 'REG12345',
        'total_staff'                => 10,
        'director_name'              => 'Test Director',
        'director_mobile'            => '1234567890',
        'director_email'             => 'test@example.com',
        'director_photo'             => 'uploads/affiliation/test_photo.jpg',
        'landline'                   => '12345',
        'address'                    => 'Test Address',
        'city'                       => 'Test City',
        'district'                   => 'Test District',
        'state'                      => 'Test State',
        'pincode'                    => '123456',
        'building_type'              => 'owned',
        'total_area'                 => '1000 sq ft',
        'number_of_classrooms'       => 5,
        'library_facility'           => 1,
        'computer_lab'               => 1,
        'internet_facility'          => 1,
        'courses_applied'            => json_encode(['Test Course'], JSON_UNESCAPED_UNICODE),
        'affiliation_fee_transaction_id' => 'TXN123',
        'application_status'         => 'submitted',
        'submitted_at'               => date('Y-m-d H:i:s'),
        'ip_address'                 => '127.0.0.1',
        
        'father_guardian_name'       => 'Guardian Name',
        'registered_address'         => 'Registered Address',
        'communication_address'      => 'Comm Address',
        'whatsapp_no'                => '1234567890',
        'registration_date'          => date('Y-m-d'),
        'pan_gst_no'                 => 'PANGST123',
        'identity_proof_no'          => 'ID123',
        'bank_name'                  => 'Test Bank',
        'bank_account_no'            => 'ACC123',
        'bank_ifsc'                  => 'IFSC123',
        'applied_for'                => 'Skill Development',
        'proposed_center_name'       => 'Proposed Center',
        'practical_lab_facility'     => 1,
        'computer_systems_available' => 1,
        'total_computers'            => 10,
        'power_backup_facility'      => 1,
        'clean_drinking_water'       => 1,
        'furniture_available'        => 1,
        'washroom_facility'          => 1,
        'safety_fire_extinguisher'   => 1,
        'faculty_details'            => json_encode([]),
        'courses_applied_details'    => json_encode([]),
        'documents_attached'         => 'Doc1, Doc2',
        'uploaded_documents'         => json_encode([])
    ]);
    
    echo "SUCCESS! Inserted test ID: " . $res . "\n";
    
    // Clean up test insert immediately
    if ($res) {
        $pdo = getDB();
        $pdo->exec("DELETE FROM `affiliation_applications` WHERE `application_number` = '$appNumber'");
        echo "CLEANUP: Deleted test record successfully.\n";
    }
} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
    echo $e->getTraceAsString() . "\n";
}
