version: '3.8'

services:
  # Nginx Web Server
  nginx:
    image: nginx:alpine
    container_name: trading-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ../backend/public:/var/www/html/backend/public:ro
      - ../frontend/dist:/var/www/html/frontend:ro
    depends_on:
      - backend
      - frontend
    networks:
      - trading-network
    restart: unless-stopped

  # PHP-FPM Backend (Laravel)
  backend:
    build:
      context: ../backend
      dockerfile: ../docker/Dockerfile.backend
    container_name: trading-backend
    volumes:
      - ../backend:/var/www/html
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini:ro
    environment:
      - APP_ENV=local
      - APP_DEBUG=true
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_DATABASE=trading_journal
      - DB_USERNAME=trading
      - DB_PASSWORD=trading_password
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - CACHE_DRIVER=redis
      - QUEUE_CONNECTION=redis
      - SESSION_DRIVER=redis
    depends_on:
      - mysql
      - redis
    networks:
      - trading-network
    restart: unless-stopped

  # Vue.js Frontend (for development)
  frontend:
    build:
      context: ../frontend
      dockerfile: ../docker/Dockerfile.frontend
    container_name: trading-frontend
    ports:
      - "5173:5173"
    volumes:
      - ../frontend:/app
      - /app/node_modules
    environment:
      - NODE_ENV=development
      - VITE_API_URL=http://localhost/api/v1
    networks:
      - trading-network
    restart: unless-stopped
    command: npm run dev -- --host

  # MySQL Database
  mysql:
    image: mysql:8.0
    container_name: trading-mysql
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ../database/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
    environment:
      - MYSQL_ROOT_PASSWORD=root_password
      - MYSQL_DATABASE=trading_journal
      - MYSQL_USER=trading
      - MYSQL_PASSWORD=trading_password
    networks:
      - trading-network
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password

  # Redis Cache & Queue
  redis:
    image: redis:alpine
    container_name: trading-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - trading-network
    restart: unless-stopped

  # Laravel Queue Worker
  queue:
    build:
      context: ../backend
      dockerfile: ../docker/Dockerfile.backend
    container_name: trading-queue
    volumes:
      - ../backend:/var/www/html
    environment:
      - APP_ENV=local
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_DATABASE=trading_journal
      - DB_USERNAME=trading
      - DB_PASSWORD=trading_password
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - QUEUE_CONNECTION=redis
    depends_on:
      - mysql
      - redis
    networks:
      - trading-network
    restart: unless-stopped
    command: php artisan queue:work --sleep=3 --tries=3 --max-time=3600

  # Laravel Scheduler (Cron)
  scheduler:
    build:
      context: ../backend
      dockerfile: ../docker/Dockerfile.backend
    container_name: trading-scheduler
    volumes:
      - ../backend:/var/www/html
    environment:
      - APP_ENV=local
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_DATABASE=trading_journal
      - DB_USERNAME=trading
      - DB_PASSWORD=trading_password
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    depends_on:
      - mysql
      - redis
    networks:
      - trading-network
    restart: unless-stopped
    command: sh -c "while true; do php artisan schedule:run; sleep 60; done"

  # phpMyAdmin (optional, for development)
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: trading-phpmyadmin
    ports:
      - "8080:80"
    environment:
      - PMA_HOST=mysql
      - MYSQL_ROOT_PASSWORD=root_password
    depends_on:
      - mysql
    networks:
      - trading-network
    restart: unless-stopped

networks:
  trading-network:
    driver: bridge

volumes:
  mysql_data:
  redis_data:

