services:
  history_db:
    image: azenkain/postgres-postgis-pgvector:18
    container_name: history_db
    restart: unless-stopped

    env_file:
      - ./assets/resources/.env
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
      - PGDATA=/var/lib/postgresql/data
    volumes:
      - history_db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - history-api-project

  history_cache:
    image: redis:8.6.2-alpine
    container_name: history_cache
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - history_cache_data:/data
    networks:
      - history-api-project

  history_migrate:
    build:
      context: .
      dockerfile_inline: |
        FROM migrate/migrate
        COPY db/migrations /migrations
    container_name: history_migrate
    depends_on:
      history_db:
        condition: service_healthy
    env_file:
      - ./assets/resources/.env
    entrypoint:
      - sh
      - -c
      - |
        DB_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@history_db:5432/${POSTGRES_DB}?sslmode=disable"

        for i in 1 2 3; do
          echo "Migration attempt $$i..."
          if /migrate -path /migrations -database "$$DB_URL" up; then
            echo "Migration success"
            exit 0
          fi
          sleep 2
        done

        echo "Migration failed after 3 attempts"
        exit 1
    networks:
      - history-api-project

  history_app:
    build: .
    container_name: history_app
    restart: unless-stopped
    depends_on:
      history_migrate:
        condition: service_completed_successfully
      history_db:
        condition: service_healthy
      history_cache:
        condition: service_started
    env_file:
      - ./assets/resources/.env
    ports:
      - "3344:3344"
    networks:
      - history-api-project

  history_email_worker:
    build: .
    container_name: history_email_worker
    restart: unless-stopped
    depends_on:
      history_db:
        condition: service_healthy
      history_cache:
        condition: service_started
    env_file:
      - ./assets/resources/.env
    command: ["./email-worker"]
    networks:
      - history-api-project

  history_storage_worker:
    build: .
    container_name: history_storage_worker
    restart: unless-stopped
    depends_on:
      history_db:
        condition: service_healthy
      history_cache:
        condition: service_started
    env_file:
      - ./assets/resources/.env
    command: ["./storage-worker"]
    networks:
      - history-api-project

  history_rag_worker:
    build: .
    container_name: history_rag_worker
    restart: unless-stopped
    depends_on:
      history_db:
        condition: service_healthy
      history_cache:
        condition: service_started
    env_file:
      - ./assets/resources/.env
    command: ["./rag-worker"]
    networks:
      - history-api-project

  history_cron_worker:
    build: .
    container_name: history_cron_worker
    restart: unless-stopped
    depends_on:
      history_db:
        condition: service_healthy
      history_cache:
        condition: service_started
    env_file:
      - ./assets/resources/.env
    command: ["./cron-worker"]
    networks:
      - history-api-project

volumes:
  history_db_data:
  history_cache_data:

networks:
  history-api-project: