# Installation Guide - Matendes HRM System

## System Requirements

### Minimum Requirements
- PHP 8.2 or higher
- Composer 2.0+
- Node.js 18+ and NPM
- MySQL 8.0+ or PostgreSQL 13+ (SQLite for development)
- Redis 6.0+ (recommended for production)
- Web server (Apache/Nginx)

### Recommended Server Specs
- **CPU**: 2+ cores
- **RAM**: 4GB+ (8GB+ for production)
- **Storage**: 20GB+ SSD
- **Network**: 1Gbps

## Installation Steps

### 1. Clone Repository
```bash
git clone https://github.com/your-org/matendes-hrm.git
cd matendes-hrm
```

### 2. Install Dependencies
```bash
# Install PHP dependencies
composer install --optimize-autoloader

# Install Node.js dependencies
cd frontend && npm install
```

### 3. Environment Configuration
```bash
# Copy environment file
cp .env.example .env

# Generate application key
php artisan key:generate

# Generate JWT secret (if using JWT)
php artisan jwt:secret
```

### 4. Database Setup
```bash
# Create database
mysql -u root -p -e "CREATE DATABASE matendes_hrm CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

# Configure .env file with database credentials
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=matendes_hrm
DB_USERNAME=your_username
DB_PASSWORD=your_password

# Run migrations
php artisan migrate

# Seed database
php artisan db:seed
```

### 5. Storage Configuration
```bash
# Link storage
php artisan storage:link

# Set permissions (Linux/Mac)
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache
```

### 6. Cache Configuration
```bash
# Configure cache (Redis recommended)
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

### 7. Queue Configuration (Production)
```bash
# Install supervisor for queue workers
sudo apt-get install supervisor

# Configure queue worker
php artisan queue:work --daemon
```

### 8. Frontend Build
```bash
cd frontend
npm run build  # For production
npm run dev    # For development
```

## Development Setup

### Quick Setup with Docker
```bash
# Using Laravel Sail
./vendor/bin/sail up -d
./vendor/bin/sail artisan migrate --seed
```

### Local Development
```bash
# Start development servers
php artisan serve  # Backend on http://localhost:8000
cd frontend && npm run dev  # Frontend on http://localhost:3000
```

## Production Deployment

### Web Server Configuration

#### Apache (.htaccess)
```apache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
```

#### Nginx
```nginx
server {
    listen 80;
    server_name your-domain.com;
    root /path/to/matendes-hrm/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

### SSL Configuration
```bash
# Using Let's Encrypt
certbot --nginx -d your-domain.com
```

## Troubleshooting

### Common Issues

#### Permission Errors
```bash
# Fix Laravel permissions
sudo chown -R $USER:www-data .
sudo find . -type f -exec chmod 664 {} \;
sudo find . -type d -exec chmod 775 {} \;
sudo chmod -R ug+rwx storage bootstrap/cache
```

#### Database Connection Issues
```bash
# Check MySQL connection
php artisan tinker
>>> DB::connection()->getPdo();
```

#### Queue Not Processing
```bash
# Restart queue worker
php artisan queue:restart

# Check failed jobs
php artisan queue:failed
```

### Performance Optimization
```bash
# Enable OPcache in production
echo "opcache.enable=1" >> /etc/php/8.2/apache2/php.ini

# Use Redis for caching
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
```

## Security Checklist

- [ ] Change default admin credentials
- [ ] Configure proper file permissions
- [ ] Enable HTTPS/SSL
- [ ] Configure firewall rules
- [ ] Set up regular backups
- [ ] Enable error logging
- [ ] Configure rate limiting
- [ ] Regular security updates

## Support

- **Documentation**: `/docs` folder
- **API Documentation**: `http://your-domain.com/api-docs.html`
- **Issue Tracker**: GitHub Issues
- **Email**: support@matendes.com

## Next Steps

1. [Read Developer Guidelines](DEVELOPER_GUIDELINES.md)
2. [Review API Documentation](API_DOCUMENTATION.md)
3. [Configure System Settings](../config/hrm.php)
4. [Set up monitoring and backups](DEPLOYMENT_GUIDE.md)