version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    container_name: codeflash-postgres
    environment:
      POSTGRES_DB: codeflash
      POSTGRES_USER: codeflash
      POSTGRES_PASSWORD: codeflash
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U codeflash"]
      interval: 5s
      timeout: 5s
      retries: 5

  db-init:
    image: codeflash/cf-api:latest
    container_name: codeflash-db-init
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql://codeflash:codeflash@postgres:5432/codeflash
    command: >
      sh -c "
        echo 'Running Prisma migrations...' &&
        cd /app/common &&
        npx prisma migrate deploy &&
        echo 'Checking for existing users...' &&
        USER_COUNT=$$(psql postgresql://codeflash:codeflash@postgres:5432/codeflash -t -c 'SELECT COUNT(*) FROM users;' 2>/dev/null | tr -d ' ' || echo '0') &&
        if [ \"$$USER_COUNT\" = \"0\" ]; then
          echo 'Creating default user and API key...' &&
          API_KEY=\"cf_$$(openssl rand -hex 32)\" &&
          SUFFIX=\"$${API_KEY: -4}\" &&
          psql postgresql://codeflash:codeflash@postgres:5432/codeflash <<-EOSQL &&
            INSERT INTO users (user_id, github_username, email, name, onboarding_completed, created_at)
            VALUES ('local|default-user', 'codeflash-user', 'user@codeflash.local', 'Default User', true, NOW())
            ON CONFLICT (user_id) DO NOTHING;
            INSERT INTO cf_api_keys (key, suffix, name, user_id, tier, created_at)
            VALUES ('$$API_KEY', '$$SUFFIX', 'Default API Key', 'local|default-user', 'free', NOW());
EOSQL
          echo '' &&
          echo '======================================' &&
          echo '  CODEFLASH API KEY CREATED' &&
          echo '======================================' &&
          echo '' &&
          echo \"$$API_KEY\" &&
          echo '' &&
          echo 'Add to cli/codeflash/.env:' &&
          echo \"CODEFLASH_API_KEY=$$API_KEY\" &&
          echo 'CODEFLASH_AIS_SERVER=local' &&
          echo 'CODEFLASH_CFAPI_SERVER=local' &&
          echo '' &&
          echo '======================================';
        else
          echo 'Users exist. To get API key run:' &&
          echo 'docker exec codeflash-postgres psql -U codeflash -d codeflash -c \"SELECT key FROM cf_api_keys LIMIT 1;\"';
        fi &&
        echo 'Database ready!'
      "
    restart: "no"

  aiservice:
    image: codeflash/aiservice:latest
    container_name: codeflash-aiservice
    depends_on:
      postgres:
        condition: service_healthy
      db-init:
        condition: service_completed_successfully
    environment:
      DATABASE_URL: postgresql://codeflash:codeflash@postgres:5432/codeflash
      SECRET_KEY: development-secret-key
      OPENAI_API_TYPE: azure
      AZURE_OPENAI_API_KEY: ${AZURE_OPENAI_API_KEY}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
      ENVIRONMENT: DEVELOPMENT
    ports:
      - "8000:8000"

  cf-api:
    image: codeflash/cf-api:latest
    container_name: codeflash-cf-api
    depends_on:
      postgres:
        condition: service_healthy
      db-init:
        condition: service_completed_successfully
      aiservice:
        condition: service_started
    environment:
      DATABASE_URL: postgresql://codeflash:codeflash@postgres:5432/codeflash
      AISERVICE_URL: http://aiservice:8000
      GH_APP_ID: 800528
      GH_APP_USER_ID: 148906541
      GH_APP_WEBHOOK_SECRET: dev-webhook-secret
      SECRET_KEY: development-secret-key
      NODE_ENV: local
    ports:
      - "3001:3001"

volumes:
  postgres_data: