name: CI

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

env:
  UV_VERSION: "0.5.13"
  PYTHON_VERSION: "3.12"

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install UV
        run: |
          curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install dependencies
        run: |
          uv pip install --system -e ".[dev]"

      - name: Run ruff check
        run: |
          uv run ruff check src tests

      - name: Run ruff format check
        run: |
          uv run ruff format --check src tests

  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install UV
        run: |
          curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install dependencies
        run: |
          uv pip install --system -e ".[dev]"

      - name: Run mypy
        run: |
          uv run mypy src

  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install UV
        run: |
          curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install xdelta3
        run: |
          sudo apt-get update
          sudo apt-get install -y xdelta3

      - name: Install dependencies
        run: |
          uv pip install --system -e ".[dev]"

      - name: Run unit tests
        run: |
          uv run pytest tests/unit -v --tb=short

      - name: Run integration tests
        run: |
          uv run pytest tests/integration -v --tb=short


  e2e-test:
    runs-on: ubuntu-latest
    services:
      localstack:
        image: localstack/localstack:latest
        ports:
          - 4566:4566
        env:
          SERVICES: s3
          DEBUG: 0
          DATA_DIR: /tmp/localstack/data
        options: >-
          --health-cmd "curl -f http://localhost:4566/_localstack/health"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4

      - name: Install UV
        run: |
          curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Install xdelta3
        run: |
          sudo apt-get update
          sudo apt-get install -y xdelta3

      - name: Install dependencies
        run: |
          uv pip install --system -e ".[dev]"

      - name: Run E2E tests
        env:
          AWS_ACCESS_KEY_ID: test
          AWS_SECRET_ACCESS_KEY: test
          AWS_DEFAULT_REGION: us-east-1
          AWS_ENDPOINT_URL: http://localhost:4566
        run: |
          uv run pytest tests/e2e -v --tb=short

