# Quick Start Guide - PineHealth Backend

This guide will help you get the Node.js/Express backend up and running quickly.

## Prerequisites

- Node.js >= 14.x installed
- MySQL server running (v5.7+ or MariaDB 10.3+)
- Existing Laravel database (or create a new one)

## Step 1: Install Dependencies

```bash
cd backend
npm install
```

## Step 2: Configure Environment

Create a `.env` file from the example:

```bash
cp .env.example .env
```

Edit `.env` and update these critical values:

```env
# Database Configuration - Update with your MySQL credentials
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=your_database_name
DB_USER=your_mysql_username
DB_PASSWORD=your_mysql_password

# JWT Secret - Change this to a random string
JWT_SECRET=your-very-secure-random-string-here

# JWT Refresh Secret - Another random string
JWT_REFRESH_SECRET=another-secure-random-string

# Email Configuration (for development, use Mailtrap or similar)
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USER=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
```

## Step 3: Ensure Database Exists

The backend uses the same database schema as the Laravel application. Make sure your database is already created and has the necessary tables.

If you're starting fresh, you can use the existing Laravel migrations:

```bash
# From the Laravel directory (Prohealth/)
php artisan migrate
```

## Step 4: Start the Server

### Development Mode (with auto-reload):

```bash
npm run dev
```

### Production Mode:

```bash
npm start
```

The server will start on `http://localhost:3000` (or the port you configured).

## Step 5: Test the API

### Health Check:

```bash
curl http://localhost:3000/health
```

Expected response:
```json
{
  "success": true,
  "message": "PineHealth Backend API is running",
  "timestamp": "2024-01-01T00:00:00.000Z",
  "environment": "development"
}
```

### Register a User:

```bash
curl -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "password": "password123"
  }'
```

### Login:

```bash
curl -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "password": "password123"
  }'
```

Save the token from the login response. You'll need it for authenticated requests.

### Get User Profile (authenticated):

```bash
curl -X GET http://localhost:3000/api/user/profile \
  -H "Authorization: Bearer YOUR_TOKEN_HERE"
```

## Common Issues

### Issue: "Unable to connect to the database"

**Solution:** 
- Verify MySQL is running: `mysql -u root -p`
- Check DB credentials in `.env` file
- Ensure database exists: `CREATE DATABASE pinehealth;`

### Issue: "Port 3000 already in use"

**Solution:** 
- Change the PORT in `.env` file
- Or kill the process using port 3000: `lsof -ti:3000 | xargs kill`

### Issue: "Cannot find module"

**Solution:**
- Delete `node_modules` and reinstall: `rm -rf node_modules && npm install`

### Issue: Email not sending

**Solution:**
- For development, use a service like Mailtrap (https://mailtrap.io)
- Update MAIL_* variables in `.env`
- Check email configuration in `src/utils/email.js`

## Integrating with React Frontend

The backend is designed to work seamlessly with the existing React frontend (in `Prohealth/resources/js/`).

### Update Frontend API Base URL:

In your frontend configuration or axios setup:

```javascript
// Update the base URL to point to the Node.js backend
axios.defaults.baseURL = 'http://localhost:3000/api';
```

### CORS Configuration:

If your frontend runs on a different port, update `CORS_ORIGIN` in `.env`:

```env
CORS_ORIGIN=http://localhost:8000,http://localhost:3000
```

## API Documentation

All available endpoints are documented in the main [README.md](README.md).

Key endpoint groups:
- `/api/auth/*` - Authentication (register, login, password reset)
- `/api/user/*` - User operations (profile, orders, tickets)
- `/api/admin/*` - Admin operations (dashboard, CRUD for all resources)
- `/api/*` - Public endpoints (blog, departments, doctors, contact)

## Production Deployment

### Environment Setup:

1. Set `APP_ENV=production` in `.env`
2. Use strong, unique values for `JWT_SECRET` and `JWT_REFRESH_SECRET`
3. Configure proper SMTP settings for email
4. Set up SSL/TLS (use nginx or similar as reverse proxy)
5. Use a process manager like PM2:

```bash
npm install -g pm2
pm2 start src/server.js --name pinehealth-backend
pm2 save
pm2 startup
```

### Security Checklist:

- ✅ Strong JWT secrets
- ✅ HTTPS enabled
- ✅ Rate limiting configured
- ✅ CORS properly configured
- ✅ Database credentials secured
- ✅ File upload size limits set
- ✅ Input validation enabled
- ✅ Error messages don't leak sensitive info

## Support

For issues or questions:
- Check the main [README.md](README.md)
- Review the code documentation in `src/` directory
- Create an issue in the repository

Happy coding! 🚀
