name: regtest

on:
  workflow_call:
    inputs:
      custom-pytest:
        description: "Custom pytest arguments"
        required: true
        type: string
      python-version:
        default: "3.9"
        type: string
      os-version:
        default: "ubuntu-latest"
        type: string
      backend-wallet-class:
        required: true
        type: string
    secrets:
      CODECOV_TOKEN:
        required: true

jobs:
  regtest:
    runs-on: ${{ inputs.os-version }}
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4

      - name: docker build
        if: ${{ inputs.backend-wallet-class == 'LNbitsWallet' }}
        run: |
          docker build -t lnbits/lnbits .

      - name: Setup Regtest
        run: |
          git clone https://github.com/lnbits/legend-regtest-enviroment.git docker
          cd docker
          chmod +x ./tests
          ./tests
          sudo chmod -R a+rwx .

      - uses: ./.github/actions/prepare
        with:
          python-version: ${{ inputs.python-version }}

      - name: Run pytest
        uses: pavelzw/pytest-action@v2
        env:
          LNBITS_DATABASE_URL: ${{ inputs.db-url }}
          LNBITS_BACKEND_WALLET_CLASS: ${{ inputs.backend-wallet-class }}
          LND_REST_ENDPOINT: https://localhost:8081/
          LND_REST_CERT: ./docker/data/lnd-3/tls.cert
          LND_REST_MACAROON: ./docker/data/lnd-3/data/chain/bitcoin/regtest/admin.macaroon
          LND_GRPC_ENDPOINT: localhost
          LND_GRPC_PORT: 10009
          LND_GRPC_CERT: docker/data/lnd-3/tls.cert
          LND_GRPC_MACAROON: docker/data/lnd-3/data/chain/bitcoin/regtest/admin.macaroon
          CORELIGHTNING_RPC: ./docker/data/clightning-1/regtest/lightning-rpc
          CORELIGHTNING_REST_URL: https://localhost:3001
          CORELIGHTNING_REST_MACAROON: ./docker/data/clightning-2-rest/access.macaroon
          CORELIGHTNING_REST_CERT: ./docker/data/clightning-2-rest/certificate.pem
          LNBITS_ENDPOINT: http://localhost:5001
          LNBITS_KEY: "d08a3313322a4514af75d488bcc27eee"
          ECLAIR_URL: http://127.0.0.1:8082
          ECLAIR_PASS: lnbits
          PYTHONUNBUFFERED: 1
          DEBUG: true
        with:
          verbose: true
          job-summary: true
          emoji: false
          click-to-expand: true
          custom-pytest: ${{ inputs.custom-pytest }}
          report-title: "regtest (${{ inputs.python-version }}, ${{ inputs.backend-wallet-class }}"

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          file: ./coverage.xml
          token: ${{ secrets.CODECOV_TOKEN }}
          verbose: true

      - name: docker lnbits logs
        if: ${{ inputs.backend-wallet-class == 'LNbitsWallet' }}
        run: |
          docker logs lnbits-lnbits-1
