version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    image: tvplus-app:latest
    container_name: tvplus-app
    restart: unless-stopped
    ports:
      - "8080:80"
    environment:
      - APP_ENV=production
      - APP_DEBUG=false
      - DB_CONNECTION=mysql
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=tvplus
      - DB_USERNAME=tvplus
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - QUEUE_CONNECTION=redis
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
    depends_on:
      - db
      - redis
    networks:
      - tvplus-network
    volumes:
      - ./storage/app/public:/var/www/html/storage/app/public
      - ./storage/logs:/var/www/html/storage/logs

  db:
    image: mysql:8.0
    container_name: tvplus-db
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=tvplus
      - MYSQL_USER=tvplus
      - MYSQL_PASSWORD=${DB_PASSWORD}
    ports:
      - "3307:3306"
    volumes:
      - db-data:/var/lib/mysql
      - ./docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - tvplus-network

  redis:
    image: redis:7-alpine
    container_name: tvplus-redis
    restart: unless-stopped
    ports:
      - "6380:6379"
    volumes:
      - redis-data:/data
    networks:
      - tvplus-network

  queue:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    container_name: tvplus-queue
    restart: unless-stopped
    command: php artisan queue:work --sleep=3 --tries=3 --max-time=3600
    environment:
      - APP_ENV=production
      - DB_HOST=db
      - REDIS_HOST=redis
    depends_on:
      - db
      - redis
    networks:
      - tvplus-network

  scheduler:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    container_name: tvplus-scheduler
    restart: unless-stopped
    command: /bin/sh -c "while [ true ]; do php artisan schedule:run --verbose --no-interaction; sleep 60; done"
    environment:
      - APP_ENV=production
      - DB_HOST=db
      - REDIS_HOST=redis
    depends_on:
      - db
      - redis
    networks:
      - tvplus-network

networks:
  tvplus-network:
    driver: bridge

volumes:
  db-data:
  redis-data: