local utils = import 'utils.libjsonnet';

function(settings)
  local is_uv = settings.package_manager == 'uv';
  {
    jobs: {
      build: {
        'runs-on': '${{ matrix.system.image }}',
        steps: [
          {
            uses: 'actions/checkout@' + utils.githubLatestActionTag('actions', 'checkout'),
          },
        ] + (if settings.private then [{
               env: {
                 GH_TOKEN: '${{ secrets.GITHUB_TOKEN }}',
               },
               name: 'Delete old caches and artifacts',
               run: |||
                 gh cache list --json id,createdAt -q '.[] | select((now - (.createdAt | sub("\\.[0-9]+"; "") | fromdateiso8601)) > 43200) | .key' | xargs -r -L1 gh cache delete
                 gh api repos/${{ github.repository }}/actions/artifacts --paginate -q '.artifacts[] | select((now - (.created_at | sub("\\.[0-9]+"; "") | fromdateiso8601)) > 43200) | .id' | xargs -r -I{} gh api -X DELETE repos/${{ github.repository }}/actions/artifacts/{}
               |||,
             }] else []) + (if !is_uv then [{
                              name: 'Fix pyproject.toml for older Poetry',
                              run: |||
                                pipx install yq
                                name=$(tomlq -r '.project.name' pyproject.toml)
                                version=$(tomlq -r '.project.version' pyproject.toml)
                                description=$(tomlq -r '.project.description' pyproject.toml)
                                author_name=$(tomlq -r '.project.authors[0].name' pyproject.toml)
                                author_email=$(tomlq -r '.project.authors[0].email' pyproject.toml)
                                # Create temporary file with the new fields
                                cat > /tmp/poetry_fields.toml << EOF
                                authors = ["$author_name <$author_email>"]
                                description = "$description"
                                name = "$name"
                                version = "$version"
                                EOF
                                # Insert the fields after [tool.poetry] line
                                awk '
                                  /^\[tool\.poetry\]$/ {
                                    print
                                    while ((getline line < "/tmp/poetry_fields.toml") > 0) {
                                      print line
                                    }
                                    close("/tmp/poetry_fields.toml")
                                    next
                                  }
                                  { print }
                                ' pyproject.toml > /tmp/pyproject.toml.new
                                mv /tmp/pyproject.toml.new pyproject.toml
                              |||,
                            }] else []) + [
          {
            name: 'Build Snap package',
            uses: 'snapcore/action-build@' + utils.githubLatestActionTag('snapcore', 'action-build'),
            id: 'build',
            with: {
              'snapcraft-args': 'pack',
              'snapcraft-channel': 'latest/stable',
            },
          },
          {
            name: 'Upload Artifacts',
            uses: 'actions/upload-artifact@' + utils.githubLatestActionTag('actions', 'upload-artifact'),
            with: {
              'if-no-files-found': 'error',
              name: '%s-snap-${{ matrix.system.arch }}' % settings.project_name,
              path: '*.snap',
            },
          },
          {
            name: 'Attest',
            uses: 'actions/attest@' + utils.githubLatestActionTag('actions', 'attest'),
            with: {
              'subject-path': '*.snap',
            },
          },
          {
            'if': "github.ref_type == 'tag'",
            name: 'Upload package',
            uses: 'softprops/action-gh-release@' + utils.githubLatestActionTag('softprops', 'action-gh-release'),
            with: {
              draft: true,
              fail_on_unmatched_files: true,
              files: '*.snap',
            },
          },
        ],
        strategy: {
          matrix: {
            system: [
              {
                arch: 'amd64',
                image: 'ubuntu-24.04',
              },
              {
                arch: 'arm64',
                image: 'ubuntu-24.04-arm',
              },
            ],
          },
        },
      },
    },
    name: 'Snap',
    on: {
      push: {
        branches: [
          'master',
        ],
        paths: [
          '**/*.py',
          '**/*.pyi',
          '.github/workflows/snap.yml',
          'pyproject.toml',
          'snapcraft.yaml',
          'uv.lock',
          'poetry.lock',
        ],
        tags: [
          'v*.*.*',
        ],
      },
      workflow_dispatch: null,
    },
    permissions: (if settings.private then { actions: 'write' } else {}) + {
      attestations: 'write',
      contents: 'write',
      'id-token': 'write',
    },
  }
