services:

  php:
    build:
      context: .
      dockerfile: docker/dockerfile_php
    environment:
      DB_HOST: mariadb
      DB_DATABASE: ${MYSQL_DATABASE}
      DB_USERNAME: ${MYSQL_USER}
      DB_PASSWORD: ${MYSQL_PASSWORD}
    image: angels-it/php-app
    volumes:
      - ./app:/var/www/html
      - ./sessions:/var/lib/php/sessions
      - ./config/php:/usr/local/etc/php
      - ./logs/php:/var/log
    networks:
      - app_network

  php-cron:
    build:
      context: .
      dockerfile: docker/dockerfile_cron
    image: angels-it/php-cron
    environment:
      APP_ENV: ${APP_ENV}
      DB_HOST: mariadb
      DB_DATABASE: ${MYSQL_DATABASE}
      DB_USERNAME: ${MYSQL_USER}
      DB_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - ./app:/var/www/html
      - ./config/php:/usr/local/etc/php
      - ./config/cron:/etc/cron.d
      - ./logs/php-cron:/var/log 
    command: >
      /bin/sh -c '
      if [ "$${APP_ENV}" = "prod" ]; then
        echo "[cron] production mode — starting cron";
        cron -f;
      else
        echo "[cron] dev mode — sleeping";
        tail -f /dev/null;
      fi
      '
    restart: always
    networks:
      - app_network

  nginx:
    image: nginx:1.24.0
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php
    volumes:
      - ./app:/var/www/html
      - ./config/nginx/default.conf.${APP_ENV}:/etc/nginx/conf.d/default.conf
      - ./config/certs:/etc/letsencrypt
      - ./config/certs-data:/data/letsencrypt
      - ./config/certs/selfsigned:/etc/ssl/selfsigned
      - ./logs/nginx:/var/log/nginx      
    networks:
      - app_network

  mariadb:
    image: mariadb:10.5
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - ./db/mysql:/var/lib/mysql
      - ./db/init:/docker-entrypoint-initdb.d

    command: [
      '--default_authentication_plugin=mysql_native_password',
      '--character-set-server=utf8mb4',
      '--collation-server=utf8mb4_unicode_ci',
      '--sql_mode=NO_ENGINE_SUBSTITUTION'
    ]
    networks:
      - app_network

  
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    ports:
      - "8080:80"
    environment:
      - PMA_HOST=mariadb
      - PMA_PORT=3306
    networks:
      - app_network      

networks:
  app_network:
    driver: bridge
