import { z } from "zod";

// ─── Legacy interfaces (kept for backward compat) ─────────────────────────────

export interface FormSchemaQuestion {
  label: string;
  type: string;
  group: string;
  optionSetRef: string | null;
  validationRef: string | null;
  required: boolean;
}

export interface FormSchemaPayload {
  meta: {
    formCode: string;
    version: string;
    checksum: string;
    tenantId: string;
    generatedAt: string;
  };
  order: {
    tabs: string[];
    groups: Record<string, string[]>;
    questions: Record<string, string[]>;
  };
  tabs: Record<string, { label: string }>;
  groups: Record<string, { label: string }>;
  questions: Record<string, FormSchemaQuestion>;
  optionSets: Record<string, { sourceType: "static" | "masterData"; sourceRef: string }>;
  validation: Record<string, { rule: Record<string, unknown>; message: string }>;
}

export interface SubmissionRequest {
  formCode: string;
  formVersion: string;
  tenantId: string;
  submittedBy: string;
  answers: Record<string, unknown>;
}

// ─── New Zod schemas ──────────────────────────────────────────────────────────

/** Legacy schema endpoint query: formCode + version */
export const SchemaQuerySchema = z.object({
  formCode: z.string().min(1),
  version:  z.string().min(1),
});
export type SchemaQuery = z.infer<typeof SchemaQuerySchema>;

/** Submit answers to a form */
export const SubmissionBodySchema = z.object({
  formCode:    z.string().min(1),
  version:     z.string().min(1),
  moduleCode:  z.string().min(1),
  submittedBy: z.string().optional(),
  answers:     z.record(z.string(), z.unknown()),
});
export type SubmissionBody = z.infer<typeof SubmissionBodySchema>;

/** Create a new form with its initial version */
export const CreateFormBodySchema = z.object({
  code:            z.string().min(1),
  displayName:     z.string().min(1),
  description:     z.string().optional(),
  surveyJson:      z.record(z.string(), z.unknown()),
  schemaChecksum:  z.string().min(1),
  semver:          z.string().optional().default("1.0.0"),
  /** platform.module.code — e.g. "audit", "inspection", "checklist" */
  moduleCode:      z.string().optional().default("audit"),
});
export type CreateFormBody = z.infer<typeof CreateFormBodySchema>;

/** Update form metadata and/or save a schema draft */
export const UpdateFormBodySchema = z.object({
  displayName:    z.string().min(1).optional(),
  description:    z.string().nullable().optional(),
  isActive:       z.boolean().optional(),
  surveyJson:     z.record(z.string(), z.unknown()).optional(),
  schemaChecksum: z.string().optional(),
  semver:         z.string().optional(),
});
export type UpdateFormBody = z.infer<typeof UpdateFormBodySchema>;

/** Publish a new version to an existing form */
export const PublishVersionBodySchema = z.object({
  semver:          z.string().min(1),
  surveyJson:      z.record(z.string(), z.unknown()),
  questionIndex:   z.record(z.string(), z.unknown()).optional().default({}),
  schemaChecksum:  z.string().min(1),
  publishedBy:     z.string().uuid().optional(),
});
export type PublishVersionBody = z.infer<typeof PublishVersionBodySchema>;

/** List submissions query params */
export const ListSubmissionsQuerySchema = z.object({
  formId: z.string().uuid("formId must be a valid UUID"),
  page:   z.coerce.number().int().positive().default(1),
  limit:  z.coerce.number().int().min(1).max(200).default(50),
  status: z.string().optional(),
});
export type ListSubmissionsQuery = z.infer<typeof ListSubmissionsQuerySchema>;
