const core = require('@actions/core');
const github = require('@actions/github');
const octokit = new github.GitHub(process.env.GITHUB_TOKEN);

async function run() {
  try {
    const username = github.context.payload.pull_request.user.login;
    const repo = github.context.payload.repository;
    
    const { data: prs } = await octokit.pulls.list({
      owner: repo.owner.login,
      repo: repo.name,
      state: 'open',
    });
    
    const userOpenPRs = prs.filter((pr) => pr.user.login === username);
    
    const hasHacktoberfestLabel = github.context.payload.pull_request.labels.some(label => label.name === 'hacktoberfest-accepted');

    if (userOpenPRs.length > 2 && !hasHacktoberfestLabel) {
      core.setFailed(`The user ${username} has more than 2 open Pull Requests. Please add the 'hacktoberfest-accepted' label to this PR.`);
    }
  } catch (error) {
    core.setFailed(error.message);
  }
}

run();