import { motion } from "motion/react";

type InputField = {
  name: string;
  type: string;
  placeholder: string;
  required?: boolean;
};

type TextareaField = {
  name: string;
  placeholder: string;
  rows?: number;
  required?: boolean;
};

type ContactSplitFormProps = {
  tag: string;
  title: string;
  description: string;
  inputs: InputField[];
  textarea?: TextareaField;
  buttonText: string;
} & ({ imageSrc: string; videoSrc?: never } | { videoSrc: string; imageSrc?: never });

const ContactSplitForm = ({
  tag,
  title,
  description,
  inputs,
  textarea,
  buttonText,
  imageSrc,
  videoSrc,
}: ContactSplitFormProps) => {
  return (
    <section
      data-webild-section="ContactSplitForm"
      aria-label="Contact section"
      className="relative w-full py-20"
    >
      <div className="w-content-width mx-auto">
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          whileInView={{ opacity: 1, y: 0 }}
          viewport={{ once: true, margin: "-15%" }}
          transition={{ duration: 0.6, ease: "easeOut" }}
          className="grid grid-cols-1 md:grid-cols-2 md:auto-rows-fr gap-5"
        >
          <div className="p-5 md:p-8 card rounded-theme">
            <form className="flex flex-col gap-5">
              <div className="flex flex-col items-center gap-1 text-center">
                <span className="card rounded-full px-3 py-1 text-sm">{tag}</span>

                <motion.h2
                  initial={{ opacity: 0, y: 20 }}
                  whileInView={{ opacity: 1, y: 0 }}
                  viewport={{ once: true, margin: "-15%" }}
                  transition={{ duration: 0.6, delay: 0.05, ease: "easeOut" }}
                  className="text-4xl font-medium text-balance"
                >
                  {title}
                </motion.h2>

                <motion.p
                  initial={{ opacity: 0, y: 20 }}
                  whileInView={{ opacity: 1, y: 0 }}
                  viewport={{ once: true, margin: "-15%" }}
                  transition={{ duration: 0.6, delay: 0.15, ease: "easeOut" }}
                  className="text-sm md:text-base leading-tight text-balance"
                >
                  {description}
                </motion.p>
              </div>

              <div className="flex flex-col gap-3">
                {inputs.map((input) => (
                  <input
                    key={input.name}
                    type={input.type}
                    name={input.name}
                    placeholder={input.placeholder}
                    required={input.required}
                    aria-label={input.placeholder}
                    className="card rounded-theme h-9 px-3 w-full text-sm"
                  />
                ))}

                {textarea && (
                  <textarea
                    name={textarea.name}
                    placeholder={textarea.placeholder}
                    required={textarea.required}
                    rows={textarea.rows || 5}
                    aria-label={textarea.placeholder}
                    className="card rounded-theme p-3 w-full text-sm"
                  />
                )}

                <button
                  type="submit"
                  className="primary-button rounded-theme h-9 px-6 inline-flex items-center justify-center text-sm text-primary-cta-text"
                >
                  {buttonText}
                </button>
              </div>
            </form>
          </div>

          <div className="h-100 md:h-full card rounded-theme overflow-hidden">
            {videoSrc ? (
              <video
                src={videoSrc}
                autoPlay
                muted
                loop
                playsInline
                aria-label="Contact video"
                className="w-full h-full object-cover"
              />
            ) : (
              <img
                src={imageSrc}
                alt=""
                className="w-full h-full object-cover"
              />
            )}
          </div>
        </motion.div>
      </div>
    </section>
  );
};

export default ContactSplitForm;
