export type QaReportCheck = {
  name: string;
  status: "pass" | "fail" | "skip";
  details?: string;
};

export type QaReportScenario = {
  name: string;
  status: "pass" | "fail" | "skip";
  details?: string;
  steps?: QaReportCheck[];
};

export function renderQaMarkdownReport(params: {
  title: string;
  startedAt: Date;
  finishedAt: Date;
  checks?: QaReportCheck[];
  scenarios?: QaReportScenario[];
  timeline?: string[];
  notes?: string[];
}) {
  const checks = params.checks ?? [];
  const scenarios = params.scenarios ?? [];
  const passCount =
    checks.filter((check) => check.status === "pass").length +
    scenarios.filter((scenario) => scenario.status === "pass").length;
  const failCount =
    checks.filter((check) => check.status === "fail").length +
    scenarios.filter((scenario) => scenario.status === "fail").length;

  const lines = [
    `# ${params.title}`,
    "",
    `- Started: ${params.startedAt.toISOString()}`,
    `- Finished: ${params.finishedAt.toISOString()}`,
    `- Duration ms: ${params.finishedAt.getTime() - params.startedAt.getTime()}`,
    `- Passed: ${passCount}`,
    `- Failed: ${failCount}`,
    "",
  ];

  if (checks.length > 0) {
    lines.push("## Checks", "");
    for (const check of checks) {
      lines.push(`- [${check.status === "pass" ? "x" : " "}] ${check.name}`);
      if (check.details) {
        lines.push(`  - ${check.details}`);
      }
    }
  }

  if (scenarios.length > 0) {
    lines.push("", "## Scenarios", "");
    for (const scenario of scenarios) {
      lines.push(`### ${scenario.name}`);
      lines.push("");
      lines.push(`- Status: ${scenario.status}`);
      if (scenario.details) {
        lines.push(`- Details: ${scenario.details}`);
      }
      if (scenario.steps?.length) {
        lines.push("- Steps:");
        for (const step of scenario.steps) {
          lines.push(`  - [${step.status === "pass" ? "x" : " "}] ${step.name}`);
          if (step.details) {
            lines.push(`    - ${step.details}`);
          }
        }
      }
      lines.push("");
    }
  }

  if (params.timeline && params.timeline.length > 0) {
    lines.push("## Timeline", "");
    for (const item of params.timeline) {
      lines.push(`- ${item}`);
    }
  }

  if (params.notes && params.notes.length > 0) {
    lines.push("", "## Notes", "");
    for (const note of params.notes) {
      lines.push(`- ${note}`);
    }
  }

  lines.push("");
  return lines.join("\n");
}
