// ─── Records ──────────────────────────────────────────────────────────────────

export interface FormRecord {
  id: string;
  code: string;
  displayName: string;
  description: string | null;
  isActive: boolean;
  currentVersionId: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface FormVersionRecord {
  id: string;
  formId: string;
  semver: string;
  schemaChecksum: string;
  surveyJson: Record<string, unknown>;
  questionIndex: Record<string, unknown>;
  isPublished: boolean;
  publishedAt: string | null;
  createdAt: string;
}

export interface FormWithVersions extends FormRecord {
  versions: FormVersionRecord[];
  liveVersion: FormVersionRecord | null;
}

export interface SubmissionRecord {
  id: string;
  submissionCode: string;
  formId: string;
  formVersionId: string;
  moduleCode: string;
  submittedBy: string | null;
  status: string;
  answers: Record<string, unknown>;
  submittedAt: string;
  createdAt: string;
}

// ─── Inputs ───────────────────────────────────────────────────────────────────

export interface CreateFormInput {
  code: string;
  displayName: string;
  description?: string;
  /** platform.module.code — resolved to module_id in the repository */
  moduleCode?: string;
  surveyJson: Record<string, unknown>;
  semver?: string;
  schemaChecksum: string;
}

export interface UpdateFormInput {
  displayName?: string;
  description?: string | null;
  isActive?: boolean;
  /** When provided, saves surveyJson as a draft version (creates or replaces existing draft) */
  surveyJson?: Record<string, unknown>;
  schemaChecksum?: string;
  /** Semver for the draft — required when surveyJson is provided and no draft exists */
  semver?: string;
}

export interface PublishVersionInput {
  semver: string;
  surveyJson: Record<string, unknown>;
  questionIndex: Record<string, unknown>;
  schemaChecksum: string;
  publishedBy?: string;
}

export interface SubmissionInput {
  formId: string;
  formVersionId: string;
  schemaChecksum: string;
  moduleCode: string;
  submittedBy?: string;
  answers: Record<string, unknown>;
}

export interface SubmissionFilter {
  page: number;
  limit: number;
  status?: string;
}

export interface SubmissionPage {
  items: SubmissionRecord[];
  total: number;
  page: number;
  limit: number;
}

// ─── Repository interface ─────────────────────────────────────────────────────

export interface FormRepository {
  listForms(): Promise<FormRecord[]>;
  getFormByCode(code: string): Promise<FormWithVersions | null>;
  getFormVersion(formId: string, semver: string): Promise<FormVersionRecord | null>;
  createForm(data: CreateFormInput): Promise<FormRecord>;
  updateForm(code: string, data: UpdateFormInput): Promise<FormWithVersions | null>;
  softDeleteForm(code: string): Promise<boolean>;
  publishVersion(formId: string, data: PublishVersionInput): Promise<FormVersionRecord>;
  saveSubmission(data: SubmissionInput): Promise<SubmissionRecord>;
  listSubmissions(formId: string, filter: SubmissionFilter): Promise<SubmissionPage>;
}

// ─── Legacy types (kept for backward compat with existing service/types) ───────

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, {
    label: string;
    type: string;
    group: string;
    optionSetRef: string | null;
    validationRef: string | null;
    required: boolean;
  }>;
  optionSets: Record<string, unknown>;
  validation: Record<string, { rule: Record<string, unknown>; message: string }>;
}
