# Docker Setup Guide

This guide will help you set up and run the Markatty project using Docker on both Mac and Windows.

## Prerequisites

### For Mac Users

1. **Install Docker Desktop for Mac**
   - Download from: https://www.docker.com/products/docker-desktop/
   - Install and start Docker Desktop
   - Ensure Docker Desktop is running (you should see the Docker icon in the menu bar)

2. **Install Git** (if not already installed)
   - Download from: https://git-scm.com/download/mac
   - Or use Homebrew: `brew install git`

3. **Terminal Access**
   - Use Terminal.app or iTerm2

### For Windows Users

1. **Install Docker Desktop for Windows**
   - Download from: https://www.docker.com/products/docker-desktop/
   - Install and start Docker Desktop
   - Ensure WSL 2 is enabled (Docker Desktop will prompt you if needed)
   - Restart your computer if prompted

2. **Install Git for Windows** (if not already installed)
   - Download from: https://git-scm.com/download/win

3. **Terminal Access**
   - Use PowerShell, Command Prompt, or Windows Terminal
   - Git Bash is also recommended

## Important: Port 80 Requirements

**⚠️ Port 80 is required for the Apache web server.**

### Mac Users

Port 80 requires administrator privileges. If you encounter permission errors:

**Option 1: Use sudo (Recommended)**
```bash
sudo docker-compose up -d
```

**Option 2: Change to port 8080 (Alternative)**
If you can't use port 80, edit `docker-compose.yml` and change:
```yaml
ports:
  - "80:80"
```
to:
```yaml
ports:
  - "8080:80"
```
Then access the site at `http://store.127.0.0.1.nip.io:8080/`

### Windows Users

Port 80 may be in use by other services (IIS, Skype, etc.). 

**Option 1: Stop conflicting services**
1. Open Services (Win + R, type `services.msc`)
2. Stop "World Wide Web Publishing Service" (IIS) if running
3. Close Skype or other applications using port 80

**Option 2: Use port 8080 (Alternative)**
Edit `docker-compose.yml` and change:
```yaml
ports:
  - "80:80"
```
to:
```yaml
ports:
  - "8080:80"
```
Then access the site at `http://store.127.0.0.1.nip.io:8080/`

## Initial Setup

### 1. Navigate to Project Directory

**Mac/Linux:**
```bash
cd /path/to/markatty/Code/html2
```

**Windows:**
```cmd
cd C:\path\to\markatty\Code\html2
```

### 2. Configure Environment Variables

Create a `.env` file if it doesn't exist:

**Mac/Linux:**
```bash
cp .env.example .env
```

**Windows:**
```cmd
copy .env.example .env
```

Edit the `.env` file and set:
```env
APP_URL=http://store.127.0.0.1.nip.io
DB_DATABASE=markatty
DB_PASSWORD=root
```

### VPS search suggest (Meilisearch) from inside Docker

Laravel runs in the `app` container. URLs like `http://search.127.0.0.1.nip.io:8080` resolve to **127.0.0.1 inside that container**, not your Mac, so `Http::get` to the search API will fail and search falls back to the database (often empty or very slow).

Use the host gateway instead (Docker Desktop Mac/Windows):

```env
SEARCH_SUGGEST_ENABLED=true
SEARCH_SUGGEST_BASE_URL=http://host.docker.internal:8080
SEARCH_SUGGEST_TOKEN=<same as SUGGEST_BEARER_TOKEN in vps-search>
```

Rebuild the search-api image after `server.js` changes: `docker compose build search-api && docker compose up -d` in `vps-search/`.

### 3. Build and Start Services

**Mac/Linux:**
```bash
docker-compose build
docker-compose up -d
```

**Windows:**
```cmd
docker-compose build
docker-compose up -d
```

**Note for Mac:** If you get permission errors on port 80, use:
```bash
sudo docker-compose up -d
```

### 4. Import Database (Optional)

If you have a database dump file:

**Mac/Linux:**
```bash
# Extract the SQL file if it's in a zip
unzip /path/to/Markatty_prod.zip -d /tmp/markatty_import

# Import to database
docker-compose exec -T db mysql -uroot -proot markatty < /tmp/markatty_import/Markatty_prod.sql
```

**Windows:**
```cmd
# Extract the SQL file if it's in a zip (use 7-Zip or WinRAR)
# Then import to database
docker-compose exec -T db mysql -uroot -proot markatty < C:\path\to\Markatty_prod.sql
```

Or use phpMyAdmin (see Access URLs below).

### 5. Install Composer Dependencies

**Mac/Linux/Windows:**
```bash
docker-compose exec app composer install
```

### 6. Run Database Migrations

**Mac/Linux/Windows:**
```bash
docker-compose exec app php artisan migrate
```

## Access URLs

Once all services are running, you can access:

- **Main Site:** http://store.127.0.0.1.nip.io/
  - Automatically redirects to: http://store.127.0.0.1.nip.io/company/register

- **phpMyAdmin:** http://localhost:8081/
  - Username: `root`
  - Password: `root` (or your DB_PASSWORD from .env)

- **MySQL Direct Access:**
  - Host: `localhost`
  - Port: `3306`
  - Username: `root`
  - Password: `root` (or your DB_PASSWORD from .env)
  - Database: `markatty`

## Common Commands

### Start Services
```bash
docker-compose up -d
```

### Stop Services
```bash
docker-compose down
```

### View Logs
```bash
# All services
docker-compose logs -f

# Specific service
docker-compose logs -f apache
docker-compose logs -f app
docker-compose logs -f db
```

### Restart a Service
```bash
docker-compose restart apache
docker-compose restart app
docker-compose restart db
```

### Execute Commands in Containers

**Run PHP Artisan:**
```bash
docker-compose exec app php artisan [command]
```

**Run Composer:**
```bash
docker-compose exec app composer [command]
```

**Access Container Shell:**
```bash
docker-compose exec app bash
docker-compose exec apache bash
docker-compose exec db bash
```

## Troubleshooting

### Port 80 Already in Use

**Mac:**
```bash
# Check what's using port 80
sudo lsof -i :80

# Stop Apache if MAMP is running
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
```

**Windows:**
```cmd
# Check what's using port 80
netstat -ano | findstr :80

# Stop IIS (if running)
iisreset /stop
```

### Services Won't Start

1. **Check Docker is running:**
   - Mac: Look for Docker icon in menu bar
   - Windows: Check system tray for Docker icon

2. **Check logs:**
   ```bash
   docker-compose logs
   ```

3. **Rebuild containers:**
   ```bash
   docker-compose down
   docker-compose build --no-cache
   docker-compose up -d
   ```

### Database Connection Issues

1. **Wait for database to be ready:**
   ```bash
   docker-compose exec db mysqladmin ping -h localhost -uroot -proot
   ```

2. **Check database credentials in .env file**

3. **Restart database:**
   ```bash
   docker-compose restart db
   ```

### Permission Issues (Mac/Linux)

If you get permission errors:
```bash
sudo chown -R $USER:$USER .
sudo chmod -R 755 storage bootstrap/cache
```

### Access Denied / Cache Administrator Error

This error usually indicates:
1. **Browser cache** - Clear cache or use Incognito/Private mode
2. **VPN/Proxy** - Disable VPN or proxy settings
3. **Browser extensions** - Disable ad blockers or privacy extensions
4. **Try direct IP:** http://127.0.0.1/company/register

## Platform-Specific Notes

### Mac (Apple Silicon - M1/M2/M3)

The docker-compose.yml is configured for `linux/arm64`. If you encounter issues:

1. Ensure Docker Desktop is using Apple Silicon version
2. Some images may need platform override:
   ```yaml
   platform: linux/amd64
   ```

### Windows (WSL 2)

1. Ensure WSL 2 is installed and updated
2. Docker Desktop should use WSL 2 backend
3. If using Git Bash, ensure line endings are handled correctly

### Windows (Hyper-V)

If using Hyper-V instead of WSL 2:
- Ensure Hyper-V is enabled in Windows Features
- Docker Desktop should detect and use Hyper-V automatically

## Service Architecture

- **Apache** (Port 80): Web server, handles HTTP requests
- **App** (PHP-FPM): PHP application container, processes PHP requests
- **DB** (Port 3306): MySQL 8.0 database server
- **phpMyAdmin** (Port 8081): Database management interface

## Next Steps

After setup:
1. Access http://store.127.0.0.1.nip.io/
2. Register a new company at `/company/register`
3. After registration, you'll be redirected to your company subdomain: `http://companyname.127.0.0.1.nip.io/`
4. Login to admin panel at `http://companyname.127.0.0.1.nip.io/admin`

## Support

For issues or questions:
- Check Docker logs: `docker-compose logs`
- Verify all services are running: `docker-compose ps`
- Check Apache configuration: `docker-compose exec apache httpd -S`
