services:
  function:
    platform: linux/amd64
    build:
      context: .
      target: deploy
      args:
        version: "local-dev"
        AWS_ACCOUNT: "086679231553"
    volumes:
      - ./src:/var/task/src:cached
      - ./config.py:/var/task/config.py:cached
    ports:
      - "9000:8080"
    environment:
      - DD_ENV=dev
      - DD_SERVICE=lambda-abacus-payment-allocation
      - DD_AGENT_HOST=dev-datadog-agent.dev.theorchard.io
      - DD_TRACE_ENABLED=true
      - DD_SERVERLESS_FLUSH_STRATEGY=periodically,1
      - LOG_LEVEL=INFO
      - ENVIRONMENT=dev
      - AWS_ACCESS_KEY_ID
      - AWS_SECRET_ACCESS_KEY
      - AWS_SESSION_TOKEN
      - AWS_REGION

  lint-and-test:
    platform: linux/amd64
    build:
      context: .
      target: unit-lint
      args:
        AWS_ACCOUNT: "086679231553"
    volumes:
      - ./src:/var/task/src:cached
      - ./config.py:/var/task/config.py:cached
      - ./tests:/var/task/tests:cached
    command: ./lint-and-test.sh
    environment:
      - SKIP_LINT=0
      - TEST_ARGS=-v
      - COV_REPORT=term  # --cov-report pytest-cov option or OFF (try html!)
      - ENVIRONMENT=dev
      - RUN_INTEGRATION=0

  lockfile:
    platform: linux/amd64
    build:
      context: .
      target: base
      args:
        AWS_ACCOUNT: "086679231553"
    volumes:
      - ./:/var/task:cached
    entrypoint: ["uv"]
    command: ["lock", "--upgrade"]

  # Database migration runner
  liquibase-runner:
    image: 437795906767.dkr.ecr.us-east-1.amazonaws.com/royalties-liquibase-runner:latest
    depends_on:
      - mysql
    environment:
      - MYSQL_DB_HOST=mysql
      - MYSQL_DB_PORT=3306
      - MYSQL_DB_NAME=royalty_accounting
      - MYSQL_DEPLOY_USER=root
      - MYSQL_DEPLOY_PASSWORD=root
      - MYSQL_SERVICE_USER=royalties
      - GITHUB_USER=theorchard
      - GITHUB_REPO=database
      - GITHUB_BRANCH=master
      - GITHUB_REPO_DIR=royalty_accounting
      # AWS credentials for accessing resources
      - AWS_ACCESS_KEY_ID
      - AWS_DEFAULT_REGION=us-east-1
      - AWS_SECRET_ACCESS_KEY
      - AWS_SESSION_TOKEN

  # MySQL database for testing
  mysql:
    image: mysql:8.0.36
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=royalty_accounting
      - MYSQL_USER=royalties
      - MYSQL_PASSWORD=1234
    ports:
      - "6767:3306"

  # Deployed Lambda image from ECR (for testing deployed builds)
  lambda:
    image: 086679231553.dkr.ecr.us-east-1.amazonaws.com/lambda-abacus-payment-allocation:latest
    depends_on:
      - liquibase-runner
    environment:
      # Environment
      - ENVIRONMENT=${ENVIRONMENT:-qa}
      # AWS credentials
      - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
      - AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
      - AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN
      - OWSREQUEST_SERVICE_MAP={"ows-royalties":"https://qa-ows-royalties.theorchard.io","ows-abacus-state":"https://qa-ows-abacus-state.theorchard.io"}
    ports:
      - "6817:8080"

  # Integration tests
  integration-test:
    platform: linux/amd64
    build:
      context: .
      target: unit-lint
      args:
        AWS_ACCOUNT: "086679231553"
    volumes:
      - ./src:/var/task/src:cached
      - ./config.py:/var/task/config.py:cached
      - ./tests:/var/task/tests:cached
    ports:
      - "8000:8000"
    command: ./lint-and-test.sh
    environment:
      - SKIP_LINT=1
      - TEST_ARGS=-vv
      - COV_REPORT=term  # --cov-report pytest-cov option or OFF (try html!)
      - ENVIRONMENT=dev
      - RUN_INTEGRATION=1
