name: PR Checks

on:
  pull_request:
    types: [opened, edited, synchronize]

jobs:
  pr-title:
    name: PR Title Check
    runs-on: ubuntu-22.04
    steps:
      - name: Check PR title
        uses: amannn/action-semantic-pull-request@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          types: |
            feat
            fix
            docs
            style
            refactor
            test
            chore
            perf
            ci
            build
            revert
            deps
          requireScope: false
          subjectPattern: ^[a-zA-Z].*[^.]$
          subjectPatternError: |
            The subject "{subject}" found in the pull request title "{title}"
            doesn't match the configured pattern. Please ensure that the subject
            starts with a letter and doesn't end with a period.

  pr-size:
    name: PR Size Check
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Check PR size
        uses: actions/github-script@v7
        with:
          script: |
            const pr = context.payload.pull_request;
            const additions = pr.additions;
            const deletions = pr.deletions;
            const total = additions + deletions;
            
            console.log(`PR size: +${additions} -${deletions} (total: ${total})`);
            
            let label = '';
            if (total < 10) {
              label = 'size/XS';
            } else if (total < 100) {
              label = 'size/S';
            } else if (total < 500) {
              label = 'size/M';
            } else if (total < 1000) {
              label = 'size/L';
            } else {
              label = 'size/XL';
            }
            
            // Remove existing size labels
            const labels = await github.rest.issues.listLabelsOnIssue({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: pr.number
            });
            
            for (const label of labels.data) {
              if (label.name.startsWith('size/')) {
                try {
                  await github.rest.issues.removeLabel({
                    owner: context.repo.owner,
                    repo: context.repo.repo,
                    issue_number: pr.number,
                    name: label.name
                  });
                } catch (error) {
                  // Ignore errors when removing labels
                }
              }
            }
            
            // Add new size label
            try {
              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: pr.number,
                labels: [label]
              });
            } catch (error) {
              console.log(`Note: Could not add label '${label}'. Labels may need to be created in the repository.`);
            }

  conflicts-check:
    name: Merge Conflicts Check
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Check for conflicts
        uses: actions/github-script@v7
        with:
          script: |
            const pr = context.payload.pull_request;
            
            if (pr.mergeable_state === 'dirty') {
              core.setFailed('This PR has merge conflicts. Please resolve them.');
              
              try {
                await github.rest.issues.addLabels({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  issue_number: pr.number,
                  labels: ['has-conflicts']
                });
              } catch (error) {
                console.log("Note: Could not add 'has-conflicts' label. Labels may need to be created in the repository.");
              }
            } else {
              // Remove conflict label if it exists
              try {
                await github.rest.issues.removeLabel({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  issue_number: pr.number,
                  name: 'has-conflicts'
                });
              } catch (error) {
                // Label might not exist, ignore error
              }
            }