'use strict';
const { v4: uuidv4 } = require('uuid');

// Zone Definitions
const ZONES_DATA = [
    { code: 'NZ', name: 'North Zone', description: 'Northern India including NCR', states: ['JAMMU & KASHMIR', 'LADAKH', 'HIMACHAL PRADESH', 'PUNJAB', 'CHANDIGARH', 'UTTARAKHAND', 'HARYANA', 'DELHI', 'RAJASTHAN', 'UTTAR PRADESH'] },
    { code: 'SZ', name: 'South Zone', description: 'Southern India', states: ['KARNATAKA', 'TELANGANA', 'ANDHRA PRADESH', 'TAMIL NADU', 'KERALA', 'PUDUCHERRY', 'LAKSHADWEEP', 'ANDAMAN & NICOBAR'] },
    { code: 'EZ', name: 'East Zone', description: 'Eastern and North-Eastern India', states: ['BIHAR', 'JHARKHAND', 'ODISHA', 'WEST BENGAL', 'SIKKIM', 'ASSAM', 'MEGHALAYA', 'ARUNACHAL PRADESH', 'NAGALAND', 'MANIPUR', 'MIZORAM', 'TRIPURA'] },
    { code: 'WZ', name: 'West Zone', description: 'Western India', states: ['GUJARAT', 'MAHARASHTRA', 'GOA', 'DADRA & NAGAR HAVELI', 'DAMAN & DIU'] },
    { code: 'CZ', name: 'Central Zone', description: 'Central India', states: ['MADHYA PRADESH', 'CHHATTISGARH'] }
];

// Raw State Data
const STATES_DATA = [
    { id: 1, name: 'ANDHRA PRADESH', code: 'AP', country_id: 105 },
    { id: 2, name: 'ASSAM', code: 'AS', country_id: 105 },
    { id: 3, name: 'ARUNACHAL PRADESH', code: 'AR', country_id: 105 },
    { id: 4, name: 'BIHAR', code: 'BR', country_id: 105 },
    { id: 5, name: 'GUJARAT', code: 'GJ', country_id: 105 },
    { id: 6, name: 'HARYANA', code: 'HR', country_id: 105 },
    { id: 7, name: 'HIMACHAL PRADESH', code: 'HP', country_id: 105 },
    { id: 8, name: 'JAMMU & KASHMIR', code: 'JK', country_id: 105 },
    { id: 9, name: 'KARNATAKA', code: 'KA', country_id: 105 },
    { id: 10, name: 'KERALA', code: 'KL', country_id: 105 },
    { id: 11, name: 'MADHYA PRADESH', code: 'MP', country_id: 105 },
    { id: 12, name: 'MAHARASHTRA', code: 'MH', country_id: 105 },
    { id: 13, name: 'MANIPUR', code: 'MN', country_id: 105 },
    { id: 14, name: 'MEGHALAYA', code: 'ML', country_id: 105 },
    { id: 15, name: 'MIZORAM', code: 'MZ', country_id: 105 },
    { id: 16, name: 'NAGALAND', code: 'NL', country_id: 105 },
    { id: 17, name: 'ODISHA', code: 'OD', country_id: 105 },
    { id: 18, name: 'PUNJAB', code: 'PB', country_id: 105 },
    { id: 19, name: 'RAJASTHAN', code: 'RJ', country_id: 105 },
    { id: 20, name: 'SIKKIM', code: 'SK', country_id: 105 },
    { id: 21, name: 'TAMIL NADU', code: 'TN', country_id: 105 },
    { id: 22, name: 'TRIPURA', code: 'TR', country_id: 105 },
    { id: 23, name: 'UTTAR PRADESH', code: 'UP', country_id: 105 },
    { id: 24, name: 'WEST BENGAL', code: 'WB', country_id: 105 },
    { id: 25, name: 'DELHI', code: 'DL', country_id: 105 },
    { id: 26, name: 'GOA', code: 'GA', country_id: 105 },
    { id: 27, name: 'PUDUCHERRY', code: 'PY', country_id: 105 },
    { id: 28, name: 'LAKSHADWEEP', code: 'LD', country_id: 105 },
    { id: 29, name: 'DAMAN & DIU', code: 'DD', country_id: 105 },
    { id: 30, name: 'DADRA & NAGAR HAVELI', code: 'DN', country_id: 105 },
    { id: 31, name: 'CHANDIGARH', code: 'CH', country_id: 105 },
    { id: 32, name: 'ANDAMAN & NICOBAR', code: 'AN', country_id: 105 },
    { id: 33, name: 'UTTARAKHAND', code: 'UK', country_id: 105 },
    { id: 34, name: 'JHARKHAND', code: 'JH', country_id: 105 },
    { id: 35, name: 'CHHATTISGARH', code: 'CG', country_id: 105 },
    { id: 36, name: 'TELANGANA', code: 'TG', country_id: 105 },
    { id: 37, name: 'LADAKH', code: 'LA', country_id: 105 }
];

// Raw City Data
const CITIES_DATA = [
    // Bihar (ID 4)
    { name: 'Araria', state_id: 4 }, { name: 'Arwal', state_id: 4 }, { name: 'Aurangabad', state_id: 4 }, { name: 'Banka', state_id: 4 }, { name: 'Begusarai', state_id: 4 }, { name: 'Bhagalpur', state_id: 4 }, { name: 'Bhojpur', state_id: 4 },
    { name: 'Buxar', state_id: 4 }, { name: 'Darbhanga', state_id: 4 }, { name: 'East Champaran', state_id: 4 }, { name: 'Gaya', state_id: 4 }, { name: 'Gopalganj', state_id: 4 }, { name: 'Jamui', state_id: 4 }, { name: 'Jehanabad', state_id: 4 },
    { name: 'Kaimur', state_id: 4 }, { name: 'Katihar', state_id: 4 }, { name: 'Khagaria', state_id: 4 }, { name: 'Kishanganj', state_id: 4 }, { name: 'Lakhisarai', state_id: 4 }, { name: 'Madhepura', state_id: 4 }, { name: 'Madhubani', state_id: 4 },
    { name: 'Munger', state_id: 4 }, { name: 'Muzaffarpur', state_id: 4 }, { name: 'Nalanda', state_id: 4 }, { name: 'Nawada', state_id: 4 }, { name: 'Patna', state_id: 4 }, { name: 'Purnia', state_id: 4 }, { name: 'Rohtas', state_id: 4 },
    { name: 'Saharsa', state_id: 4 }, { name: 'Samastipur', state_id: 4 }, { name: 'Saran', state_id: 4 }, { name: 'Sheikhpura', state_id: 4 }, { name: 'Sheohar', state_id: 4 }, { name: 'Sitamarhi', state_id: 4 }, { name: 'Siwan', state_id: 4 },
    { name: 'Supaul', state_id: 4 }, { name: 'Vaishali', state_id: 4 }, { name: 'West Champaran', state_id: 4 },
    // Chhattisgarh (ID 35)
    { name: 'Balod', state_id: 35 }, { name: 'Baloda Bazar', state_id: 35 }, { name: 'Balrampur', state_id: 35 }, { name: 'Bastar', state_id: 35 }, { name: 'Bemetara', state_id: 35 }, { name: 'Bijapur', state_id: 35 }, { name: 'Bilaspur', state_id: 35 },
    { name: 'Dantewada', state_id: 35 }, { name: 'Dhamtari', state_id: 35 }, { name: 'Durg', state_id: 35 }, { name: 'Gariaband', state_id: 35 }, { name: 'Gaurela-Pendra-Marwahi', state_id: 35 }, { name: 'Janjgir-Champa', state_id: 35 }, { name: 'Jashpur', state_id: 35 },
    { name: 'Kabirdham', state_id: 35 }, { name: 'Kanker', state_id: 35 }, { name: 'Khairagarh-Chhuikhadan-Gandai', state_id: 35 }, { name: 'Kondagaon', state_id: 35 }, { name: 'Korba', state_id: 35 }, { name: 'Koriya', state_id: 35 }, { name: 'Mahasamund', state_id: 35 },
    { name: 'Manendragarh-Chirmiri-Bharatpur', state_id: 35 }, { name: 'Mohla-Manpur-Ambagarh Chowki', state_id: 35 }, { name: 'Mungeli', state_id: 35 }, { name: 'Narayanpur', state_id: 35 }, { name: 'Raigarh', state_id: 35 }, { name: 'Raipur', state_id: 35 },
    { name: 'Rajnandgaon', state_id: 35 }, { name: 'Sakti', state_id: 35 }, { name: 'Sarangarh-Bilaigarh', state_id: 35 }, { name: 'Sukma', state_id: 35 }, { name: 'Surajpur', state_id: 35 }, { name: 'Surguja', state_id: 35 },
    { name: 'Diu', state_id: 29 }, { name: 'Daman', state_id: 29 }, { name: 'Central Delhi', state_id: 25 }, { name: 'East Delhi', state_id: 25 },
    { name: 'New Delhi', state_id: 25 }, { name: 'North Delhi', state_id: 25 }, { name: 'North East Delhi', state_id: 25 }, { name: 'North West Delhi', state_id: 25 }, { name: 'South Delhi', state_id: 25 }, { name: 'South West Delhi', state_id: 25 },
    { name: 'West Delhi', state_id: 25 }, { name: 'North Goa', state_id: 26 }, { name: 'South Goa', state_id: 26 },
    // Gujarat (ID 5)
    { name: 'Ahmedabad', state_id: 5 }, { name: 'Amreli', state_id: 5 }, { name: 'Anand', state_id: 5 }, { name: 'Aravalli', state_id: 5 }, { name: 'Banaskantha', state_id: 5 }, { name: 'Bharuch', state_id: 5 }, { name: 'Bhavnagar', state_id: 5 },
    { name: 'Botad', state_id: 5 }, { name: 'Chhota Udaipur', state_id: 5 }, { name: 'Dahod', state_id: 5 }, { name: 'Dang', state_id: 5 }, { name: 'Devbhoomi Dwarka', state_id: 5 }, { name: 'Gandhinagar', state_id: 5 }, { name: 'Gir Somnath', state_id: 5 },
    { name: 'Jamnagar', state_id: 5 }, { name: 'Junagadh', state_id: 5 }, { name: 'Kheda', state_id: 5 }, { name: 'Kutch', state_id: 5 }, { name: 'Mahisagar', state_id: 5 }, { name: 'Mehsana', state_id: 5 }, { name: 'Morbi', state_id: 5 },
    { name: 'Narmada', state_id: 5 }, { name: 'Navsari', state_id: 5 }, { name: 'Panchmahal', state_id: 5 }, { name: 'Patan', state_id: 5 }, { name: 'Porbandar', state_id: 5 }, { name: 'Rajkot', state_id: 5 }, { name: 'Sabarkantha', state_id: 5 },
    { name: 'Surat', state_id: 5 }, { name: 'Surendranagar', state_id: 5 }, { name: 'Tapi', state_id: 5 }, { name: 'Vadodara', state_id: 5 }, { name: 'Valsad', state_id: 5 },
    // Haryana (ID 6)
    { name: 'Ambala', state_id: 6 }, { name: 'Bhiwani', state_id: 6 }, { name: 'Charkhi Dadri', state_id: 6 }, { name: 'Faridabad', state_id: 6 }, { name: 'Fatehabad', state_id: 6 }, { name: 'Gurugram', state_id: 6 }, { name: 'Hisar', state_id: 6 },
    { name: 'Jhajjar', state_id: 6 }, { name: 'Jind', state_id: 6 }, { name: 'Kaithal', state_id: 6 }, { name: 'Karnal', state_id: 6 }, { name: 'Kurukshetra', state_id: 6 }, { name: 'Mahendragarh', state_id: 6 }, { name: 'Nuh', state_id: 6 },
    { name: 'Palwal', state_id: 6 }, { name: 'Panchkula', state_id: 6 }, { name: 'Panipat', state_id: 6 }, { name: 'Rewari', state_id: 6 }, { name: 'Rohtak', state_id: 6 }, { name: 'Sirsa', state_id: 6 }, { name: 'Sonipat', state_id: 6 },
    { name: 'Yamunanagar', state_id: 6 },
    { name: 'Bilaspur', state_id: 7 }, { name: 'Chamba', state_id: 7 }, { name: 'Hamirpur', state_id: 7 }, { name: 'Kangra', state_id: 7 }, { name: 'Kinnaur', state_id: 7 }, { name: 'Kulu', state_id: 7 }, { name: 'Lahaul and Spiti', state_id: 7 },
    { name: 'Mandi', state_id: 7 }, { name: 'Shimla', state_id: 7 }, { name: 'Sirmaur', state_id: 7 }, { name: 'Solan', state_id: 7 }, { name: 'Una', state_id: 7 }, { name: 'Anantnag', state_id: 8 }, { name: 'Badgam', state_id: 8 },
    { name: 'Bandipore', state_id: 8 }, { name: 'Baramula', state_id: 8 }, { name: 'Doda', state_id: 8 }, { name: 'Jammu', state_id: 8 }, { name: 'Kargil', state_id: 37 }, { name: 'Kathua', state_id: 8 }, { name: 'Kupwara', state_id: 8 },
    { name: 'Leh', state_id: 37 }, { name: 'Poonch', state_id: 8 }, { name: 'Pulwama', state_id: 8 }, { name: 'Rajauri', state_id: 8 }, { name: 'Srinagar', state_id: 8 }, { name: 'Samba', state_id: 8 }, { name: 'Udhampur', state_id: 8 },
    { name: 'Bokaro', state_id: 34 }, { name: 'Chatra', state_id: 34 }, { name: 'Deoghar', state_id: 34 }, { name: 'Dhanbad', state_id: 34 }, { name: 'Dumka', state_id: 34 }, { name: 'Purba Singhbhum', state_id: 34 }, { name: 'Garhwa', state_id: 34 },
    { name: 'Giridih', state_id: 34 }, { name: 'Godda', state_id: 34 }, { name: 'Gumla', state_id: 34 }, { name: 'Hazaribagh', state_id: 34 }, { name: 'Koderma', state_id: 34 }, { name: 'Lohardaga', state_id: 34 }, { name: 'Pakur', state_id: 34 },
    { name: 'Palamu', state_id: 34 }, { name: 'Ranchi', state_id: 34 }, { name: 'Sahibganj', state_id: 34 }, { name: 'Seraikela and Kharsawan', state_id: 34 }, { name: 'Pashchim Singhbhum', state_id: 34 }, { name: 'Ramgarh', state_id: 34 },
    { name: 'Bidar', state_id: 9 }, { name: 'Belgaum', state_id: 9 }, { name: 'Bijapur', state_id: 9 }, { name: 'Bagalkot', state_id: 9 }, { name: 'Bellary', state_id: 9 }, { name: 'Bangalore Rural District', state_id: 9 },
    { name: 'Bangalore Urban District', state_id: 9 }, { name: 'Chamarajnagar', state_id: 9 }, { name: 'Chikmagalur', state_id: 9 }, { name: 'Chitradurga', state_id: 9 }, { name: 'Davanagere', state_id: 9 }, { name: 'Dharwad', state_id: 9 },
    { name: 'Dakshina Kannada', state_id: 9 }, { name: 'Gadag', state_id: 9 }, { name: 'Gulbarga', state_id: 9 }, { name: 'Hassan', state_id: 9 }, { name: 'Haveri District', state_id: 9 }, { name: 'Kodagu', state_id: 9 }, { name: 'Kolar', state_id: 9 },
    { name: 'Koppal', state_id: 9 }, { name: 'Mandya', state_id: 9 }, { name: 'Mysore', state_id: 9 }, { name: 'Raichur', state_id: 9 }, { name: 'Shimoga', state_id: 9 }, { name: 'Tumkur', state_id: 9 }, { name: 'Udupi', state_id: 9 },
    { name: 'Uttara Kannada', state_id: 9 }, { name: 'Ramanagara', state_id: 9 }, { name: 'Chikballapur', state_id: 9 }, { name: 'Yadagiri', state_id: 9 }, { name: 'Vijayanagara', state_id: 9 },
    { name: 'Alappuzha', state_id: 10 }, { name: 'Ernakulam', state_id: 10 },
    { name: 'Idukki', state_id: 10 }, { name: 'Kollam', state_id: 10 }, { name: 'Kannur', state_id: 10 }, { name: 'Kasaragod', state_id: 10 }, { name: 'Kottayam', state_id: 10 }, { name: 'Kozhikode', state_id: 10 }, { name: 'Malappuram', state_id: 10 },
    { name: 'Thrissur', state_id: 10 }, { name: 'Thiruvananthapuram', state_id: 10 }, { name: 'Wayanad', state_id: 10 },
    // Madhya Pradesh (ID 11)
    { name: 'Agar Malwa', state_id: 11 }, { name: 'Alirajpur', state_id: 11 }, { name: 'Anuppur', state_id: 11 }, { name: 'Ashok Nagar', state_id: 11 }, { name: 'Balaghat', state_id: 11 }, { name: 'Barwani', state_id: 11 }, { name: 'Betul', state_id: 11 },
    { name: 'Bhind', state_id: 11 }, { name: 'Bhopal', state_id: 11 }, { name: 'Burhanpur', state_id: 11 }, { name: 'Chhatarpur', state_id: 11 }, { name: 'Chhindwara', state_id: 11 }, { name: 'Damoh', state_id: 11 }, { name: 'Datia', state_id: 11 },
    { name: 'Dewas', state_id: 11 }, { name: 'Dhar', state_id: 11 }, { name: 'Dindori', state_id: 11 }, { name: 'Guna', state_id: 11 }, { name: 'Gwalior', state_id: 11 }, { name: 'Harda', state_id: 11 }, { name: 'Hoshangabad', state_id: 11 }, // Narmadapuram
    { name: 'Indore', state_id: 11 }, { name: 'Jabalpur', state_id: 11 }, { name: 'Jhabua', state_id: 11 }, { name: 'Katni', state_id: 11 }, { name: 'Khandwa', state_id: 11 }, { name: 'Khargone', state_id: 11 }, { name: 'Maihar', state_id: 11 },
    { name: 'Mandla', state_id: 11 }, { name: 'Mandsaur', state_id: 11 }, { name: 'Mauganj', state_id: 11 }, { name: 'Morena', state_id: 11 }, { name: 'Narsinghpur', state_id: 11 }, { name: 'Neemuch', state_id: 11 }, { name: 'Niwari', state_id: 11 },
    { name: 'Pandhurna', state_id: 11 }, { name: 'Panna', state_id: 11 }, { name: 'Raisen', state_id: 11 }, { name: 'Rajgarh', state_id: 11 }, { name: 'Ratlam', state_id: 11 }, { name: 'Rewa', state_id: 11 }, { name: 'Sagar', state_id: 11 },
    { name: 'Satna', state_id: 11 }, { name: 'Sehore', state_id: 11 }, { name: 'Seoni', state_id: 11 }, { name: 'Shahdol', state_id: 11 }, { name: 'Shajapur', state_id: 11 }, { name: 'Sheopur', state_id: 11 }, { name: 'Shivpuri', state_id: 11 },
    { name: 'Sidhi', state_id: 11 }, { name: 'Singrauli', state_id: 11 }, { name: 'Tikamgarh', state_id: 11 }, { name: 'Ujjain', state_id: 11 }, { name: 'Umaria', state_id: 11 }, { name: 'Vidisha', state_id: 11 },
    { name: 'Ahmednagar', state_id: 12 }, { name: 'Akola', state_id: 12 }, { name: 'Amrawati', state_id: 12 }, { name: 'Aurangabad', state_id: 12 }, { name: 'Bhandara', state_id: 12 }, { name: 'Beed', state_id: 12 }, { name: 'Buldhana', state_id: 12 },
    { name: 'Chandrapur', state_id: 12 }, { name: 'Dhule', state_id: 12 }, { name: 'Gadchiroli', state_id: 12 }, { name: 'Gondiya', state_id: 12 }, { name: 'Hingoli', state_id: 12 }, { name: 'Jalgaon', state_id: 12 }, { name: 'Jalna', state_id: 12 },
    { name: 'Kolhapur', state_id: 12 }, { name: 'Latur', state_id: 12 }, { name: 'Mumbai City', state_id: 12 }, { name: 'Mumbai suburban', state_id: 12 }, { name: 'Nandurbar', state_id: 12 }, { name: 'Nanded', state_id: 12 }, { name: 'Nagpur', state_id: 12 },
    { name: 'Nashik', state_id: 12 }, { name: 'Osmanabad', state_id: 12 }, { name: 'Parbhani', state_id: 12 }, { name: 'Pune', state_id: 12 }, { name: 'Raigad', state_id: 12 }, { name: 'Ratnagiri', state_id: 12 }, { name: 'Sindhudurg', state_id: 12 },
    { name: 'Sangli', state_id: 12 }, { name: 'Solapur', state_id: 12 }, { name: 'Satara', state_id: 12 }, { name: 'Thane', state_id: 12 }, { name: 'Wardha', state_id: 12 }, { name: 'Washim', state_id: 12 }, { name: 'Yavatmal', state_id: 12 },
    // Manipur (ID 13)
    { name: 'Bishnupur', state_id: 13 }, { name: 'Chandel', state_id: 13 }, { name: 'Churachandpur', state_id: 13 }, { name: 'Imphal East', state_id: 13 }, { name: 'Imphal West', state_id: 13 }, { name: 'Jiribam', state_id: 13 }, { name: 'Kakching', state_id: 13 },
    { name: 'Kamjong', state_id: 13 }, { name: 'Kangpokpi', state_id: 13 }, { name: 'Noney', state_id: 13 }, { name: 'Pherzawl', state_id: 13 }, { name: 'Senapati', state_id: 13 }, { name: 'Tamenglong', state_id: 13 }, { name: 'Tengnoupal', state_id: 13 },
    { name: 'Thoubal', state_id: 13 }, { name: 'Ukhrul', state_id: 13 },
    // Meghalaya (ID 14)
    { name: 'East Garo Hills', state_id: 14 }, { name: 'East Jaintia Hills', state_id: 14 }, { name: 'East Khasi Hills', state_id: 14 }, { name: 'Eastern West Khasi Hills', state_id: 14 }, { name: 'North Garo Hills', state_id: 14 }, { name: 'Ri-Bhoi', state_id: 14 },
    { name: 'South Garo Hills', state_id: 14 }, { name: 'South West Garo Hills', state_id: 14 }, { name: 'South West Khasi Hills', state_id: 14 }, { name: 'West Garo Hills', state_id: 14 }, { name: 'West Jaintia Hills', state_id: 14 }, { name: 'West Khasi Hills', state_id: 14 },
    // Mizoram (ID 15)
    { name: 'Aizawl', state_id: 15 }, { name: 'Champhai', state_id: 15 }, { name: 'Hnahthial', state_id: 15 }, { name: 'Khawzawl', state_id: 15 }, { name: 'Kolasib', state_id: 15 }, { name: 'Lawngtlai', state_id: 15 }, { name: 'Lunglei', state_id: 15 },
    { name: 'Mamit', state_id: 15 }, { name: 'Saiha', state_id: 15 }, { name: 'Saitual', state_id: 15 }, { name: 'Serchhip', state_id: 15 },
    // Nagaland (ID 16)
    { name: 'Chumoukedima', state_id: 16 }, { name: 'Dimapur', state_id: 16 }, { name: 'Kiphire', state_id: 16 }, { name: 'Kohima', state_id: 16 }, { name: 'Longleng', state_id: 16 }, { name: 'Mokokchung', state_id: 16 }, { name: 'Mon', state_id: 16 },
    { name: 'Niuland', state_id: 16 }, { name: 'Noklak', state_id: 16 }, { name: 'Peren', state_id: 16 }, { name: 'Phek', state_id: 16 }, { name: 'Shamator', state_id: 16 }, { name: 'Tseminyu', state_id: 16 }, { name: 'Tuensang', state_id: 16 },
    { name: 'Wokha', state_id: 16 }, { name: 'Zunheboto', state_id: 16 },
    { name: 'Angul', state_id: 17 }, { name: 'Boudh', state_id: 17 }, { name: 'Bhadrak', state_id: 17 }, { name: 'Bolangir', state_id: 17 }, { name: 'Bargarh', state_id: 17 }, { name: 'Baleswar', state_id: 17 }, { name: 'Cuttack', state_id: 17 },
    { name: 'Debagarh', state_id: 17 }, { name: 'Dhenkanal', state_id: 17 }, { name: 'Ganjam', state_id: 17 }, { name: 'Gajapati', state_id: 17 }, { name: 'Jharsuguda', state_id: 17 }, { name: 'Jajapur', state_id: 17 }, { name: 'Jagatsinghpur', state_id: 17 },
    { name: 'Khordha', state_id: 17 }, { name: 'Kendujhar', state_id: 17 }, { name: 'Kalahandi', state_id: 17 }, { name: 'Kandhamal', state_id: 17 }, { name: 'Koraput', state_id: 17 }, { name: 'Kendrapara', state_id: 17 }, { name: 'Malkangiri', state_id: 17 },
    { name: 'Mayurbhanj', state_id: 17 }, { name: 'Nabarangpur', state_id: 17 }, { name: 'Nuapada', state_id: 17 }, { name: 'Nayagarh', state_id: 17 }, { name: 'Puri', state_id: 17 }, { name: 'Rayagada', state_id: 17 }, { name: 'Sambalpur', state_id: 17 },
    { name: 'Subarnapur', state_id: 17 }, { name: 'Sundargarh', state_id: 17 }, { name: 'Karaikal', state_id: 27 }, { name: 'Mahe', state_id: 27 }, { name: 'Puducherry', state_id: 27 }, { name: 'Yanam', state_id: 27 },
    // Punjab (ID 18)
    { name: 'Amritsar', state_id: 18 }, { name: 'Barnala', state_id: 18 }, { name: 'Bathinda', state_id: 18 }, { name: 'Faridkot', state_id: 18 }, { name: 'Fatehgarh Sahib', state_id: 18 }, { name: 'Fazilka', state_id: 18 }, { name: 'Ferozepur', state_id: 18 },
    { name: 'Gurdaspur', state_id: 18 }, { name: 'Hoshiarpur', state_id: 18 }, { name: 'Jalandhar', state_id: 18 }, { name: 'Kapurthala', state_id: 18 }, { name: 'Ludhiana', state_id: 18 }, { name: 'Malerkotla', state_id: 18 }, { name: 'Mansa', state_id: 18 },
    { name: 'Moga', state_id: 18 }, { name: 'Mohali', state_id: 18 }, { name: 'Muktsar', state_id: 18 }, { name: 'Nawanshahr', state_id: 18 }, { name: 'Pathankot', state_id: 18 }, { name: 'Patiala', state_id: 18 }, { name: 'Rupnagar', state_id: 18 },
    { name: 'Sangrur', state_id: 18 }, { name: 'Tarn Taran', state_id: 18 },
    // Rajasthan (ID 19)
    { name: 'Ajmer', state_id: 19 }, { name: 'Alwar', state_id: 19 }, { name: 'Balotra', state_id: 19 }, { name: 'Banswara', state_id: 19 }, { name: 'Baran', state_id: 19 }, { name: 'Barmer', state_id: 19 }, { name: 'Beawar', state_id: 19 },
    { name: 'Bharatpur', state_id: 19 }, { name: 'Bhilwara', state_id: 19 }, { name: 'Bikaner', state_id: 19 }, { name: 'Bundi', state_id: 19 }, { name: 'Chittorgarh', state_id: 19 }, { name: 'Churu', state_id: 19 }, { name: 'Dausa', state_id: 19 },
    { name: 'Deeg', state_id: 19 }, { name: 'Didwana-Kuchaman', state_id: 19 }, { name: 'Dholpur', state_id: 19 }, { name: 'Dungarpur', state_id: 19 }, { name: 'Hanumangarh', state_id: 19 }, { name: 'Jaipur', state_id: 19 }, { name: 'Jaisalmer', state_id: 19 },
    { name: 'Jalore', state_id: 19 }, { name: 'Jhalawar', state_id: 19 }, { name: 'Jhunjhunu', state_id: 19 }, { name: 'Jodhpur', state_id: 19 }, { name: 'Karauli', state_id: 19 }, { name: 'Khairthal-Tijara', state_id: 19 }, { name: 'Kota', state_id: 19 },
    { name: 'Kotputli-Behror', state_id: 19 }, { name: 'Nagaur', state_id: 19 }, { name: 'Pali', state_id: 19 }, { name: 'Phalodi', state_id: 19 }, { name: 'Pratapgarh', state_id: 19 }, { name: 'Rajsamand', state_id: 19 }, { name: 'Salumbar', state_id: 19 },
    { name: 'Sawai Madhopur', state_id: 19 }, { name: 'Sikar', state_id: 19 }, { name: 'Sirohi', state_id: 19 }, { name: 'Sri Ganganagar', state_id: 19 }, { name: 'Tonk', state_id: 19 }, { name: 'Udaipur', state_id: 19 },
    // Sikkim (ID 20)
    { name: 'Gangtok', state_id: 20 }, { name: 'Gyalshing', state_id: 20 }, { name: 'Mangan', state_id: 20 }, { name: 'Namchi', state_id: 20 }, { name: 'Pakyong', state_id: 20 }, { name: 'Soreng', state_id: 20 },
    // Tamil Nadu (ID 21)
    { name: 'Ariyalur', state_id: 21 }, { name: 'Chengalpattu', state_id: 21 }, { name: 'Chennai', state_id: 21 }, { name: 'Coimbatore', state_id: 21 }, { name: 'Cuddalore', state_id: 21 }, { name: 'Dharmapuri', state_id: 21 }, { name: 'Dindigul', state_id: 21 },
    { name: 'Erode', state_id: 21 }, { name: 'Kallakurichi', state_id: 21 }, { name: 'Kanchipuram', state_id: 21 }, { name: 'Kanyakumari', state_id: 21 }, { name: 'Karur', state_id: 21 }, { name: 'Krishnagiri', state_id: 21 }, { name: 'Madurai', state_id: 21 },
    { name: 'Mayiladuthurai', state_id: 21 }, { name: 'Nagapattinam', state_id: 21 }, { name: 'Namakkal', state_id: 21 }, { name: 'Nilgiris', state_id: 21 }, { name: 'Perambalur', state_id: 21 }, { name: 'Pudukkottai', state_id: 21 }, { name: 'Ramanathapuram', state_id: 21 },
    { name: 'Ranipet', state_id: 21 }, { name: 'Salem', state_id: 21 }, { name: 'Sivagangai', state_id: 21 }, { name: 'Tenkasi', state_id: 21 }, { name: 'Thanjavur', state_id: 21 }, { name: 'Theni', state_id: 21 }, { name: 'Thoothukudi', state_id: 21 },
    { name: 'Tiruchirappalli', state_id: 21 }, { name: 'Tirunelveli', state_id: 21 }, { name: 'Tirupattur', state_id: 21 }, { name: 'Tiruppur', state_id: 21 }, { name: 'Tiruvallur', state_id: 21 }, { name: 'Tiruvannamalai', state_id: 21 },
    { name: 'Tiruvarur', state_id: 21 }, { name: 'Vellore', state_id: 21 }, { name: 'Viluppuram', state_id: 21 }, { name: 'Virudhunagar', state_id: 21 },
    { name: 'Bageshwar', state_id: 33 }, { name: 'Chamoli', state_id: 33 }, { name: 'Champawat', state_id: 33 }, { name: 'Dehradun', state_id: 33 }, { name: 'Haridwar', state_id: 33 }, { name: 'Nainital', state_id: 33 }, { name: 'Pauri Garhwal', state_id: 33 },
    { name: 'Pithoragharh', state_id: 33 }, { name: 'Rudraprayag', state_id: 33 }, { name: 'Tehri Garhwal', state_id: 33 }, { name: 'Udham Singh Nagar', state_id: 33 }, { name: 'Uttarkashi', state_id: 33 }, { name: 'Agra', state_id: 23 },
    // Uttar Pradesh (ID 23)
    { name: 'Agra', state_id: 23 }, { name: 'Aligarh', state_id: 23 }, { name: 'Ambedkar Nagar', state_id: 23 }, { name: 'Amethi', state_id: 23 }, { name: 'Amroha', state_id: 23 }, { name: 'Auraiya', state_id: 23 }, { name: 'Ayodhya', state_id: 23 },
    { name: 'Azamgarh', state_id: 23 }, { name: 'Badaun', state_id: 23 }, { name: 'Baghpat', state_id: 23 }, { name: 'Bahraich', state_id: 23 }, { name: 'Ballia', state_id: 23 }, { name: 'Balrampur', state_id: 23 }, { name: 'Banda', state_id: 23 },
    { name: 'Barabanki', state_id: 23 }, { name: 'Bareilly', state_id: 23 }, { name: 'Basti', state_id: 23 }, { name: 'Bhadohi', state_id: 23 }, { name: 'Bijnor', state_id: 23 }, { name: 'Bulandshahr', state_id: 23 }, { name: 'Chandauli', state_id: 23 },
    { name: 'Chitrakoot', state_id: 23 }, { name: 'Deoria', state_id: 23 }, { name: 'Etah', state_id: 23 }, { name: 'Etawah', state_id: 23 }, { name: 'Farrukhabad', state_id: 23 }, { name: 'Fatehpur', state_id: 23 }, { name: 'Firozabad', state_id: 23 },
    { name: 'Gautam Buddha Nagar', state_id: 23 }, { name: 'Ghaziabad', state_id: 23 }, { name: 'Ghazipur', state_id: 23 }, { name: 'Gonda', state_id: 23 }, { name: 'Gorakhpur', state_id: 23 }, { name: 'Hamirpur', state_id: 23 }, { name: 'Hapur', state_id: 23 },
    { name: 'Hardoi', state_id: 23 }, { name: 'Hathras', state_id: 23 }, { name: 'Jalaun', state_id: 23 }, { name: 'Jaunpur', state_id: 23 }, { name: 'Jhansi', state_id: 23 }, { name: 'Kannauj', state_id: 23 }, { name: 'Kanpur Dehat', state_id: 23 },
    { name: 'Kanpur Nagar', state_id: 23 }, { name: 'Kasganj', state_id: 23 }, { name: 'Kaushambi', state_id: 23 }, { name: 'Kushinagar', state_id: 23 }, { name: 'Lakhimpur Kheri', state_id: 23 }, { name: 'Lalitpur', state_id: 23 }, { name: 'Lucknow', state_id: 23 },
    { name: 'Maharajganj', state_id: 23 }, { name: 'Mahoba', state_id: 23 }, { name: 'Mainpuri', state_id: 23 }, { name: 'Mathura', state_id: 23 }, { name: 'Mau', state_id: 23 }, { name: 'Meerut', state_id: 23 }, { name: 'Mirzapur', state_id: 23 },
    { name: 'Moradabad', state_id: 23 }, { name: 'Muzaffarnagar', state_id: 23 }, { name: 'Pilibhit', state_id: 23 }, { name: 'Pratapgarh', state_id: 23 }, { name: 'Prayagraj', state_id: 23 }, { name: 'Rae Bareli', state_id: 23 }, { name: 'Rampur', state_id: 23 },
    { name: 'Saharanpur', state_id: 23 }, { name: 'Sambhal', state_id: 23 }, { name: 'Sant Kabir Nagar', state_id: 23 }, { name: 'Shahjahanpur', state_id: 23 }, { name: 'Shamli', state_id: 23 }, { name: 'Shravasti', state_id: 23 }, { name: 'Siddharthnagar', state_id: 23 },
    { name: 'Sitapur', state_id: 23 }, { name: 'Sonbhadra', state_id: 23 }, { name: 'Sultanpur', state_id: 23 }, { name: 'Unnao', state_id: 23 }, { name: 'Varanasi', state_id: 23 },
    // Andhra Pradesh (ID 1)
    { name: 'Anantapur', state_id: 1 }, { name: 'Chittoor', state_id: 1 }, { name: 'East Godavari', state_id: 1 }, { name: 'Guntur', state_id: 1 }, { name: 'Krishna', state_id: 1 },
    { name: 'Kurnool', state_id: 1 }, { name: 'Nellore', state_id: 1 }, { name: 'Prakasam', state_id: 1 }, { name: 'Srikakulam', state_id: 1 }, { name: 'Visakhapatnam', state_id: 1 },
    { name: 'Vizianagaram', state_id: 1 }, { name: 'West Godavari', state_id: 1 }, { name: 'YSR Kadapa', state_id: 1 },
    // New AP Districts
    { name: 'Parvathipuram Manyam', state_id: 1 }, { name: 'Alluri Sitharama Raju', state_id: 1 }, { name: 'Anakapalli', state_id: 1 }, { name: 'Kakinada', state_id: 1 }, { name: 'Konaseema', state_id: 1 },
    { name: 'Eluru', state_id: 1 }, { name: 'NTR', state_id: 1 }, { name: 'Bapatla', state_id: 1 }, { name: 'Palnadu', state_id: 1 }, { name: 'Nandyal', state_id: 1 },
    { name: 'Sri Sathya Sai', state_id: 1 }, { name: 'Annamayya', state_id: 1 }, { name: 'Tirupati', state_id: 1 },
    // Telangana (ID 36)
    { name: 'Adilabad', state_id: 36 }, { name: 'Bhadradri Kothagudem', state_id: 36 }, { name: 'Hyderabad', state_id: 36 }, { name: 'Jagtial', state_id: 36 }, { name: 'Jangaon', state_id: 36 },
    { name: 'Jayashankar Bhupalpally', state_id: 36 }, { name: 'Jogulamba Gadwal', state_id: 36 }, { name: 'Kamareddy', state_id: 36 }, { name: 'Karimnagar', state_id: 36 }, { name: 'Khammam', state_id: 36 },
    { name: 'Komaram Bheem Asifabad', state_id: 36 }, { name: 'Mahabubabad', state_id: 36 }, { name: 'Mahbubnagar', state_id: 36 }, { name: 'Mancherial', state_id: 36 }, { name: 'Medak', state_id: 36 },
    { name: 'Medchal-Malkajgiri', state_id: 36 }, { name: 'Nagarkurnool', state_id: 36 }, { name: 'Nalgonda', state_id: 36 }, { name: 'Nirmal', state_id: 36 }, { name: 'Nizamabad', state_id: 36 },
    { name: 'Peddapalli', state_id: 36 }, { name: 'Rajanna Sircilla', state_id: 36 }, { name: 'Rangareddy', state_id: 36 }, { name: 'Sangareddy', state_id: 36 }, { name: 'Siddipet', state_id: 36 },
    { name: 'Suryapet', state_id: 36 }, { name: 'Vikarabad', state_id: 36 }, { name: 'Wanaparthy', state_id: 36 }, { name: 'Warangal (Rural)', state_id: 36 }, { name: 'Warangal (Urban)', state_id: 36 },
    { name: 'Yadadri Bhuvanagiri', state_id: 36 }, { name: 'Mulugu', state_id: 36 }, { name: 'Narayanpet', state_id: 36 },
    // West Bengal (ID 24)
    { name: 'Alipurduar', state_id: 24 }, { name: 'Bankura', state_id: 24 }, { name: 'Birbhum', state_id: 24 }, { name: 'Cooch Behar', state_id: 24 }, { name: 'Dakshin Dinajpur', state_id: 24 }, { name: 'Darjeeling', state_id: 24 }, { name: 'Hooghly', state_id: 24 },
    { name: 'Howrah', state_id: 24 }, { name: 'Jalpaiguri', state_id: 24 }, { name: 'Jhargram', state_id: 24 }, { name: 'Kalimpong', state_id: 24 }, { name: 'Kolkata', state_id: 24 }, { name: 'Malda', state_id: 24 }, { name: 'Murshidabad', state_id: 24 },
    { name: 'Nadia', state_id: 24 }, { name: 'North 24 Parganas', state_id: 24 }, { name: 'Paschim Bardhaman', state_id: 24 }, { name: 'Paschim Medinipur', state_id: 24 }, { name: 'Purba Bardhaman', state_id: 24 }, { name: 'Purba Medinipur', state_id: 24 },
    { name: 'Purulia', state_id: 24 }, { name: 'South 24 Parganas', state_id: 24 }, { name: 'Uttar Dinajpur', state_id: 24 },
    // Arunachal Pradesh (ID 3)
    { name: 'Anjaw', state_id: 3 }, { name: 'Changlang', state_id: 3 }, { name: 'Dibang Valley', state_id: 3 }, { name: 'East Kameng', state_id: 3 }, { name: 'East Siang', state_id: 3 }, { name: 'Kamle', state_id: 3 }, { name: 'Kra Daadi', state_id: 3 },
    { name: 'Kurung Kumey', state_id: 3 }, { name: 'Lepa Rada', state_id: 3 }, { name: 'Lohit', state_id: 3 }, { name: 'Longding', state_id: 3 }, { name: 'Lower Dibang Valley', state_id: 3 }, { name: 'Lower Siang', state_id: 3 }, { name: 'Lower Subansiri', state_id: 3 },
    { name: 'Namsai', state_id: 3 }, { name: 'Pakke Kessang', state_id: 3 }, { name: 'Papum Pare', state_id: 3 }, { name: 'Shi Yomi', state_id: 3 }, { name: 'Siang', state_id: 3 }, { name: 'Tawang', state_id: 3 }, { name: 'Tirap', state_id: 3 },
    { name: 'Upper Siang', state_id: 3 }, { name: 'Upper Subansiri', state_id: 3 }, { name: 'West Kameng', state_id: 3 }, { name: 'West Siang', state_id: 3 }, { name: 'Itanagar Capital Complex', state_id: 3 }, { name: 'Bichom', state_id: 3 }, { name: 'Keyi Panyor', state_id: 3 },
    // Assam (ID 2)
    { name: 'Baksa', state_id: 2 }, { name: 'Barpeta', state_id: 2 }, { name: 'Bongaigaon', state_id: 2 }, { name: 'Cachar', state_id: 2 }, { name: 'Charaideo', state_id: 2 }, { name: 'Chirang', state_id: 2 }, { name: 'Darrang', state_id: 2 },
    { name: 'Dhemaji', state_id: 2 }, { name: 'Dhubri', state_id: 2 }, { name: 'Dibrugarh', state_id: 2 }, { name: 'Dima Hasao', state_id: 2 }, { name: 'Goalpara', state_id: 2 }, { name: 'Golaghat', state_id: 2 }, { name: 'Hailakandi', state_id: 2 },
    { name: 'Jorhat', state_id: 2 }, { name: 'Kamrup', state_id: 2 }, { name: 'Kamrup Metropolitan', state_id: 2 }, { name: 'Karbi Anglong', state_id: 2 }, { name: 'Karimganj', state_id: 2 }, { name: 'Kokrajhar', state_id: 2 }, { name: 'Lakhimpur', state_id: 2 },
    { name: 'Majuli', state_id: 2 }, { name: 'Morigaon', state_id: 2 }, { name: 'Nagaon', state_id: 2 }, { name: 'Nalbari', state_id: 2 }, { name: 'Sivasagar', state_id: 2 }, { name: 'Sonitpur', state_id: 2 }, { name: 'South Salmara-Mankachar', state_id: 2 },
    { name: 'Tinsukia', state_id: 2 }, { name: 'Udalguri', state_id: 2 }, { name: 'West Karbi Anglong', state_id: 2 },
    // Tripura (ID 22)
    { name: 'Dhalai', state_id: 22 }, { name: 'Gomati', state_id: 22 }, { name: 'Khowai', state_id: 22 }, { name: 'North Tripura', state_id: 22 }, { name: 'Sepahijala', state_id: 22 }, { name: 'South Tripura', state_id: 22 }, { name: 'Unakoti', state_id: 22 },
    { name: 'West Tripura', state_id: 22 }
];

module.exports = {
    up: async (queryInterface, Sequelize) => {
        try {
            console.log('Starting migration...');

            // 1. Create Zones and maintain a map for state lookup
            const zoneMap = new Map(); // Name -> UUID
            const zoneInserts = ZONES_DATA.map(zone => {
                const id = uuidv4();
                zoneMap.set(zone.name, id);
                return {
                    id: id,
                    zoneCode: zone.code,
                    zoneName: zone.name,
                    description: zone.description,
                    isActive: true,
                    createdAt: new Date(),
                    updatedAt: new Date()
                };
            });

            // Insert Zones
            if (zoneInserts.length > 0) {
                await queryInterface.bulkInsert('zones', zoneInserts);
                console.log(`Inserted ${zoneInserts.length} zones`);
            }

            // 2. Prepare States with Zone IDs
            const stateMap = new Map(); // Legacy ID -> UUID (for city lookup)
            const stateInserts = STATES_DATA.map(state => {
                const id = uuidv4();
                // Store both ID and ZoneID for lookup
                stateMap.set(state.id, { id: id, zoneId: null }); // Will update zoneId below

                // Find which Zone this state belongs to
                let zoneId = null;
                for (const z of ZONES_DATA) {
                    if (z.states.includes(state.name)) {
                        zoneId = zoneMap.get(z.name);
                        // Update the map with the found zoneId
                        stateMap.get(state.id).zoneId = zoneId;
                        break;
                    }
                }

                if (!zoneId) {
                    console.warn(`Warning: State ${state.name} not mapped to any Zone`);
                }

                return {
                    id: id,
                    stateName: state.name,
                    zoneId: zoneId,
                    isActive: true,
                    createdAt: new Date(),
                    updatedAt: new Date()
                    // code and country_id not present in State.ts (based on my viewing)
                    // Let's re-verify State.ts.
                    // State.ts: stateName, zoneId, isActive. NO code, NO country_id.
                };
            });

            // Insert States
            if (stateInserts.length > 0) {
                await queryInterface.bulkInsert('states', stateInserts);
                console.log(`Inserted ${stateInserts.length} states`);
            }

            // 3. Prepare Districts (Cities) using State Map
            const districtInserts = CITIES_DATA.map(city => {
                const stateData = stateMap.get(city.state_id);
                if (!stateData) {
                    console.warn(`Warning: City ${city.name} refers to unknown State ID ${city.state_id}`);
                    return null;
                }
                const { id: stateUuid, zoneId } = stateData;

                return {
                    id: uuidv4(),
                    districtName: city.name,
                    districtName: city.name,
                    stateId: stateUuid,
                    zoneId: zoneId,
                    isActive: true,
                    createdAt: new Date(),
                    updatedAt: new Date()
                };
            }).filter(d => d !== null);

            // Insert Districts
            // Bulk insert in chunks to avoid packet size issues if list is huge (it is ~380 items which is fine, but good practice)
            if (districtInserts.length > 0) {
                await queryInterface.bulkInsert('districts', districtInserts);
                console.log(`Inserted ${districtInserts.length} districts`);
            }

            console.log('Migration completed successfully.');

        } catch (error) {
            console.error('Migration failed:', error);
            throw error;
        }
    },

    down: async (queryInterface, Sequelize) => {
        // Delete in reverse order of foreign key dependency
        // Delete in reverse order of foreign key dependency
        await queryInterface.bulkDelete('districts', null, {});
        await queryInterface.bulkDelete('states', null, {});
        await queryInterface.bulkDelete('zones', null, {});
    }
};
