# Setup Guide

This guide walks you through setting up the Building Management System for local development.

## Prerequisites

Ensure the following are installed on your system:

| Requirement | Version | Verification Command |
|-------------|---------|---------------------|
| PHP | 8.2+ | `php -v` |
| Composer | 2.x | `composer -V` |
| MySQL | 8.0+ | `mysql --version` |
| Redis | 6.0+ | `redis-cli --version` |
| Node.js | 18+ | `node -v` |
| npm | 9+ | `npm -v` |

### Required PHP Extensions

```bash
php -m | grep -E "pdo_mysql|imagick|redis|gd|mbstring|xml|curl|zip"
```

Required extensions:
- pdo_mysql
- imagick
- redis
- gd
- mbstring
- xml
- curl
- zip
- bcmath

## Installation Steps

### Step 1: Clone the Repository

```bash
git clone <repository-url> building
cd building
```

### Step 2: Install PHP Dependencies

```bash
composer install
```

### Step 3: Install Node Dependencies

```bash
npm install
```

### Step 4: Environment Configuration

```bash
# Copy the example environment file
cp .env.example .env

# Generate application key
php artisan key:generate
```

### Step 5: Configure Database

Edit `.env` file with your database credentials:

```env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=building
DB_USERNAME=your_username
DB_PASSWORD=your_password
```

### Step 6: Run Migrations

```bash
# Run migrations
php artisan migrate

# Seed the database (optional, for development)
php artisan db:seed
```

### Step 7: Storage Link

```bash
php artisan storage:link
```

### Step 8: Build Frontend Assets

```bash
# Development build
npm run dev

# Production build
npm run build
```

### Step 9: Start Development Server

```bash
php artisan serve --port=8001
```

The API will be available at: `http://127.0.0.1:8001/api/v1`

## Additional Services

### Queue Worker

For background job processing:

```bash
php artisan queue:work
```

### Laravel Horizon (Recommended for Production)

```bash
php artisan horizon
```

Access Horizon dashboard at: `/horizon`

### Scheduled Tasks

For local development, run the scheduler manually:

```bash
php artisan schedule:work
```

For production, add to crontab:

```bash
* * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1
```

## Development Tools

### Laravel Telescope (Debugging)

Access at: `/telescope`

Prune old entries:
```bash
php artisan telescope:prune
```

### Running Tests

```bash
# Run all tests
php artisan test

# Run specific test suite
php artisan test --testsuite=Feature
php artisan test --testsuite=Unit

# Run with coverage
php artisan test --coverage
```

## Verifying Installation

After setup, verify the installation:

1. **Check API Health**
   ```bash
   curl http://127.0.0.1:8001/api/v1/health
   ```

2. **Test Authentication**
   ```bash
   curl -X POST http://127.0.0.1:8001/api/v1/auth/login \
     -H "Content-Type: application/json" \
     -d '{"email": "admin@example.com", "password": "password"}'
   ```

3. **Check Database Connection**
   ```bash
   php artisan db:show
   ```

## Next Steps

- Review [Environment Configuration](ENVIRONMENT_CONFIGURATION.md) for detailed settings
- Explore [System Architecture](../02-architecture/SYSTEM_ARCHITECTURE.md)
- Check [Troubleshooting](TROUBLESHOOTING.md) if you encounter issues
