export interface TemplateResource {
  id: string;
  name: {
    en: string;
    am: string;
  };
  description?: {
    en: string;
    am: string;
  };
  presigned?: string;
  fileInfo?: {
    fileName: string;
    fileSize?: number;
    uploadedAt?: string;
  };
  resourceTypeId?: string;
  createdAt?: string;
  updatedAt?: string;
  isCurrent?: boolean;
}

export interface TemplateResponse {
  count: number;
  items: TemplateResource[];
}

export interface ResourceTypeResponse {
  count: number;
  items: Array<{
    id: string;
    name: string | { en?: string; am?: string };
    code?: string;
    description?: string;
  }>;
}

export interface CreateTemplatePayload {
  name: {
    en: string;
    am: string;
  };
  description?: {
    en: string;
    am: string;
  };
  resourceTypeId: string;
  fileInfo?: {
    fileName: string;
    fileSize?: number;
  };
}

export interface HeaderFooterFileInfo {
  fileName: string;
  contentType: string;
  size: number;
  originalname: string;
}

export interface TemplateSampleRequest {
  data: TemplateSampleData;
  settings?: TemplateSampleRequestSetting[];
  resources?: TemplateSampleResource[];
}

export interface TemplateSampleResponse {
  pdf: string;
  contentType: string;
}

export interface TemplateSampleData {
  collaborators: Array<{ am: string; en: string }>;
  preferredLanguage: "am" | "en";
  referenceNumber?: string;
  groupedReceivers?: Array<{
    prefix?: string;
    suffix?: string;
    items: string[];
  }>;
  groupedCCReceivers?: Array<{
    prefix?: string;
    suffix?: string;
    items: string[];
  }>;
  groupedForYourReferenceReceivers?: Array<{
    prefix?: string;
    suffix?: string;
    items: string[];
  }>;
  receiverList?: TemplateReceiverGroup[];
  forYourReferenceList?: TemplateReceiverGroup[];
  content: {
    isWithDelegateSignature: boolean;
    delegatorName?: string;
    body: string;
    date: string;
    internalCC: string[];
    externalCC: string[];
    recordTags?: unknown[];
    ccList?: TemplateReceiverGroup[];
    prefixCC?: string;
    suffixCC?: string;
    from: string[];
    subject: string;
    sincerelyText: string;
    to: string[];
    prefix: string;
    suffix: string;
    forYourReferenceCC?: string[];
  };
  recordType: "external" | "internal";
  unitId?: string;
  header?: TemplateSampleFileInfo;
  footer?: TemplateSampleFileInfo;
  seal?: TemplateSampleFileInfo;
  stamps?: TemplateSampleFileInfo[];
}

export type TemplateReceiver = {
  type: "unit" | "non_smart";
  name: string;
  unitId?: string;
  [key: string]: unknown;
};

export interface TemplateReceiverGroup {
  prefix: string;
  suffix: string;
  receivers: TemplateReceiver[];
}

export interface TemplateSampleFileInfo {
  bucket: string;
  originalname: string;
  contentType: string;
  fileName?: string;
  size?: number;
  [key: string]: unknown;
}

export type TemplateSettingValueScope = "unit" | "global";

export interface TemplateSettingValueBulkItem {
  templateSettingId: string;
  resourceId?: string | null;
  value?: Record<string, unknown> | null;
  scope?: TemplateSettingValueScope;
  revertToDefault?: boolean;
}

export interface TemplateSampleRequestSetting {
  id?: string;
  code: string;
  value: Record<string, unknown>;
  resourceId?: string | null;
}

export interface TemplateSampleResource {
  resourceId: string;
  fileInfo?: TemplateSampleFileInfo;
  presigned?: string;
}

export interface TemplateSampleSetting {
  id: string;
  code: string;
  // CSS string, a CSS prop map, or a typed (non-CSS) object value such as
  // letter labels / page margins.
  value: Record<string, unknown> | string;
  name?: {
    am: string;
    en: string;
  };
  description?: {
    am: string;
    en: string;
  };
  templateSettingValues?: Array<{
    value: Record<string, string>;
    unitId?: string | null;
    resourceId?: string | null;
  }>;
  resourceId?: string | null;
  appliesTo?: { en: string; am: string };
  isUnitOverride?: boolean;
}

export interface TemplateSettingSectionGroup {
  code: string;
  rank?: string;
  name: { am: string; en: string };
  settings: TemplateSampleSetting[];
}

export interface TemplateSettingTypeGroup {
  type?: {
    id: string;
    code: string;
    name: { am: string; en: string };
    description?: { am: string; en: string } | null;
  };
  sections: TemplateSettingSectionGroup[];
  settings?: TemplateSampleSetting[];
}

export interface TemplateSettingsByUnitResponse {
  count: number;
  items: TemplateSampleSetting[];
  groups: TemplateSettingTypeGroup[];
}

export interface TemplateSettingValueHistoryEntry {
  id: string;
  createdAt?: string;
  updatedAt?: string;
  value?: Record<string, unknown>;
  resourceId?: string | null;
  unitId?: string | null;
  scope?: TemplateSettingValueScope;
  changedBy?: {
    name?: string;
    email?: string;
  };
  [key: string]: unknown;
}

export interface TemplateSettingValueHistoryResponse {
  count: number;
  items: TemplateSettingValueHistoryEntry[];
}
