export type DocumentTemplateLifecycleStatus =
  | 'draft'
  | 'validated'
  | 'published'
  | 'active'
  | 'archived';

export type TemplateHealthStatus = 'pass' | 'warning' | 'fail';

export interface DocumentTemplateRecord {
  id: string;
  organizationId: string;
  documentType: string;
  productType: string;
  fileType: string;
  templateName: string;
  description: string | null;
  isDefault: boolean;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  createdBy: string;
  updatedBy: string;
}

export interface DocumentTemplateVersionValidationSummary {
  status: 'PASS' | 'WARNING' | 'FAIL';
  validatedAt: string | null;
  requiredFieldsMissing: string[];
  duplicateFields: string[];
  unmappedPlaceholders: string[];
  orphanMappings: string[];
  unsupportedSchemaTypes: string[];
  unknownPlugins: string[];
  sectionMarkerIssues: string[];
  suggestedNextStep?: string | null;
  issues: Array<{
    code: string;
    severity: 'warning' | 'error';
    location: string;
    message: string;
    suggestedFix: string | null;
  }>;
  healthStatus: TemplateHealthStatus;
  runtimeIssues: Array<{
    code: string;
    severity: 'warning' | 'error';
    message: string;
  }>;
}

export interface DocumentTemplateVersionAuditSummary {
  status: 'PASS' | 'WARNING' | 'FAIL';
  auditedAt: string | null;
  runtimeVersion: string | null;
  visualRegressionStatus: 'PASS' | 'WARNING' | 'FAIL' | 'NOT_RUN';
  visualRegressionAt: string | null;
}

export interface DocumentTemplateVersionVisualSummary {
  status: 'PASS' | 'WARNING' | 'FAIL' | 'NOT_RUN';
  generatedAt: string | null;
  changedPages: number;
  layoutDifferences: number;
}

export interface DocumentTemplateVersionRecord {
  id: string;
  organizationId: string;
  templateId: string;
  version: string;
  filePath: string | null;
  schemaJson: unknown;
  metadataJson?: unknown;
  previewImageUrl: string | null;
  isActive: boolean;
  lifecycleStatus?: DocumentTemplateLifecycleStatus;
  runtimeVersion?: string | null;
  templateVariant?: string | null;
  brand?: string | null;
  publishedAt?: string | null;
  publishedBy?: string | null;
  activatedAt?: string | null;
  activatedBy?: string | null;
  archivedAt?: string | null;
  archivedBy?: string | null;
  previousVersionId?: string | null;
  nextVersionId?: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  createdBy: string;
}

export interface DocumentTemplateMappingRecord {
  id: string;
  organizationId: string;
  templateVersionId: string;
  placeholderKey: string;
  sourcePath: string;
  dataType: 'scalar' | 'multiline' | 'table' | 'image';
  sheetName: string | null;
  defaultValue: string | null;
  formatMask: string | null;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface DocumentTemplateTableColumnRecord {
  id: string;
  organizationId: string;
  mappingId: string;
  columnName: string;
  sourceField: string;
  columnLetter: string | null;
  sortOrder: number;
  formatMask: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface DocumentTemplateMappingColumnMutationPayload {
  id?: string;
  columnName: string;
  sourceField: string;
  columnLetter?: string | null;
  sortOrder?: number;
  formatMask?: string | null;
}

export interface DocumentTemplateMappingMutationPayload {
  placeholderKey: string;
  sourcePath: string;
  dataType: DocumentTemplateMappingRecord['dataType'];
  sheetName?: string | null;
  defaultValue?: string | null;
  formatMask?: string | null;
  sortOrder?: number;
  columns?: DocumentTemplateMappingColumnMutationPayload[];
}

export interface DocumentTemplateMappingWithColumns
  extends DocumentTemplateMappingRecord {
  columns: DocumentTemplateTableColumnRecord[];
}

export interface DocumentTemplateVersionDetail
  extends DocumentTemplateVersionRecord {
  mappings: DocumentTemplateMappingWithColumns[];
  validationSummary?: DocumentTemplateVersionValidationSummary | null;
  auditSummary?: DocumentTemplateVersionAuditSummary | null;
  visualSummary?: DocumentTemplateVersionVisualSummary | null;
}

export interface DocumentTemplateDetail extends DocumentTemplateRecord {
  versions: DocumentTemplateVersionDetail[];
  fileType: string;
  mappingCount: number;
}

export interface DocumentTemplateListItem extends DocumentTemplateRecord {
  latestVersion: string | null;
  versionCount: number;
  mappingCount: number;
}

export interface DocumentTemplateFilters {
  documentType?: string;
  fileType?: string;
  isActive?: 'true' | 'false';
}

export interface DocumentTemplateMutationPayload {
  documentType: string;
  productType: string;
  fileType: string;
  templateName: string;
  description?: string | null;
  isDefault?: boolean;
  isActive?: boolean;
}

export interface DocumentTemplateVersionMutationPayload {
  version: string;
  filePath?: string | null;
  schemaJson: unknown;
  previewImageUrl?: string | null;
  isActive?: boolean;
  runtimeVersion?: string | null;
  templateVariant?: string | null;
  brand?: string | null;
  cloneFromVersionId?: string | null;
}

export interface ResolveTemplateParams {
  documentType: string;
  productType: string;
  fileType: string;
}

export interface ResolvedDocumentTemplate {
  template: DocumentTemplateRecord;
  version: DocumentTemplateVersionRecord;
}

export interface MutationSuccessResponse {
  success: boolean;
  message: string;
}

export interface DocumentTemplateListResponse extends MutationSuccessResponse {
  time: string;
  items: DocumentTemplateListItem[];
}

export interface DocumentTemplateDetailResponse extends MutationSuccessResponse {
  time: string;
  template: DocumentTemplateDetail;
}

export interface DocumentTemplateVersionsResponse extends MutationSuccessResponse {
  time: string;
  items: DocumentTemplateVersionDetail[];
}

export interface DocumentTemplateVersionActionResponse
  extends MutationSuccessResponse {
  version: DocumentTemplateVersionDetail;
}

export interface DocumentTemplateVersionValidateResponse
  extends MutationSuccessResponse {
  validation: DocumentTemplateVersionValidationSummary;
}

export interface DocumentTemplateVersionCompareResponse
  extends MutationSuccessResponse {
  comparison: {
    leftVersionId: string;
    rightVersionId: string;
    metadataChanges: Array<{
      field: string;
      left: string | number | boolean | null;
      right: string | number | boolean | null;
    }>;
    placeholderOnlyLeft: string[];
    placeholderOnlyRight: string[];
    mappingDifferences: Array<{
      placeholderKey: string;
      difference: string;
      leftValue?: string | null;
      rightValue?: string | null;
    }>;
    fieldSchemaChanges: {
      addedFields: string[];
      removedFields: string[];
      changedFields: Array<{
        field: string;
        difference: string;
      }>;
    };
    jsonDiff: Array<{
      path: string;
      left: string | number | boolean | null;
      right: string | number | boolean | null;
    }>;
    runtimeCompatibility: {
      leftStatus: 'PASS' | 'WARNING' | 'FAIL';
      rightStatus: 'PASS' | 'WARNING' | 'FAIL';
    };
    leftValidation: DocumentTemplateVersionValidationSummary;
    rightValidation: DocumentTemplateVersionValidationSummary;
  };
}

export interface DocumentTemplateVersionPreviewResponse
  extends MutationSuccessResponse {
  preview: {
    fileName: string;
    contentType: 'application/pdf';
    base64: string;
    pageCount: number;
    source: 'audit-fixture';
  };
}
