import { toDesiredEmbeddedFields } from "@formbricks/types/embedded-data-mapping";
import type { TSurveyHiddenFields, TSurveyVariables } from "@formbricks/types/surveys/types";

/**
 * One survey's legacy declarations as they come off the database: raw JSON.
 *
 * Deliberately `unknown` rather than the parsed survey types. Every write path is zod-validated, but
 * this migration reads the columns directly, so the shapes are a claim about the data rather than
 * something the compiler knows — and a wrong claim here would throw inside the runner's single
 * transaction and roll back every survey migrated before it.
 */
export interface TLegacySurveyRow {
  id: string;
  workspaceId: string;
  variables: unknown;
  hiddenFields: unknown;
}

/**
 * An `EmbeddedData` row to insert. `key` and `locked` are left to the column defaults — a migrated
 * field is local (no library key) and unlocked (nothing could refuse an outside write before this).
 */
export interface TEmbeddedDataInsert {
  id: string;
  workspaceId: string;
  surveyId: string;
  name: string;
  source: "computed" | "ingested";
  dataType: "string" | "number" | "boolean" | "date";
  defaultValue: string | number | boolean | null;
}

export interface TSurveyEmbeddedDataInsert {
  id: string;
  workspaceId: string;
  surveyId: string;
  embeddedDataId: string;
  storageKey: string;
  /** Index within `toDesiredEmbeddedFields` — variables in declaration order, then hidden fields. */
  order: number;
}

export type TSurveyBackfillPlan =
  | { status: "ok"; fields: TEmbeddedDataInsert[]; links: TSurveyEmbeddedDataInsert[] }
  | { status: "skipped"; reason: "duplicate-address"; detail: string[] }
  | { status: "skipped"; reason: "malformed-declarations"; detail: string[] };

/** `variables` must be an array of objects each carrying a string `id` — that id is the storage key. */
const readVariables = (value: unknown): { variables: TSurveyVariables } | { problem: string } => {
  if (value === null || value === undefined) return { variables: [] };
  if (!Array.isArray(value)) return { problem: `variables is ${typeof value}, not an array` };

  for (const entry of value) {
    if (typeof entry !== "object" || entry === null) {
      return { problem: "variables contains a non-object element" };
    }
    if (typeof (entry as { id?: unknown }).id !== "string") {
      return { problem: "variables contains an element without a string id" };
    }
  }

  return { variables: value as TSurveyVariables };
};

/** `hiddenFields.fieldIds` must be an array of strings — each string is both the name and the key. */
const readHiddenFields = (value: unknown): { hiddenFields: TSurveyHiddenFields } | { problem: string } => {
  if (value === null || value === undefined) return { hiddenFields: { enabled: false } };
  if (typeof value !== "object" || Array.isArray(value)) {
    return { problem: `hiddenFields is ${Array.isArray(value) ? "an array" : typeof value}, not an object` };
  }

  const fieldIds = (value as { fieldIds?: unknown }).fieldIds;
  if (fieldIds === null || fieldIds === undefined) return { hiddenFields: { enabled: false } };
  if (!Array.isArray(fieldIds))
    return { problem: `hiddenFields.fieldIds is ${typeof fieldIds}, not an array` };
  if (fieldIds.some((fieldId) => typeof fieldId !== "string")) {
    return { problem: "hiddenFields.fieldIds contains a non-string entry" };
  }

  return { hiddenFields: { enabled: true, fieldIds: fieldIds as string[] } };
};

/**
 * Works out the rows and links one survey needs, or reports why it can't be migrated.
 *
 * The §8 mapping itself lives in `toDesiredEmbeddedFields`, shared with the ENG-1978 write bridge and
 * the ENG-1836 read seam — the two invariants that keep a migrated survey resolving (a variable's
 * `storageKey` is its original cuid, a hidden field's is its original name) are stated once, there.
 *
 * What this adds is what a bulk migration needs and a single survey save does not. The runner wraps
 * every data migration in one transaction, so anything that throws takes every already-migrated
 * survey down with it. Two shapes of bad survey are therefore reported and skipped rather than
 * attempted:
 *
 * - **Malformed declarations.** The columns are raw JSON here. `toDesiredEmbeddedFields` maps them
 *   with `?? []`, which catches null and undefined but not a wrong type — an object where an array
 *   belongs throws, and an element with no id yields a null `storageKey` on a NOT NULL column.
 * - **A duplicate address.** `@@unique([surveyId, storageKey])` would reject it. Reachable through
 *   the legacy API rather than the editor, which prevents duplicate names.
 *   `toDesiredEmbeddedFields` passes duplicates through instead of merging them precisely so this
 *   decision belongs to the caller.
 *
 * Ids are generated by the caller rather than left to the column default, so the links can be built
 * in the same pass and both tables written with a plain `createMany`.
 */
export const planSurveyBackfill = (survey: TLegacySurveyRow, newId: () => string): TSurveyBackfillPlan => {
  const variables = readVariables(survey.variables);
  if ("problem" in variables) {
    return { status: "skipped", reason: "malformed-declarations", detail: [variables.problem] };
  }
  const hiddenFields = readHiddenFields(survey.hiddenFields);
  if ("problem" in hiddenFields) {
    return { status: "skipped", reason: "malformed-declarations", detail: [hiddenFields.problem] };
  }

  const desired = toDesiredEmbeddedFields({ ...variables, ...hiddenFields });

  const seen = new Set<string>();
  const duplicates = new Set<string>();
  for (const field of desired) {
    if (seen.has(field.storageKey)) duplicates.add(field.storageKey);
    seen.add(field.storageKey);
  }
  if (duplicates.size > 0) {
    return { status: "skipped", reason: "duplicate-address", detail: [...duplicates] };
  }

  const fields: TEmbeddedDataInsert[] = [];
  const links: TSurveyEmbeddedDataInsert[] = [];

  for (const [order, desiredField] of desired.entries()) {
    if (desiredField.source === "reserved") {
      // Loud on purpose. Reserved fields are never stored as rows, so this means the shared mapping
      // changed under us — writing an `ingested` row instead would manufacture exactly the row the
      // schema forbids, and only a comment would have noticed.
      throw new Error(
        `toDesiredEmbeddedFields produced a reserved field (${desiredField.storageKey}); reserved fields are never stored as rows`
      );
    }

    const embeddedDataId = newId();
    fields.push({
      id: embeddedDataId,
      workspaceId: survey.workspaceId,
      surveyId: survey.id,
      name: desiredField.name,
      source: desiredField.source,
      dataType: desiredField.dataType,
      defaultValue: desiredField.defaultValue,
    });
    links.push({
      id: newId(),
      workspaceId: survey.workspaceId,
      surveyId: survey.id,
      embeddedDataId,
      storageKey: desiredField.storageKey,
      order,
    });
  }

  return { status: "ok", fields, links };
};
