version: '3.8'

services:
  # Web service
  web:
    build: .
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./hesabixCore:/var/www/html/hesabixCore
      - ./webUI:/var/www/html/webUI
      - ./public_html:/var/www/html/public_html
      - ./hesabixBackup:/var/www/html/hesabixBackup
      - ./ssl:/etc/ssl/private
    depends_on:
      - db
    environment:
      - DATABASE_URL=mysql://hesabix_user:hesabix_password@db:3306/hesabix_db
      - APP_ENV=prod
      - APP_SECRET=${APP_SECRET:-your-secret-key}
      - DOMAIN=${DOMAIN:-localhost}
    networks:
      - hesabix-network
    restart: unless-stopped
    command: >
      bash -c "
        apt-get update &&
        apt-get install -y curl unzip php-mbstring php-gd php-soap &&
        curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer &&
        curl -fsSL https://deb.nodesource.com/setup_20.x | bash - &&
        apt-get install -y nodejs &&
        cd /var/www/html/hesabixCore &&
        composer install &&
        cd /var/www/html/webUI &&
        npm install &&
        npm run build-only &&
        apache2-foreground
      "

  # Database service
  db:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=root_password
      - MYSQL_DATABASE=hesabix_db
      - MYSQL_USER=hesabix_user
      - MYSQL_PASSWORD=hesabix_password
    volumes:
      - mysql_data:/var/lib/mysql
      - ./hesabixBackup/databasefiles:/docker-entrypoint-initdb.d
    networks:
      - hesabix-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-proot_password"]
      interval: 5s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    command: >
      bash -c "
        docker-entrypoint.sh mysqld &
        sleep 30 &&
        mysql -u root -proot_password hesabix_db < /docker-entrypoint-initdb.d/hesabix-db-default.sql
      "

  # phpMyAdmin service
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8080:80"
    environment:
      - PMA_HOST=db
      - PMA_USER=hesabix_user
      - PMA_PASSWORD=hesabix_password
    depends_on:
      - db
    networks:
      - hesabix-network
    restart: unless-stopped

  # Certbot service for SSL
  certbot:
    image: certbot/certbot
    volumes:
      - ./ssl:/etc/letsencrypt
      - ./public_html:/var/www/html
    depends_on:
      - web
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
    networks:
      - hesabix-network
    restart: unless-stopped

networks:
  hesabix-network:
    driver: bridge

volumes:
  mysql_data: 