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

// We add this at the top of each report to identify which ones come from this command.
export const COMMENT_HEADER = `<!-- Made with ❤️ by \`expotools code-review\` -->`;

/**
 * Generates the report based on given outputs and the commit the checks were run against.
 */
export function generateReportFromOutputs(outputs: ReviewOutput[], commitSha: string): string {
  return [
    COMMENT_HEADER,
    "*Hi there! 👋 I'm a bot whose goal is to ensure your contributions meet our guidelines.*",
    header(outputs),
    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[]): string {
  if (outputs.length > 0) {
    return `I've found some issues in your pull request that should be addressed (click on them for more details) 👇`;
  }
  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 `---\n*Generated by ExpoBot 🤖 against ${sha}*`;
}

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