# ============================================
# MakLinkApp – docker-compose.yml
# Multi-service deployment for TFSBD Platform
# ============================================

version: '3.9'

services:

  # --- Main app (Nginx + PHP + Python) ---
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    container_name: maklinkapp
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    environment:
      APP_ENV:    production
      APP_URL:    https://maklinkapp.tfsbd.com
      DB_HOST:    db
      DB_PORT:    3306
      DB_NAME:    maklinkapp
      DB_USER:    maklinkapp_user
      DB_PASS:    ${DB_PASS}
      JWT_SECRET: ${JWT_SECRET}
      MAIL_HOST:  ${MAIL_HOST}
      MAIL_USER:  ${MAIL_USER}
      MAIL_PASS:  ${MAIL_PASS}
    volumes:
      - ./uploads:/var/www/uploads
      - ./logs:/var/log/app
      - ./ssl:/etc/ssl/maklinkapp:ro
    depends_on:
      db:
        condition: service_healthy
    networks:
      - mla-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # --- MySQL database ---
  db:
    image: mysql:8.3
    container_name: maklinkapp-db
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      MYSQL_DATABASE:      maklinkapp
      MYSQL_USER:          maklinkapp_user
      MYSQL_PASSWORD:      ${DB_PASS}
    volumes:
      - db_data:/var/lib/mysql
      - ./database/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro
      - ./database/seed.sql:/docker-entrypoint-initdb.d/02-seed.sql:ro
    ports:
      - "127.0.0.1:3306:3306"
    networks:
      - mla-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASS}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # --- Redis (sessions & cache) ---
  redis:
    image: redis:7.2-alpine
    container_name: maklinkapp-redis
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASS} --maxmemory 128mb --maxmemory-policy allkeys-lru
    volumes:
      - redis_data:/data
    ports:
      - "127.0.0.1:6379:6379"
    networks:
      - mla-network

  # --- Modular apps (add more here) ---
  # app-eco-boat:
  #   build: ./apps/eco-boat
  #   container_name: maklinkapp-eco-boat
  #   networks: [mla-network]
  #   labels:
  #     - "traefik.http.routers.eco-boat.rule=PathPrefix(`/apps/eco-boat`)"

volumes:
  db_data:
  redis_data:

networks:
  mla-network:
    driver: bridge
