name: Flag issues with bad titles

on:
  issues:
    types: [opened]

permissions:
  issues: write

jobs:
  check-title:
    runs-on: ubuntu-latest
    steps:
      - name: Flag or redirect issue
        uses: actions/github-script@v7
        with:
          script: |
            const title = context.payload.issue.title.trim();
            const titleLower = title.toLowerCase();

            const badTitles = [
              "[bug]", "bug report", "bug", "issue",
              "help", "question", "test", "...", "untitled"
            ];

            const featureRequestTitles = [
              "feature request", "[feature]", "[feature request]", "[enhancement]"
            ];

            if (badTitles.includes(titleLower)) {
              // Ensure the label exists
              try {
                await github.rest.issues.getLabel({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  name: 'invalid-title',
                });
              } catch {
                await github.rest.issues.createLabel({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  name: 'invalid-title',
                  color: 'e4e669',
                  description: 'Issue title does not meet quality standards',
                });
              }

              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.payload.issue.number,
                labels: ['invalid-title'],
              });

              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.payload.issue.number,
                body: [
                  '## Invalid title',
                  '',
                  `Your issue title \`${title}\` is too generic to be actionable.`,
                  '',
                  'Please edit the title to something descriptive that summarizes the problem — for example:',
                  '> _Map view crashes when zooming in on Safari 17_',
                  '',
                  '**This issue will be automatically closed in 24 hours if the title has not been updated.**',
                ].join('\n'),
              });

            } else if (featureRequestTitles.some(t => titleLower.startsWith(t))) {
              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.payload.issue.number,
                body: [
                  '## Wrong place for feature requests',
                  '',
                  'Feature requests should be submitted in [Discussions](https://github.com/mauriceboe/TREK/discussions/new?category=feature-requests), not as issues.',
                  '',
                  'This issue has been closed. Feel free to re-submit your idea in the right place!',
                ].join('\n'),
              });

              await github.rest.issues.update({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.payload.issue.number,
                state: 'closed',
                state_reason: 'not_planned',
              });
            }
