# 🚀 Dubai DLD Analytics API - Setup Guide

## Database Setup Required

Your MySQL database is not currently running. Here's how to set it up:

### Option 1: Install MySQL (Recommended)

#### Ubuntu/Debian:
```bash
sudo apt update
sudo apt install mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
sudo mysql_secure_installation
```

#### CentOS/RHEL:
```bash
sudo yum install mysql-server
sudo systemctl start mysqld
sudo systemctl enable mysqld
sudo mysql_secure_installation
```

#### macOS (with Homebrew):
```bash
brew install mysql
brew services start mysql
mysql_secure_installation
```

### Option 2: Use Docker (Quick Setup)

```bash
# Start MySQL with Docker
docker run --name dubai-mysql \
  -e MYSQL_ROOT_PASSWORD=Admin@123 \
  -e MYSQL_DATABASE=dubai_dld \
  -p 3306:3306 \
  -d mysql:8.0

# Wait for MySQL to start (30 seconds)
sleep 30

# Import the schema
docker exec -i dubai-mysql mysql -uroot -pAdmin@123 dubai_dld < 2.sql
```

### Option 3: Use XAMPP/WAMP/MAMP

1. Download and install XAMPP/WAMP/MAMP
2. Start MySQL service
3. Set root password to `Admin@123`
4. Create database `dubai_dld`
5. Import `2.sql` file

## After MySQL is Running

### 1. Configure Database Connection
```bash
./configure_db.sh
```

### 2. Install Node.js Dependencies
```bash
npm install
```

### 3. Start the API Server
```bash
npm run dev
```

### 4. Test the API
```bash
./test_api.sh
```

## Database Credentials

- **Host**: localhost
- **Port**: 3306
- **Username**: root
- **Password**: Admin@123
- **Database**: dubai_dld

## Verification Steps

### 1. Test MySQL Connection
```bash
mysql -h localhost -u root -pAdmin@123 -e "SELECT 1;"
```

### 2. Check Database Exists
```bash
mysql -h localhost -u root -pAdmin@123 -e "SHOW DATABASES LIKE 'dubai_dld';"
```

### 3. Check Tables
```bash
mysql -h localhost -u root -pAdmin@123 dubai_dld -e "SHOW TABLES;"
```

## Expected Tables

The database should contain these tables:
- `transactions` (218,518 records)
- `rents` (4,041 records)
- `projects` (355 records)
- `buildings` (16,322 records)
- `lands` (186,796 records)
- `valuations` (3,560 records)
- `brokers` (37,332 records)
- `developers` (from schema)

## Troubleshooting

### MySQL Connection Issues
1. **Service not running**: Start MySQL service
2. **Wrong password**: Reset MySQL root password
3. **Port conflict**: Check if port 3306 is available
4. **Firewall**: Allow MySQL connections

### Common Commands
```bash
# Start MySQL
sudo systemctl start mysql

# Stop MySQL
sudo systemctl stop mysql

# Restart MySQL
sudo systemctl restart mysql

# Check MySQL status
sudo systemctl status mysql

# Connect to MySQL
mysql -u root -p

# Reset root password
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Admin@123';"
```

## Once Database is Ready

### Quick Test
```bash
# 1. Configure database
./configure_db.sh

# 2. Install dependencies
npm install

# 3. Start server
npm run dev

# 4. Test API (in another terminal)
curl http://localhost:3000/health
```

### Full Test Suite
```bash
./test_api.sh
```

## Features Ready to Test

✅ **Context-Aware Queries**
- Q1: "Give me the last 6 months rental price trend for Business Bay"
- Q2: "Summarise by week" (refinement)
- Q3: "Apartments only" (filter)

✅ **All 10 Specific Queries**
- Project summaries
- Fast moving projects
- Off-plan uptick areas
- Commercial/Residential leasing
- BHK apartment prices

✅ **Chart.js Integration**
- Line charts for trends
- Bar charts for comparisons
- Pie charts for distributions
- Summary cards

## Next Steps

1. **Set up MySQL** using one of the options above
2. **Run the configuration script**: `./configure_db.sh`
3. **Start the API**: `npm run dev`
4. **Test everything**: `./test_api.sh`
5. **Open the dashboard**: `public/index.html`

---

**Status**: ⏳ Waiting for MySQL setup
**API**: ✅ Ready to run
**Database**: ⏳ Needs MySQL installation/startup

