# Bangladesh Location Data Import Guide

এই guide-এ Bangladesh-এর সব Countries, Divisions, Districts, এবং Thanas-এর data database-এ import করার পদ্ধতি দেওয়া আছে।

## 📋 Data Summary

- **1 Country**: Bangladesh
- **8 Divisions**: Dhaka, Chittagong, Rajshahi, Khulna, Barisal, Sylhet, Rangpur, Mymensingh
- **64 Districts**: সব divisions-এর অধীনে সব districts
- **Major Thanas**: সব গুরুত্বপূর্ণ districts-এর major thanas

## 🚀 Import Methods

### Method 1: Node.js Script (Recommended)

এই method সব platform-এ কাজ করে (Windows, Linux, Mac)।

```bash
# Backend directory-তে যান
cd backend

# Script run করুন
node database/import_bangladesh_locations.js
```

**Environment Variables (Optional):**
```bash
# Windows
set DB_HOST=localhost
set DB_USER=root
set DB_PASS=your_password
set DB_NAME=cms_db
node database/import_bangladesh_locations.js

# Linux/Mac
export DB_HOST=localhost
export DB_USER=root
export DB_PASS=your_password
export DB_NAME=cms_db
node database/import_bangladesh_locations.js
```

### Method 2: MySQL Command Line (Windows)

```bash
# Backend/database directory-তে যান
cd backend/database

# Batch file run করুন
run_bangladesh_locations.bat
```

**Manual MySQL Command:**
```bash
mysql -h localhost -u root -p cms_db < insert_bangladesh_locations.sql
```

### Method 3: MySQL Command Line (Linux/Mac)

```bash
# Backend/database directory-তে যান
cd backend/database

# Script executable করুন
chmod +x run_bangladesh_locations.sh

# Script run করুন
./run_bangladesh_locations.sh
```

**Manual MySQL Command:**
```bash
mysql -h localhost -u root -p cms_db < insert_bangladesh_locations.sql
```

### Method 4: phpMyAdmin / MySQL Workbench

1. phpMyAdmin বা MySQL Workbench open করুন
2. `cms_db` database select করুন
3. SQL tab-এ যান
4. `insert_bangladesh_locations.sql` file open করুন
5. সব content copy করে paste করুন
6. Execute করুন

## ✅ Verification

Import করার পর verification করুন:

```sql
-- Check counts
SELECT COUNT(*) as total_countries FROM countries WHERE is_active = 1;
SELECT COUNT(*) as total_divisions FROM divisions WHERE is_active = 1;
SELECT COUNT(*) as total_districts FROM districts WHERE is_active = 1;
SELECT COUNT(*) as total_thanas FROM thanas WHERE is_active = 1;

-- Check some sample data
SELECT * FROM countries;
SELECT d.name as division, COUNT(DISTINCT dt.id) as districts_count 
FROM divisions d 
LEFT JOIN districts dt ON d.id = dt.division_id 
GROUP BY d.id, d.name;
```

## 📊 Expected Results

Import successful হলে:
- **Countries**: 1 (Bangladesh)
- **Divisions**: 8
- **Districts**: 64
- **Thanas**: 200+ (major thanas)

## 🔧 Troubleshooting

### Error: Access Denied
```bash
# Database credentials check করুন
# MySQL user-এর proper permissions আছে কিনা check করুন
```

### Error: Table doesn't exist
```bash
# প্রথমে migration file run করুন
# backend/database/migration_add_locations.sql
```

### Error: Duplicate entry
```bash
# Data already exists হলে
# ON DUPLICATE KEY UPDATE clause automatically handle করবে
# কোনো problem হবে না
```

### Error: Foreign key constraint fails
```bash
# Ensure countries are inserted first
# Then divisions
# Then districts
# Then thanas
# Script automatically handles this order
```

## 📝 Notes

1. **Duplicate Handling**: Script-এ `ON DUPLICATE KEY UPDATE` clause আছে, তাই multiple times run করলে problem হবে না
2. **Data Completeness**: এই script-এ major thanas আছে। যদি সব thanas দরকার হয়, তাহলে additional data add করতে হবে
3. **Active Status**: সব data `is_active = 1` দিয়ে insert হবে
4. **Codes**: প্রতিটি location-এর জন্য unique code দেওয়া আছে

## 🎯 Next Steps

Data import করার পর:

1. **Frontend-এ check করুন**: Location Management tab-এ যান এবং data দেখুন
2. **API Test করুন**: 
   ```bash
   GET /api/locations/countries
   GET /api/locations/divisions
   GET /api/locations/districts
   GET /api/locations/thanas
   ```
3. **Cascading Dropdowns Test করুন**: Division select করলে Districts load হয় কিনা check করুন

## 📞 Support

যদি কোনো problem হয়:
1. Error message check করুন
2. Database connection verify করুন
3. SQL file syntax check করুন
4. Database permissions verify করুন

---

**Created**: Bangladesh Location Data Import Script
**Version**: 1.0
**Last Updated**: 2024

