services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: arxiv_app_image
    container_name: arxiv_app
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
      - vendor_data:/var/www/html/vendor
      - node_modules_data:/var/www/html/node_modules
      - ./docker/php/opcache.ini:/usr/local/etc/php/conf.d/zz-opcache.ini:ro
    ports:
      - "8000:8000"
    env_file:
      - .env
    environment:
      DB_HOST: db
      DB_PORT: 3306
      TZ: Asia/Baku
    depends_on:
      db:
        condition: service_healthy
    command: >
      sh -lc "
      composer install &&
      php artisan key:generate --force &&
      until php -r 'try { new PDO(\"mysql:host=db;port=3306;dbname=arxiv\", \"root\", \"root\"); exit(0); } catch (Exception) { exit(1); }'; do
        echo 'Waiting for MySQL...';
        sleep 2;
      done &&
      php artisan migrate --force &&
      php artisan db:seed --force &&
      php -S 0.0.0.0:8000 -t public
      "
  queue:
    image: arxiv_app_image
    container_name: arxiv_queue
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
      - vendor_data:/var/www/html/vendor
      - ./docker/php/opcache.ini:/usr/local/etc/php/conf.d/zz-opcache.ini:ro
    env_file:
      - .env
    environment:
      DB_HOST: db
      DB_PORT: 3306
      TZ: Asia/Baku
    restart: unless-stopped
    depends_on:
      app:
        condition: service_started
      db:
        condition: service_healthy
    command: >
      sh -lc "
      until [ -f /var/www/html/vendor/autoload.php ]; do
        echo 'Waiting for dependencies...';
        sleep 2;
      done &&
      until php -r 'try { new PDO(\"mysql:host=db;port=3306;dbname=arxiv\", \"root\", \"root\"); exit(0); } catch (Exception) { exit(1); }'; do
        echo 'Waiting for MySQL...';
        sleep 2;
      done &&
      php artisan queue:work --verbose --tries=3 --timeout=90
      "
  db:
    image: mysql:8.0
    container_name: arxiv_db
    restart: unless-stopped
    command: ["--default-time-zone=+04:00"]
    environment:
      MYSQL_DATABASE: arxiv
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: arxiv
      MYSQL_PASSWORD: arxiv
      TZ: Asia/Baku
    ports:
      - "8889:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-proot"]
      interval: 5s
      timeout: 3s
      retries: 20
  phpmyadmin:
    image: phpmyadmin:latest
    container_name: arxiv_phpmyadmin
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "8081:80"
volumes:
  mysql_data:
  vendor_data:
  node_modules_data:
networks:
  default:
    name: arxiv_network
