import type { FunctionalComponent } from "preact";
import type { ClassicTestResult } from "types";

import { TestResultDescription } from "@/components/TestResult/TestResultDescription";
import { TestResultError } from "@/components/TestResult/TestResultError";
import { TestResultLinks } from "@/components/TestResult/TestResultLinks";
import { TestResultMetadata } from "@/components/TestResult/TestResultMetadata";
import { TestResultParameters } from "@/components/TestResult/TestResultParameters";
import { TestResultSetup } from "@/components/TestResult/TestResultSetup";
import { TestResultSteps } from "@/components/TestResult/TestResultSteps";
import { TestResultTeardown } from "@/components/TestResult/TestResultTeardown";

import * as styles from "@/components/BaseLayout/styles.scss";

export type TestResultOverviewProps = {
  testResult?: ClassicTestResult;
};

export const TestResultOverview: FunctionalComponent<TestResultOverviewProps> = ({ testResult }) => {
  const { error, parameters, groupedLabels, links, descriptionHtml, setup, steps, teardown } = testResult || {};

  return (
    <>
      {Boolean(error?.message) && (
        <div className={styles["test-result-errors"]}>
          <TestResultError {...error} />
        </div>
      )}
      {Boolean(parameters?.length) && <TestResultParameters parameters={parameters} />}
      {Boolean(groupedLabels && Object.keys(groupedLabels || {})?.length) && (
        <TestResultMetadata testResult={testResult} />
      )}
      {Boolean(links?.length) && <TestResultLinks links={links} />}
      {Boolean(descriptionHtml) && <TestResultDescription descriptionHtml={descriptionHtml} />}
      <div className={styles["test-results"]}>
        {Boolean(setup?.length) && <TestResultSetup setup={setup} />}
        {Boolean(steps?.length) && <TestResultSteps steps={steps} />}
        {Boolean(teardown?.length) && <TestResultTeardown teardown={teardown} />}
      </div>
    </>
  );
};
