"use server";

import { submitFormInquiry, type SubmitFormInquiryFields, type SubmitFormInquiryResult } from "@roottale/cms-client/server";

import { siteFormConnection, type SiteFormKey } from "../site-inquiry-forms";

export type SiteFormAttempt = Omit<SubmitFormInquiryFields, "siteId" | "placement">;

// formKey와 answers는 신뢰하지 않는다. 사이트/폼 ID는 서버 설정에서 고르고,
// API가 key의 tenant/site, 발행 버전, 동의와 모든 답변을 다시 검증한다.
export async function submitSiteForm(
  formKey: SiteFormKey,
  attempt: SiteFormAttempt,
): Promise<SubmitFormInquiryResult> {
  if (formKey !== "quote" && formKey !== "service") throw new Error("Unknown inquiry form");
  const connection = siteFormConnection(formKey);
  return submitFormInquiry({
    ...connection,
    fields: {
      ...attempt,
      siteId: connection.siteId,
      placement: `contact/${formKey}`,
    },
  });
}
