services:
  # Laravel Application Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: deemah-app
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - .:/var/www/html
      - ~/.aws:/root/.aws:ro  # Mount AWS credentials from host (read-only)
    environment:
      - APP_ENV=${APP_ENV:-local}
      - APP_DEBUG=${APP_DEBUG:-true}
      - DB_CONNECTION=pgsql
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_DATABASE=${DB_DATABASE:-deemah_authorization}
      - DB_USERNAME=${DB_USERNAME:-postgres}
      - DB_PASSWORD=${DB_PASSWORD:-postgres}
      - AWS_PROFILE=${AWS_PROFILE:-default}  # AWS profile selection
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
      - AWS_BUCKET=${AWS_BUCKET:-deemah-authorization-media}
    ports:
      - "8000:8000"
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - deemah-network
    command: php artisan serve --host=0.0.0.0 --port=8000

  # PostgreSQL Database Service
  postgres:
    image: postgres:16-alpine  # Using PostgreSQL 16 (latest stable, > 14)
    container_name: deemah-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_DB=${DB_DATABASE:-deemah_authorization}
      - POSTGRES_USER=${DB_USERNAME:-postgres}
      - POSTGRES_PASSWORD=${DB_PASSWORD:-postgres}
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - postgres-data:/var/lib/postgresql/data  # Persistent volume for database
    ports:
      - "5432:5432"
    networks:
      - deemah-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME:-postgres} -d ${DB_DATABASE:-deemah_authorization}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Vite Development Server for Hot Module Replacement
  vite:
    build:
      context: .
      dockerfile: Dockerfile.vite
    container_name: deemah-vite
    restart: unless-stopped
    working_dir: /var/www/html
    volumes:
      - .:/var/www/html
      - /var/www/html/node_modules  # Use container's node_modules
    ports:
      - "5173:5173"  # Vite default port
    networks:
      - deemah-network
    environment:
      - VITE_HOST=0.0.0.0
      - VITE_PORT=5173
    command: npm run dev

# Named volumes for data persistence
volumes:
  postgres-data:
    driver: local

# Network for service communication
networks:
  deemah-network:
    driver: bridge
