# ==============================================================================
# PAYE Self Service API - Docker Compose Configuration
# ==============================================================================
# Production-ready configuration for Linux deployment
#
# Usage:
#   Development:  docker-compose up -d
#   Production:   docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
#   Logs:         docker-compose logs -f api
#   Stop:         docker-compose down
#   Rebuild:      docker-compose up -d --build
# ==============================================================================

services:
  # ============================================================================
  # PAYE Self Service API
  # ============================================================================
  api:
    # --------------------------------------------------------------------------
    # Build configuration
    # --------------------------------------------------------------------------
    build:
      context: .
      dockerfile: Dockerfile

    # --------------------------------------------------------------------------
    # Container naming
    # --------------------------------------------------------------------------
    image: paye-self-service-api:latest
    container_name: paye-self-service-api
    hostname: paye-api

    # --------------------------------------------------------------------------
    # Restart policy
    # - always: Restart container if it stops for any reason
    # - unless-stopped: Same as always, but respects manual stops
    # --------------------------------------------------------------------------
    restart: unless-stopped

    # --------------------------------------------------------------------------
    # Port mapping
    # Host:Container - External port 80 maps to container port 8080
    # Change host port (left side) as needed for your environment
    # --------------------------------------------------------------------------
    ports:
      - "${API_PORT:-80}:8080"

    # --------------------------------------------------------------------------
    # Environment variables
    # Loaded from .env file and can be overridden
    # --------------------------------------------------------------------------
    env_file:
      - .env

    # --------------------------------------------------------------------------
    # Environment variable definitions
    # These override any values in .env file
    # --------------------------------------------------------------------------
    environment:
      # ASP.NET Core settings
      - ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT:-Production}
      - ASPNETCORE_URLS=http://+:8080

      # Timezone for Quartz scheduler (West Africa Time)
      - TZ=${TZ:-Africa/Lagos}

      # JWT Configuration
      - JWT__Secret=${JWT_SECRET}

      # Database Connection Strings
      # Using the new server IP: 45.13.59.80
      - ConnectionStrings__SelfServiceConnect=${DB_SELFSERVICE_CONNECTION}
      - ConnectionStrings__EirsContext=${DB_EIRS_CONNECTION}
      - ConnectionStrings__ERASContext=${DB_ERAS_CONNECTION}
      - ConnectionStrings__PayeConnection=${DB_PAYE_CONNECTION}

      # External API Configuration
      - ConnectionStrings__ErasBaseUrl=${ERAS_BASE_URL:-https://apieirs.blouzatech.ng/}
      - ConnectionStrings__eirsusername=${EIRS_USERNAME}
      - ConnectionStrings__eirspassword=${EIRS_PASSWORD}

      # SMS Service (currently disabled in code)
      - ConnectionStrings__SmsBaseUrl=${SMS_BASE_URL:-https://app.multitexter.com/v2/app/sms}
      - ConnectionStrings__username=${SMS_USERNAME}
      - ConnectionStrings__password=${SMS_PASSWORD}

      # File paths (Linux paths)
      - ConnectionStrings__templatepath=/app/templates/
      - ConnectionStrings__upload=/app/uploads/

      # Quartz Job Schedule
      - ConnectionStrings__TaxOfficeJob=${TAX_OFFICE_JOB_CRON:-0 59 8 * * ?}

    # --------------------------------------------------------------------------
    # Volume mounts for data persistence
    # Left side = host path, Right side = container path
    # --------------------------------------------------------------------------
    volumes:
      # Upload files - persisted on host
      - paye-uploads:/app/uploads

      # Template files - can be customized
      - paye-templates:/app/templates

      # Error logs - for debugging and monitoring
      - paye-logs:/app/ErrorLog

      # Payload logs
      - paye-payloads:/app/Payloads

      # Application logs
      - paye-app-logs:/app/logs

      # HTML templates for PDF generation (read-only, pre-populated)
      # Uncomment to override built-in templates
      # - ./Document/HtmlContent:/app/Document/HtmlContent:ro

    # --------------------------------------------------------------------------
    # Health check configuration
    # Verifies API and database connectivity
    # --------------------------------------------------------------------------
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

    # --------------------------------------------------------------------------
    # Resource limits
    # Adjust based on your server capacity
    # --------------------------------------------------------------------------
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2G
        reservations:
          cpus: '0.5'
          memory: 512M

    # --------------------------------------------------------------------------
    # Logging configuration
    # Uses json-file driver with rotation
    # --------------------------------------------------------------------------
    logging:
      driver: "json-file"
      options:
        max-size: "50m"
        max-file: "5"

    # --------------------------------------------------------------------------
    # Network configuration
    # --------------------------------------------------------------------------
    networks:
      - paye-network

    # --------------------------------------------------------------------------
    # DNS configuration for external database access
    # Ensure container can resolve external SQL Server
    # --------------------------------------------------------------------------
    dns:
      - 8.8.8.8
      - 8.8.4.4

    # --------------------------------------------------------------------------
    # Extra hosts (optional)
    # Add custom hostname mappings if needed
    # --------------------------------------------------------------------------
    # extra_hosts:
    #   - "sqlserver:45.13.59.80"

    # --------------------------------------------------------------------------
    # Security options
    # --------------------------------------------------------------------------
    security_opt:
      - no-new-privileges:true

    # --------------------------------------------------------------------------
    # Read-only root filesystem (enhanced security)
    # Disabled by default as app needs to write logs
    # --------------------------------------------------------------------------
    # read_only: true

    # --------------------------------------------------------------------------
    # Temporary filesystems for runtime data
    # --------------------------------------------------------------------------
    tmpfs:
      - /tmp:size=100M

# ==============================================================================
# Named volumes for data persistence
# ==============================================================================
volumes:
  # Uploaded files storage
  paye-uploads:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${UPLOAD_VOLUME_PATH:-./data/uploads}

  # Template files storage
  paye-templates:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${TEMPLATE_VOLUME_PATH:-./data/templates}

  # Error logs storage
  paye-logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${LOGS_VOLUME_PATH:-./data/logs}

  # Payload logs storage
  paye-payloads:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${PAYLOADS_VOLUME_PATH:-./data/payloads}

  # Application logs
  paye-app-logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${APP_LOGS_VOLUME_PATH:-./data/app-logs}

# ==============================================================================
# Network configuration
# ==============================================================================
networks:
  paye-network:
    driver: bridge
    # Let Docker auto-assign subnet to avoid conflicts with other projects
