"use client";

import type { Dictionary } from "@/i18n/get-dictionary";
import { submitServiceRequest } from "@/lib/actions/service-request";
import { errorMessage } from "@/lib/forms/error-message";
import { useFormAction } from "@/lib/forms/use-form-action";
import { validateRequest } from "@/lib/forms/validation";
import { FormFailure } from "@/components/shared/form-failure";
import { FormFooter } from "@/components/shared/form-footer";
import { FormSuccess } from "@/components/shared/form-success";
import { HiddenLabelInput } from "@/components/shared/hidden-label-input";
import { ServicePicker } from "@/components/shared/service-picker";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Textarea } from "@/components/ui/textarea";

type ServiceRequestFormProps = {
  t: Dictionary["serviceRequest"]["form"];
  /** "Select Service" legend and tile labels (shared with the Home form). */
  picker: Pick<Dictionary["home"]["contact"]["form"], "selectService" | "services">;
  /** Per-field error messages (shared by all forms). */
  formText: Dictionary["forms"];
};

// Company is required on this page (Figma marks it with *).
const validate = (data: FormData) => validateRequest(data, { companyRequired: true });

/**
 * The form inside the "Let's Build Something Great Together" card.
 * Figma: fields 20px apart with the card's 20px sides; the six service tiles in one row on desktop;
 * a footer bar (32px sides, 24px top/bottom) with "Cancel" (clears the form) and "Send Request".
 */
export function ServiceRequestForm({ t, picker, formText }: ServiceRequestFormProps) {
  const { errors, onSubmit, onChange, pending, sent, failed, sendAgain } = useFormAction(submitServiceRequest, validate);
  const err = (field: string) => errorMessage(errors, formText.errors, field);

  if (sent) {
    const { sentTitle, sendAnother } = formText.status;
    return <FormSuccess title={sentTitle} message={t.success} againLabel={sendAnother} onAgain={sendAgain} />;
  }

  return (
    <form onSubmit={onSubmit} onChange={onChange} noValidate>
      <FieldGroup className="px-5">
        <div className="grid grid-cols-2 gap-5">
          <HiddenLabelInput name="fullName" label={t.fullName} required autoComplete="name" error={err("fullName")} />
          <HiddenLabelInput name="company" label={t.company} required autoComplete="organization" error={err("company")} />
          <HiddenLabelInput name="phone" label={t.phone} type="tel" required autoComplete="tel" error={err("phone")} />
          <HiddenLabelInput name="email" label={t.email} type="email" required autoComplete="email" error={err("email")} />
        </div>

        <ServicePicker
          name="service"
          legend={picker.selectService}
          labels={picker.services}
          listClassName="grid-cols-3 gap-5 lg:grid-cols-6"
          tileClassName="text-subtle"
        />

        <Field>
          <FieldLabel htmlFor="request-idea" className="sr-only">
            {t.idea}
          </FieldLabel>
          <Textarea id="request-idea" name="idea" placeholder={t.idea} />
        </Field>
        {failed && <FormFailure message={formText.status.failed} />}
      </FieldGroup>

      <FormFooter cancelLabel={t.cancel} submitLabel={pending ? t.sending : t.submit} pending={pending} />
    </form>
  );
}
