import { ReviewOutput, ReviewStatus } from './types';

/**
 * Generates the review comment based on given outputs and the commit the checks were run against.
 */
export function generateReviewBodyFromOutputs(
  outputs: ReviewOutput[],
  hasComments: boolean,
  commitSha: string
): string {
  return [
    "*Hi there! 👋 I'm a bot whose goal is to ensure your contributions meet our guidelines.*",
    header(outputs, hasComments),
    outputs.map(reportForOutput).join('\n'),
    footerForCommit(commitSha),
  ]
    .filter(Boolean)
    .join('\n\n');
}

/**
 * Generates a report based on given review output.
 */
function reportForOutput(output: ReviewOutput): string {
  return `<details>
  <summary><strong>${prefixForStatus(output.status)}</strong>: ${output.title}</summary>

&NewLine;
${output.body}
</details>

---`;
}

/**
 * Returns appropriate header for the review, depending on review results.
 */
function header(outputs: ReviewOutput[], hasComments: boolean): string {
  if (outputs.length > 0) {
    return `I've found some issues in your pull request that should be addressed (click on them to expand) 👇`;
  }
  if (hasComments) {
    return `It's pretty good 👍 I just have a few comments to consider 👇`;
  }
  return 'Looks like I have nothing to complain about 👏 Keep up the good work! 💪';
}

/**
 * Returns review body footer containing commit hash against which the review was made.
 */
function footerForCommit(sha: string): string {
  return `*Generated by ExpoBot 🤖 against ${sha}*`;
}

/**
 * Returns title prefix depending on the status.
 */
function prefixForStatus(status: ReviewStatus): string {
  switch (status) {
    case ReviewStatus.WARN:
      return '⚠️ Warning';
    case ReviewStatus.ERROR:
      return '❌ Error';
  }
  return '';
}
