version: '3.8'

services:
  # ─── PHP + Apache Web Server ───────────────────────────────────────────────
  app:
    image: php:8.2-apache
    container_name: kostcycle_app
    restart: unless-stopped
    ports:
      - "127.0.0.1:8080:80" # Akses langsung dari host saja, publik via Caddy
    volumes:
      - .:/var/www/html
      - ./docker/apache/vhost.conf:/etc/apache2/sites-available/000-default.conf
      - ./docker/php/php.ini:/usr/local/etc/php/conf.d/custom.ini
    environment:
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=kostcycle
      - DB_USERNAME=kostcycle_user
      - DB_PASSWORD=kostcycle_pass
    depends_on:
      db:
        condition: service_healthy
    networks:
      - kostcycle_network
    command: >
      bash -c "
        docker-php-ext-install mysqli pdo pdo_mysql &&
        a2enmod rewrite &&
        apache2-foreground
      "

  # ─── MySQL Database ────────────────────────────────────────────────────────
  db:
    image: mysql:8.0
    container_name: kostcycle_db
    restart: unless-stopped
    ports:
      - "3307:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: kostcycle
      MYSQL_USER: kostcycle_user
      MYSQL_PASSWORD: kostcycle_pass
    command: --sql-mode="NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION"
    volumes:
      - kostcycle_db_data:/var/lib/mysql
      - ./DB_multiseller.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-proot_password"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    networks:
      - kostcycle_network

  # ─── phpMyAdmin ────────────────────────────────────────────────────────────
  phpmyadmin:
    image: phpmyadmin:latest
    container_name: kostcycle_phpmyadmin
    restart: unless-stopped
    ports:
      - "8081:80"
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_USER: root
      PMA_PASSWORD: root_password
    depends_on:
      db:
        condition: service_healthy
    networks:
      - kostcycle_network

# ─── Volumes ────────────────────────────────────────────────────────────────
volumes:
  kostcycle_db_data:
    driver: local

# ─── Networks ────────────────────────────────────────────────────────────────
networks:
  kostcycle_network:
    driver: bridge
