version: '3.8'

services:
  timescaledb:
    image: timescale/timescaledb:latest-pg16
    container_name: meta_timescaledb
    ports:
      - "5555:5432"
    environment:
      POSTGRES_DB: meta_insights
      POSTGRES_USER: meta_user
      POSTGRES_PASSWORD: meta_password
    volumes:
      - timescale_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U meta_user -d meta_insights"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  # View Manager - Setup database schema and views
  view-manager:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: view_manager
    environment:
      # Database connection (connects to timescaledb service)
      DATABASE_URL: postgresql://meta_user:meta_password@timescaledb:5432/meta_insights
    env_file:
      - .env  # Optional: for any additional configuration
    volumes:
      # Mount metadata.yaml for account configuration
      - ./metadata.yaml:/app/metadata.yaml:ro
      # Optional: Mount custom db_schema.sql if you want to edit it externally
      # - ./src/meta_api_grabber/db_schema.sql:/app/src/meta_api_grabber/db_schema.sql:ro
    depends_on:
      timescaledb:
        condition: service_healthy
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Optional: Grafana for visualization
  grafana:
    image: grafana/grafana:latest
    container_name: meta_grafana
    ports:
      - "3555:3000"
    environment:
      GF_SECURITY_ADMIN_USER: admin
      GF_SECURITY_ADMIN_PASSWORD: admin
      GF_INSTALL_PLUGINS: grafana-clock-panel
    volumes:
      - grafana_data:/var/lib/grafana
    depends_on:
      timescaledb:
        condition: service_healthy
    restart: unless-stopped

volumes:
  timescale_data:
  grafana_data:
