"use client";

import type { QuestionField } from "@/lib/schema-adapter";
import QuestionBirthplace from "./question-birthplace";
import QuestionButton from "./question-button";
import QuestionCheckbox from "./question-checkbox";
import QuestionDate from "./question-date";
import QuestionSheet from "./question-sheet";
import QuestionFile from "./question-file";
import QuestionNumber from "./question-number";
import QuestionPhone from "./question-phone";
import QuestionPhoto from "./question-photo";
import QuestionRadio from "./question-radio";
import QuestionSlider from "./question-slider";
import QuestionText from "./question-text";
import QuestionTextarea from "./question-textarea";

type QuestionRendererProps = {
  question: QuestionField;
  disabled?: boolean;
  dobQuestion?: QuestionField;
};

export function QuestionRenderer({
  question,
  disabled,
  dobQuestion,
}: QuestionRendererProps) {
  const compactTextHeight =
    question.type === "email" || question.ui_config?.compact === true
      ? "h-[54px]"
      : undefined;

  switch (question.type) {
    case "birthplace":
    case "location":
    case "residence":
      return (
        <QuestionBirthplace
          question={question}
          disabled={disabled}
        />
      );
    case "button":
      return (
        <QuestionButton
          question={question}
          disabled={disabled}
        />
      );
    case "checkbox":
      return (
        <QuestionCheckbox
          question={question}
          disabled={disabled}
        />
      );
    case "date":
      return (
        <QuestionDate
          question={question}
          disabled={disabled}
        />
      );
    case "dropdown":
      return (
        <QuestionSheet
          question={question}
          disabled={disabled}
        />
      );
    case "file":
      return (
        <QuestionFile
          question={question}
          disabled={disabled}
        />
      );
    case "number":
      if (
        question.ui_config?.derivedFromDob === true &&
        dobQuestion
      ) {
        return (
          <QuestionNumber
            question={question}
            derivedFromQuestion={dobQuestion}
            disabled={disabled}
          />
        );
      }

      return (
        <QuestionNumber
          question={question}
          disabled={disabled}
        />
      );
    case "phone":
      return (
        <QuestionPhone
          question={question}
          disabled={disabled}
        />
      );
    case "photo":
      return (
        <QuestionPhoto
          question={question}
          disabled={disabled}
        />
      );
    case "radio":
      return (
        <QuestionRadio
          question={question}
          disabled={disabled}
        />
      );
    case "slider":
    case "scale":
    case "range":
      return (
        <QuestionSlider
          question={question}
          disabled={disabled}
        />
      );
    case "text":
      if (
        question.ui_config?.enable_geoip === true ||
        question.ui_config?.location_kind === "residence" ||
        question.id?.includes("approximate_location_of_current_living_area") ||
        question.id?.includes("current_residence") ||
        question.id?.includes("living_area")
      ) {
        return (
          <QuestionBirthplace
            question={question}
            disabled={disabled}
          />
        );
      }
      return (
        <QuestionText
          question={question}
          heightClassName={compactTextHeight}
          disabled={disabled}
        />
      );
    case "textarea":
      return (
        <QuestionTextarea
          question={question}
          disabled={disabled}
        />
      );
    default:
      return null;
  }
}

export default QuestionRenderer;
