version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    container_name: srm_postgres
    environment:
      POSTGRES_DB: software_release_management
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data/pgdata
      - ./scripts/init-db.sql:/docker-entrypoint-initdb.d/01-init-db.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d software_release_management"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    networks:
      - srm-network

  # Database migration service
  database-migrator:
    build:
      context: ./src/Database
      dockerfile: Dockerfile.migrator
    container_name: srm_db_migrator
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ConnectionStrings__DefaultConnection=Host=postgres;Port=5432;Database=software_release_management;Username=postgres;Password=postgres
    networks:
      - srm-network
    restart: no

  # PgAdmin for database management (optional)
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: srm_pgadmin
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@company.com
      PGADMIN_DEFAULT_PASSWORD: admin123
      PGADMIN_CONFIG_SERVER_MODE: 'False'
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
    ports:
      - "5050:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
      - ./scripts/servers.json:/pgadmin4/servers.json:ro
    depends_on:
      - postgres
    networks:
      - srm-network
    profiles:
      - dev
    restart: unless-stopped

volumes:
  postgres_data:
    driver: local
  pgadmin_data:
    driver: local

networks:
  srm-network:
    driver: bridge
    name: software-release-management