services:
  nginx:
    image: nginx:1.27-alpine
    container_name: taskco-app-nginx
    restart: unless-stopped
    ports:
      - "8081:8080"
      - "8444:8443"
    volumes:
      - ./.docker/nginx/app/prod.conf:/etc/nginx/conf.d/default.conf:ro
      - ./ssl:/etc/nginx/ssl:ro
      - ./app/public:/var/www/html/public:ro
    depends_on:
      app:
        condition: service_healthy
    networks:
      - taskco_network
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://taskco-app:8000/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s   # wait for Nginx to fully start


  app:
    build:
      context: .
      dockerfile: .docker/app/Dockerfile
    image: taskco-app:latest
    container_name: taskco-app
    restart: unless-stopped
    env_file:
      - .env
    environment:
      AUTO_MIGRATE: "true"
      AUTO_FIX_PERMISSIONS: "true"
    ports:
      - "8000:8000"
    volumes:
      - taskco-app-storage:/var/www/html/storage
      - taskco-app-bootstrap-cache:/var/www/html/bootstrap/cache
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - taskco_network
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://127.0.0.1:8000/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  postgres:
    image: postgres:16-alpine
    container_name: taskco-app-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_DATABASE}        # e.g., taskco
      POSTGRES_USER: ${DB_USERNAME}      # e.g., taskco_admin
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    env_file:
      - .env
    ports:
      - "5442:5432"
    volumes:
      - taskco-app-postgres-data:/var/lib/postgresql/data
    networks:
      - taskco_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h localhost -U ${DB_USERNAME} -d ${DB_DATABASE}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  redis:
    image: redis:7-alpine
    container_name: taskco-app-redis
    restart: unless-stopped
    ports:
      - "6382:6379"
    volumes:
      - taskco-app-redis-data:/data
    networks:
      - taskco_network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  worker:
    build:
      context: .
      dockerfile: .docker/worker/worker.Dockerfile
    image: taskco-app-worker:latest
    container_name: taskco-app-worker
    restart: unless-stopped
    env_file:
      - .env
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./app:/var/www/html
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - taskco_network
    healthcheck:
      test: ["CMD-SHELL", "supervisorctl status || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  scheduler:
    image: taskco-app-worker:latest
    container_name: taskco-app-scheduler
    restart: unless-stopped
    command: php artisan schedule:work
    env_file:
      - .env
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./app:/var/www/html
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - taskco_network
    healthcheck:
      test: ["CMD-SHELL", "php /var/www/html/artisan schedule:list || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  horizon:
    build:
      context: .
      dockerfile: .docker/worker/worker.Dockerfile
    image: taskco-app-worker:latest
    container_name: taskco-app-horizon
    restart: unless-stopped
    command: php artisan horizon
    env_file:
      - .env
    volumes:
      - ./app:/var/www/html
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      - HORIZON_ENABLED=true
    networks:
      - taskco_network
    healthcheck:
      test: ["CMD-SHELL", "pgrep -f 'artisan horizon' || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    profiles:
      - horizon

  mailpit:
    image: axllent/mailpit:latest
    container_name: taskco_mailpit
    ports:
      - "1026:1025" # SMTP server
      - "8026:8025" # Web UI
    restart: unless-stopped
    networks:
      - taskco_network


networks:
  taskco_network:
    driver: bridge

volumes:
  taskco-app-postgres-data:
  taskco-app-redis-data:
  taskco-app-storage:
  taskco-app-bootstrap-cache: