# Backend Setup Guide - EV Seminar Registration System

Spring Boot 3.4.4 + Java 21 + MySQL + Gmail SMTP

---

## Prerequisites

| Tool | Version | Download |
|------|---------|----------|
| Java JDK | 21+ | https://adoptium.net/ |
| Maven | 3.9+ | https://maven.apache.org/download.cgi |
| MySQL | 8.0+ | Via DBngin or https://dev.mysql.com/downloads/ |
| TablePlus (optional) | Latest | https://tableplus.com/ |

### Verify installations
```bash
java -version      # should show 21+
mvn -version       # should show 3.9+
```

---

## Step 1: Database Setup

### 1.1 Start MySQL Server
- Open **DBngin** and start your local MySQL server on port **3306**

### 1.2 Create Database & Tables
- Open **TablePlus** and connect to:
  - Host: `127.0.0.1`
  - Port: `3306`
  - User: `root`
  - Password: *(leave empty)*

- Open a new SQL query tab, copy the entire contents of `database/schema.sql` and execute it

### 1.3 Verify
Run this query in TablePlus to confirm:
```sql
USE ev_seminar_db;
SHOW TABLES;
```
You should see 5 tables: `customer`, `verification_code`, `vehicle`, `seminar`, `registration`

Also check seed data:
```sql
SELECT * FROM vehicle;   -- should have 5 vehicles
SELECT * FROM seminar;   -- should have 5 seminars
```

---

## Step 2: Gmail SMTP Setup (for sending emails)

### 2.1 Enable 2-Step Verification
1. Go to https://myaccount.google.com/security
2. Turn on **2-Step Verification** if not already enabled

### 2.2 Generate App Password
1. Go to https://myaccount.google.com/apppasswords
2. App name: `EV Seminar System`
3. Click **Create**
4. Copy the 16-character password (e.g. `abcd efgh ijkl mnop`)

### 2.3 Update application.properties
Open `backend/src/main/resources/application.properties` and replace:
```properties
spring.mail.username=YOUR_GMAIL@gmail.com
spring.mail.password=YOUR_APP_PASSWORD
```
With your actual Gmail and the 16-character App Password (remove spaces):
```properties
spring.mail.username=yourname@gmail.com
spring.mail.password=abcdefghijklmnop
```

> **IMPORTANT:** Do NOT commit your real Gmail credentials to git. Consider using environment variables for production.

---

## Step 3: Run the Backend

### Option A: Using Maven CLI
```bash
cd ev-seminar-system/backend
mvn spring-boot:run
```

### Option B: Using IDE (IntelliJ IDEA)
1. Open `ev-seminar-system/backend` as a project
2. Wait for Maven to download dependencies
3. Run `EvSeminarApplication.java` (the main class)

### Verify Backend is Running
Once started, you should see:
```
Started EvSeminarApplication in X.XX seconds
```

Test the API:
```bash
# Health check - should return vehicle list
curl http://localhost:8080/api/vehicles

# Or open in browser:
# http://localhost:8080/api/vehicles
```

---

## Step 4: Verify API Endpoints

### Public Endpoints (no auth required)
| Method | URL | Expected |
|--------|-----|----------|
| GET | `http://localhost:8080/api/vehicles` | 5 vehicles JSON |
| GET | `http://localhost:8080/api/vehicles/1` | Vehicle detail |
| GET | `http://localhost:8080/api/seminars` | Upcoming seminars |
| POST | `http://localhost:8080/api/auth/register` | Register new user |
| POST | `http://localhost:8080/api/auth/login` | Login, returns JWT |

### Protected Endpoints (JWT required)
Add header: `Authorization: Bearer <your-jwt-token>`

| Method | URL | Expected |
|--------|-----|----------|
| GET | `http://localhost:8080/api/auth/me` | Current user info |
| GET | `http://localhost:8080/api/registrations` | My registrations |
| POST | `http://localhost:8080/api/registrations` | Register for seminar |

---

## Configuration Reference

File: `backend/src/main/resources/application.properties`

| Property | Default | Description |
|----------|---------|-------------|
| `server.port` | 8080 | Backend server port |
| `spring.datasource.url` | jdbc:mysql://127.0.0.1:3306/ev_seminar_db | MySQL connection |
| `spring.datasource.username` | root | MySQL username |
| `spring.datasource.password` | *(empty)* | MySQL password |
| `jwt.secret` | *(pre-set)* | JWT signing key |
| `jwt.expiration-ms` | 86400000 | JWT expiry (24 hours) |
| `spring.mail.username` | YOUR_GMAIL | Gmail address |
| `spring.mail.password` | YOUR_APP_PASSWORD | Gmail App Password |

---

## Troubleshooting

### "Communications link failure" on startup
- MySQL server is not running. Open DBngin and start the server.

### "Access denied for user 'root'"
- Check your MySQL password. If you set one, update `spring.datasource.password` in application.properties.

### "Table 'ev_seminar_db.customer' doesn't exist"
- You haven't run `schema.sql` yet. Execute it in TablePlus.

### "Could not resolve placeholder 'jwt.secret'"
- Make sure `application.properties` exists and is not corrupted.

### Emails not sending
- Check your Gmail App Password is correct (no spaces)
- Make sure 2-Step Verification is enabled on your Google account
- Check the console log for SMTP error messages

### Port 8080 already in use
- Kill the process using port 8080, or change `server.port` in application.properties
