const mysql = require('mysql2/promise');
require('dotenv').config();

const dbConfig = {
  host: process.env.DB_HOST || 'localhost',
  port: process.env.DB_PORT || 3306,
  user: process.env.DB_USER || 'root',
  password: process.env.DB_PASSWORD || process.env.DB_PASS || '',
  database: process.env.DB_NAME || 'cms_db',
  multipleStatements: true,
  charset: 'utf8mb4'
};

async function forceInsertAllLocations() {
  let connection;
  
  try {
    console.log('========================================');
    console.log('Force Inserting All Bangladesh Locations');
    console.log('========================================');
    console.log('');
    
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ Connected to database');
    console.log('');
    
    // Step 1: Ensure Country exists
    console.log('Step 1: Ensuring Bangladesh country exists...');
    await connection.query(`
      INSERT INTO countries (id, name, code, is_active) 
      VALUES (1, 'Bangladesh', 'BD', 1)
      ON DUPLICATE KEY UPDATE name = VALUES(name), code = VALUES(code);
    `);
    console.log('✅ Country ensured');
    console.log('');
    
    // Step 2: Ensure all Divisions exist
    console.log('Step 2: Ensuring all 8 divisions exist...');
    await connection.query(`
      INSERT INTO divisions (id, country_id, name, code, is_active) VALUES
      (1, 1, 'Dhaka', 'DHK', 1),
      (2, 1, 'Chittagong', 'CTG', 1),
      (3, 1, 'Rajshahi', 'RAJ', 1),
      (4, 1, 'Khulna', 'KHL', 1),
      (5, 1, 'Barisal', 'BAR', 1),
      (6, 1, 'Sylhet', 'SYL', 1),
      (7, 1, 'Rangpur', 'RAN', 1),
      (8, 1, 'Mymensingh', 'MYM', 1)
      ON DUPLICATE KEY UPDATE name = VALUES(name), code = VALUES(code);
    `);
    console.log('✅ All 8 divisions ensured');
    console.log('');
    
    // Step 3: Clear and insert all Districts
    console.log('Step 3: Clearing existing districts...');
    await connection.query('DELETE FROM districts');
    console.log('✅ Districts cleared');
    console.log('');
    
    console.log('Step 4: Inserting all 64 districts...');
    const districtsSQL = `
      INSERT INTO districts (division_id, name, code, is_active) VALUES
      -- Dhaka Division (13 districts)
      (1, 'Dhaka', 'DHK', 1),
      (1, 'Gazipur', 'GZP', 1),
      (1, 'Kishoreganj', 'KSG', 1),
      (1, 'Manikganj', 'MKG', 1),
      (1, 'Munshiganj', 'MSG', 1),
      (1, 'Narayanganj', 'NRG', 1),
      (1, 'Narsingdi', 'NSD', 1),
      (1, 'Tangail', 'TGL', 1),
      (1, 'Faridpur', 'FRP', 1),
      (1, 'Gopalganj', 'GPG', 1),
      (1, 'Madaripur', 'MDP', 1),
      (1, 'Rajbari', 'RJB', 1),
      (1, 'Shariatpur', 'SRP', 1),
      
      -- Chittagong Division (11 districts)
      (2, 'Chittagong', 'CTG', 1),
      (2, 'Cox''s Bazar', 'CXB', 1),
      (2, 'Bandarban', 'BDB', 1),
      (2, 'Rangamati', 'RGM', 1),
      (2, 'Khagrachhari', 'KGC', 1),
      (2, 'Feni', 'FNI', 1),
      (2, 'Lakshmipur', 'LKP', 1),
      (2, 'Noakhali', 'NKL', 1),
      (2, 'Chandpur', 'CDP', 1),
      (2, 'Comilla', 'CML', 1),
      (2, 'Brahmanbaria', 'BRB', 1),
      
      -- Rajshahi Division (8 districts)
      (3, 'Rajshahi', 'RAJ', 1),
      (3, 'Bogra', 'BGR', 1),
      (3, 'Joypurhat', 'JPH', 1),
      (3, 'Naogaon', 'NGN', 1),
      (3, 'Natore', 'NTR', 1),
      (3, 'Chapai Nawabganj', 'CNG', 1),
      (3, 'Pabna', 'PBN', 1),
      (3, 'Sirajganj', 'SRG', 1),
      
      -- Khulna Division (10 districts)
      (4, 'Khulna', 'KHL', 1),
      (4, 'Bagerhat', 'BGH', 1),
      (4, 'Chuadanga', 'CHD', 1),
      (4, 'Jashore', 'JSR', 1),
      (4, 'Jhenaidah', 'JND', 1),
      (4, 'Kushtia', 'KST', 1),
      (4, 'Magura', 'MGR', 1),
      (4, 'Meherpur', 'MHP', 1),
      (4, 'Narail', 'NRL', 1),
      (4, 'Satkhira', 'SKR', 1),
      
      -- Barisal Division (6 districts)
      (5, 'Barisal', 'BAR', 1),
      (5, 'Barguna', 'BGN', 1),
      (5, 'Bhola', 'BHL', 1),
      (5, 'Jhalokati', 'JHK', 1),
      (5, 'Patuakhali', 'PTK', 1),
      (5, 'Pirojpur', 'PRP', 1),
      
      -- Sylhet Division (4 districts)
      (6, 'Sylhet', 'SYL', 1),
      (6, 'Habiganj', 'HBG', 1),
      (6, 'Moulvibazar', 'MLV', 1),
      (6, 'Sunamganj', 'SNG', 1),
      
      -- Rangpur Division (8 districts)
      (7, 'Rangpur', 'RAN', 1),
      (7, 'Dinajpur', 'DNP', 1),
      (7, 'Gaibandha', 'GBD', 1),
      (7, 'Kurigram', 'KRM', 1),
      (7, 'Lalmonirhat', 'LMH', 1),
      (7, 'Nilphamari', 'NPM', 1),
      (7, 'Panchagarh', 'PCG', 1),
      (7, 'Thakurgaon', 'TKG', 1),
      
      -- Mymensingh Division (4 districts)
      (8, 'Mymensingh', 'MYM', 1),
      (8, 'Jamalpur', 'JMP', 1),
      (8, 'Netrokona', 'NTK', 1),
      (8, 'Sherpur', 'SHP', 1);
    `;
    
    await connection.query(districtsSQL);
    console.log('✅ All 64 districts inserted!');
    console.log('');
    
    // Step 4: Clear and insert all Thanas
    console.log('Step 5: Clearing existing thanas...');
    await connection.query('DELETE FROM thanas');
    console.log('✅ Thanas cleared');
    console.log('');
    
    console.log('Step 6: Inserting all thanas...');
    console.log('This may take a few moments...');
    console.log('');
    
    // Get district IDs first
    const [districts] = await connection.query(`
      SELECT id, name, division_id FROM districts ORDER BY division_id, name
    `);
    
    const districtMap = {};
    districts.forEach(d => {
      districtMap[d.name] = d.id;
    });
    
    // Insert thanas for each district
    const thanasData = [
      // Dhaka District
      { district: 'Dhaka', thanas: [
        'Dhanmondi', 'Gulshan', 'Banani', 'Uttara', 'Motijheel', 'Ramna', 'Tejgaon', 
        'Wari', 'Lalbagh', 'Kotwali', 'Sutrapur', 'Sabujbagh', 'Cantonment', 
        'Demra', 'Dohar', 'Dhamrai', 'Keraniganj', 'Nawabganj', 'Savar'
      ]},
      // Gazipur District
      { district: 'Gazipur', thanas: [
        'Gazipur Sadar', 'Kaliakair', 'Kapasia', 'Sreepur', 'Kaliganj'
      ]},
      // Narayanganj District
      { district: 'Narayanganj', thanas: [
        'Narayanganj Sadar', 'Bandar', 'Rupganj', 'Sonargaon', 'Araihazar'
      ]},
      // Tangail District
      { district: 'Tangail', thanas: [
        'Tangail Sadar', 'Basail', 'Bhuapur', 'Delduar', 'Dhanbari', 'Ghatail', 
        'Gopalpur', 'Kalihati', 'Madhupur', 'Mirzapur', 'Nagarpur', 'Sakhipur'
      ]},
      // Kishoreganj District
      { district: 'Kishoreganj', thanas: [
        'Kishoreganj Sadar', 'Austagram', 'Bajitpur', 'Bhairab', 'Hossainpur', 
        'Itna', 'Karimganj', 'Katiadi', 'Kuliarchar', 'Mithamain', 'Nikli', 
        'Pakundia', 'Tarail'
      ]},
      // Manikganj District
      { district: 'Manikganj', thanas: [
        'Manikganj Sadar', 'Daulatpur', 'Ghior', 'Harirampur', 'Saturia', 
        'Shibalaya', 'Singair'
      ]},
      // Munshiganj District
      { district: 'Munshiganj', thanas: [
        'Munshiganj Sadar', 'Gazaria', 'Lohajang', 'Sirajdikhan', 'Sreenagar', 'Tongibari'
      ]},
      // Narsingdi District
      { district: 'Narsingdi', thanas: [
        'Narsingdi Sadar', 'Belabo', 'Monohardi', 'Palash', 'Raipura', 'Shibpur'
      ]},
      // Faridpur District
      { district: 'Faridpur', thanas: [
        'Faridpur Sadar', 'Alfadanga', 'Bhanga', 'Boalmari', 'Charbhadrasan', 
        'Madhukhali', 'Nagarkanda', 'Sadarpur'
      ]},
      // Gopalganj District
      { district: 'Gopalganj', thanas: [
        'Gopalganj Sadar', 'Kashiani', 'Kotalipara', 'Muksudpur', 'Tungipara'
      ]},
      // Madaripur District
      { district: 'Madaripur', thanas: [
        'Madaripur Sadar', 'Kalkini', 'Rajoir', 'Shibchar'
      ]},
      // Rajbari District
      { district: 'Rajbari', thanas: [
        'Rajbari Sadar', 'Baliakandi', 'Goalandaghat', 'Pangsha'
      ]},
      // Shariatpur District
      { district: 'Shariatpur', thanas: [
        'Shariatpur Sadar', 'Bhedarganj', 'Damudya', 'Gosairhat', 'Naria', 'Zajira'
      ]},
      // Chittagong District
      { district: 'Chittagong', thanas: [
        'Chittagong Sadar', 'Hathazari', 'Raujan', 'Fatikchhari', 'Sandwip', 
        'Sitakunda', 'Mirsharai', 'Patiya', 'Banshkhali', 'Boalkhali', 'Anwara', 
        'Chandanaish', 'Satkania', 'Lohagara'
      ]},
      // Comilla District
      { district: 'Comilla', thanas: [
        'Comilla Sadar', 'Barura', 'Brahmanpara', 'Burichang', 'Chandina', 
        'Chauddagram', 'Daudkandi', 'Debidwar', 'Homna', 'Laksam', 'Monohargonj', 
        'Meghna', 'Muradnagar', 'Nangalkot', 'Titas'
      ]},
      // Cox's Bazar District
      { district: 'Cox\'s Bazar', thanas: [
        'Cox\'s Bazar Sadar', 'Chakaria', 'Kutubdia', 'Maheshkhali', 'Pekua', 
        'Ramu', 'Teknaf', 'Ukhia'
      ]},
      // Feni District
      { district: 'Feni', thanas: [
        'Feni Sadar', 'Chhagalnaiya', 'Daganbhuiyan', 'Parshuram', 'Sonagazi'
      ]},
      // Lakshmipur District
      { district: 'Lakshmipur', thanas: [
        'Lakshmipur Sadar', 'Raipur', 'Ramganj', 'Ramgati', 'Kamalnagar'
      ]},
      // Noakhali District
      { district: 'Noakhali', thanas: [
        'Noakhali Sadar', 'Begumganj', 'Chatkhil', 'Companiganj', 'Hatiya', 
        'Kabirhat', 'Senbagh', 'Sonaimuri', 'Subarnachar'
      ]},
      // Chandpur District
      { district: 'Chandpur', thanas: [
        'Chandpur Sadar', 'Faridganj', 'Haimchar', 'Hajiganj', 'Kachua', 
        'Matlab Dakshin', 'Matlab Uttar', 'Shahrasti'
      ]},
      // Brahmanbaria District
      { district: 'Brahmanbaria', thanas: [
        'Brahmanbaria Sadar', 'Ashuganj', 'Bancharampur', 'Bijoynagar', 'Kasba', 
        'Nabinagar', 'Nasirnagar', 'Sarail'
      ]},
      // Bandarban District
      { district: 'Bandarban', thanas: [
        'Bandarban Sadar', 'Alikadam', 'Lama', 'Naikhongchhari', 'Rowangchhari', 
        'Ruma', 'Thanchi'
      ]},
      // Rangamati District
      { district: 'Rangamati', thanas: [
        'Rangamati Sadar', 'Bagaichhari', 'Barkal', 'Jurachhari', 'Kaptai', 
        'Kawkhali', 'Langadu', 'Naniarchar', 'Rajasthali'
      ]},
      // Khagrachhari District
      { district: 'Khagrachhari', thanas: [
        'Khagrachhari Sadar', 'Dighinala', 'Lakshmichhari', 'Mahalchhari', 
        'Manikchhari', 'Matiranga', 'Panchhari', 'Ramgarh'
      ]},
      // Rajshahi District
      { district: 'Rajshahi', thanas: [
        'Rajshahi Sadar', 'Bagha', 'Bagmara', 'Charghat', 'Durgapur', 'Godagari', 
        'Mohanpur', 'Paba', 'Puthia', 'Tanore'
      ]},
      // Bogra District
      { district: 'Bogra', thanas: [
        'Bogra Sadar', 'Adamdighi', 'Dhunat', 'Dhupchanchia', 'Gabtali', 'Kahaloo', 
        'Nandigram', 'Sariakandi', 'Shajahanpur', 'Sherpur', 'Shibganj', 'Sonatala'
      ]},
      // Joypurhat District
      { district: 'Joypurhat', thanas: [
        'Joypurhat Sadar', 'Akkelpur', 'Kalai', 'Khetlal', 'Panchbibi'
      ]},
      // Naogaon District
      { district: 'Naogaon', thanas: [
        'Naogaon Sadar', 'Atrai', 'Badalgachhi', 'Dhamoirhat', 'Manda', 'Mahadebpur', 
        'Niamatpur', 'Patnitala', 'Porsha', 'Raninagar', 'Sapahar'
      ]},
      // Natore District
      { district: 'Natore', thanas: [
        'Natore Sadar', 'Bagatipara', 'Baraigram', 'Gurudaspur', 'Lalpur', 'Singra'
      ]},
      // Chapai Nawabganj District
      { district: 'Chapai Nawabganj', thanas: [
        'Chapai Nawabganj Sadar', 'Bholahat', 'Gomastapur', 'Nachole', 'Shibganj'
      ]},
      // Pabna District
      { district: 'Pabna', thanas: [
        'Pabna Sadar', 'Atgharia', 'Bera', 'Bhangura', 'Chatmohar', 'Faridpur', 
        'Ishwardi', 'Santhia', 'Sujanagar'
      ]},
      // Sirajganj District
      { district: 'Sirajganj', thanas: [
        'Sirajganj Sadar', 'Belkuchi', 'Chauhali', 'Kamarkhanda', 'Kazipur', 
        'Raiganj', 'Shahjadpur', 'Tarash', 'Ullahpara'
      ]},
      // Khulna District
      { district: 'Khulna', thanas: [
        'Khulna Sadar', 'Batiaghata', 'Dacope', 'Dumuria', 'Dighalia', 'Koyra', 
        'Paikgachha', 'Phultala', 'Rupsa', 'Terokhada'
      ]},
      // Jashore District
      { district: 'Jashore', thanas: [
        'Jashore Sadar', 'Abhaynagar', 'Bagherpara', 'Chaugachha', 'Jhikargacha', 
        'Keshabpur', 'Manirampur', 'Sharsha'
      ]},
      // Bagerhat District
      { district: 'Bagerhat', thanas: [
        'Bagerhat Sadar', 'Chitalmari', 'Fakirhat', 'Kachua', 'Mollahat', 'Mongla', 
        'Morrelganj', 'Rampal', 'Sarankhola'
      ]},
      // Chuadanga District
      { district: 'Chuadanga', thanas: [
        'Chuadanga Sadar', 'Alamdanga', 'Damurhuda', 'Jibannagar'
      ]},
      // Jhenaidah District
      { district: 'Jhenaidah', thanas: [
        'Jhenaidah Sadar', 'Harinakunda', 'Kaliganj', 'Kotchandpur', 'Maheshpur', 'Shailkupa'
      ]},
      // Kushtia District
      { district: 'Kushtia', thanas: [
        'Kushtia Sadar', 'Bheramara', 'Daulatpur', 'Khoksa', 'Kumarkhali', 'Mirpur'
      ]},
      // Magura District
      { district: 'Magura', thanas: [
        'Magura Sadar', 'Mohammadpur', 'Shalikha', 'Sreepur'
      ]},
      // Meherpur District
      { district: 'Meherpur', thanas: [
        'Meherpur Sadar', 'Gangni', 'Mujibnagar'
      ]},
      // Narail District
      { district: 'Narail', thanas: [
        'Narail Sadar', 'Kalia', 'Lohagara'
      ]},
      // Satkhira District
      { district: 'Satkhira', thanas: [
        'Satkhira Sadar', 'Assasuni', 'Debhata', 'Kalaroa', 'Kaliganj', 'Shyamnagar', 'Tala'
      ]},
      // Barisal District
      { district: 'Barisal', thanas: [
        'Barisal Sadar', 'Agailjhara', 'Babuganj', 'Bakerganj', 'Banaripara', 
        'Gaurnadi', 'Hizla', 'Mehendiganj', 'Muladi', 'Wazirpur'
      ]},
      // Barguna District
      { district: 'Barguna', thanas: [
        'Barguna Sadar', 'Amtali', 'Bamna', 'Betagi', 'Patharghata'
      ]},
      // Bhola District
      { district: 'Bhola', thanas: [
        'Bhola Sadar', 'Borhanuddin', 'Char Fasson', 'Daulatkhan', 'Lalmohan', 
        'Manpura', 'Tazumuddin'
      ]},
      // Jhalokati District
      { district: 'Jhalokati', thanas: [
        'Jhalokati Sadar', 'Kathalia', 'Nalchity', 'Rajapur'
      ]},
      // Patuakhali District
      { district: 'Patuakhali', thanas: [
        'Patuakhali Sadar', 'Bauphal', 'Dashmina', 'Dumki', 'Galachipa', 'Kalapara', 
        'Mirzaganj', 'Rangabali'
      ]},
      // Pirojpur District
      { district: 'Pirojpur', thanas: [
        'Pirojpur Sadar', 'Bhandaria', 'Kaukhali', 'Mathbaria', 'Nazirpur', 
        'Nesarabad', 'Zianagar'
      ]},
      // Sylhet District
      { district: 'Sylhet', thanas: [
        'Sylhet Sadar', 'Balaganj', 'Beanibazar', 'Bishwanath', 'Fenchuganj', 
        'Golapganj', 'Gowainghat', 'Jaintiapur', 'Kanaighat', 'Osmani Nagar', 'Zakiganj'
      ]},
      // Habiganj District
      { district: 'Habiganj', thanas: [
        'Habiganj Sadar', 'Ajmiriganj', 'Bahubal', 'Baniyachong', 'Chunarughat', 
        'Lakhai', 'Madhabpur', 'Nabiganj'
      ]},
      // Moulvibazar District
      { district: 'Moulvibazar', thanas: [
        'Moulvibazar Sadar', 'Barlekha', 'Juri', 'Kamalganj', 'Kulaura', 'Rajnagar', 'Sreemangal'
      ]},
      // Sunamganj District
      { district: 'Sunamganj', thanas: [
        'Sunamganj Sadar', 'Bishwamvarpur', 'Chhatak', 'Derai', 'Dharampasha', 
        'Dowarabazar', 'Jagannathpur', 'Jamalganj', 'Sulla', 'Tahirpur'
      ]},
      // Rangpur District
      { district: 'Rangpur', thanas: [
        'Rangpur Sadar', 'Badarganj', 'Gangachara', 'Kaunia', 'Mithapukur', 
        'Pirgacha', 'Pirganj', 'Taraganj'
      ]},
      // Dinajpur District
      { district: 'Dinajpur', thanas: [
        'Dinajpur Sadar', 'Birampur', 'Birganj', 'Biral', 'Bochaganj', 'Chirirbandar', 
        'Fulbari', 'Ghoraghat', 'Hakimpur', 'Kaharole', 'Khansama', 'Nawabganj', 'Parbatipur'
      ]},
      // Gaibandha District
      { district: 'Gaibandha', thanas: [
        'Gaibandha Sadar', 'Fulchhari', 'Gobindaganj', 'Palashbari', 'Sadullapur', 
        'Saghata', 'Sundarganj'
      ]},
      // Kurigram District
      { district: 'Kurigram', thanas: [
        'Kurigram Sadar', 'Bhurungamari', 'Char Rajibpur', 'Chilmari', 'Phulbari', 
        'Nageshwari', 'Rajarhat', 'Raomari', 'Ulipur'
      ]},
      // Lalmonirhat District
      { district: 'Lalmonirhat', thanas: [
        'Lalmonirhat Sadar', 'Aditmari', 'Hatibandha', 'Kaliganj', 'Patgram'
      ]},
      // Nilphamari District
      { district: 'Nilphamari', thanas: [
        'Nilphamari Sadar', 'Dimla', 'Domar', 'Jaldhaka', 'Kishoreganj', 'Saidpur'
      ]},
      // Panchagarh District
      { district: 'Panchagarh', thanas: [
        'Panchagarh Sadar', 'Atwari', 'Boda', 'Debiganj', 'Tetulia'
      ]},
      // Thakurgaon District
      { district: 'Thakurgaon', thanas: [
        'Thakurgaon Sadar', 'Baliadangi', 'Haripur', 'Pirganj', 'Ranisankail'
      ]},
      // Mymensingh District
      { district: 'Mymensingh', thanas: [
        'Mymensingh Sadar', 'Bhaluka', 'Dhobaura', 'Fulbaria', 'Gaffargaon', 
        'Gauripur', 'Haluaghat', 'Ishwarganj', 'Muktagachha', 'Nandail', 'Phulpur', 'Tarakanda'
      ]},
      // Jamalpur District
      { district: 'Jamalpur', thanas: [
        'Jamalpur Sadar', 'Baksiganj', 'Dewanganj', 'Islampur', 'Madarganj', 
        'Melandaha', 'Sarishabari'
      ]},
      // Netrokona District
      { district: 'Netrokona', thanas: [
        'Netrokona Sadar', 'Atpara', 'Barhatta', 'Durgapur', 'Khaliajuri', 
        'Kalmakanda', 'Kendua', 'Madan', 'Mohanganj', 'Purbadhala'
      ]},
      // Sherpur District
      { district: 'Sherpur', thanas: [
        'Sherpur Sadar', 'Jhenaigati', 'Nakla', 'Nalitabari', 'Sreebardi'
      ]}
    ];
    
    let totalThanas = 0;
    for (const item of thanasData) {
      const districtId = districtMap[item.district];
      if (!districtId) {
        console.log(`⚠️  Warning: District "${item.district}" not found, skipping...`);
        continue;
      }
      
      const thanaValues = item.thanas.map(t => {
        const code = t.substring(0, 3).toUpperCase().replace(/\s/g, '');
        return `(${districtId}, '${t.replace(/'/g, "''")}', '${code}', 1)`;
      }).join(',\n      ');
      
      const insertSQL = `
        INSERT INTO thanas (district_id, name, code, is_active) VALUES
        ${thanaValues};
      `;
      
      try {
        await connection.query(insertSQL);
        totalThanas += item.thanas.length;
        console.log(`✅ ${item.district}: ${item.thanas.length} thanas inserted`);
      } catch (error) {
        console.error(`❌ Error inserting thanas for ${item.district}:`, error.message);
      }
    }
    
    console.log('');
    console.log('========================================');
    console.log('✅ SUCCESS: All locations imported!');
    console.log('========================================');
    console.log('');
    
    // Final verification
    const [countryCount] = await connection.query('SELECT COUNT(*) as total FROM countries WHERE is_active = 1');
    const [divisionCount] = await connection.query('SELECT COUNT(*) as total FROM divisions WHERE is_active = 1');
    const [districtCount] = await connection.query('SELECT COUNT(*) as total FROM districts WHERE is_active = 1');
    const [thanaCount] = await connection.query('SELECT COUNT(*) as total FROM thanas WHERE is_active = 1');
    
    console.log('Final Verification:');
    console.log(`- Countries: ${countryCount[0].total}`);
    console.log(`- Divisions: ${divisionCount[0].total}`);
    console.log(`- Districts: ${districtCount[0].total}`);
    console.log(`- Thanas: ${thanaCount[0].total}`);
    console.log('');
    
    console.log('✅ All data successfully inserted into database!');
    
  } catch (error) {
    console.error('');
    console.error('========================================');
    console.error('❌ ERROR: Failed to insert locations!');
    console.error('========================================');
    console.error('');
    console.error('Error details:');
    console.error(error.message);
    if (error.stack) {
      console.error('Stack:', error.stack);
    }
    console.error('');
    process.exit(1);
  } finally {
    if (connection) {
      await connection.end();
      console.log('Database connection closed');
    }
  }
}

forceInsertAllLocations()
  .then(() => {
    console.log('Import completed successfully!');
    process.exit(0);
  })
  .catch((error) => {
    console.error('Fatal error:', error);
    process.exit(1);
  });

