import { type FileAsset as FileAssetType } from '@material-hu/types/attachments';

export enum InputType {
  DROPDOWN = 'DROPDOWN',
  CHECKBOX = 'CHECKBOX',
  INFO = 'INFO',
  TEXT = 'TEXT',
  AUTOCOMPLETE = 'AUTOCOMPLETE',
  INTEGER = 'INTEGER',
  TIME = 'TIME',
  DATE = 'DATE',
  RATE = 'RATE',
  PHONE = 'PHONE',
  STAR_RATING = 'STAR_RATING',
  MULTIPLE_CHOICE = 'MULTIPLE_CHOICE',
  FLOAT = 'FLOAT',
  SIGNATURE = 'SIGNATURE',
  FILE = 'FILE',
}

export enum SectionEvent {
  COMPLETE = 'COMPLETE',
  SAVE = 'SAVE',
  RELOAD = 'RELOAD',
  BACK = 'BACK',
}

export enum DynamicFormType {
  REGULAR = 'REGULAR',
  PDF = 'PDF',
}

export type InputValidations = {
  pattern?: string;
  maxItems?: number;
  minItems?: number;
  minimum?: number;
  maximum?: number;
  maxLength?: number;
  minLength?: number;
};

export type Form = {
  type: DynamicFormType;
  tag: string;
  published: boolean;
  title: string;
  description: string | null;
  sectionsCount: number;
  id: number;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  fileAsset?: FileAssetType;
};

export type FormAnswer = {
  id: number;
  formId: number;
  formTag: string;
  fillingTime: number;
  sectionsPath: number[];
  lastProgressionId: string;
  progressionIds: string[];
  variables: Record<string, any>;
  editable: boolean;
};

export type SectionAction = {
  event: string;
  nameId: string;
  nextSection: string;
  type: string;
  payload?: Record<string, unknown>;
};

export type SectionComponent = {
  nameId: string;
  answer: string | number | null;
  content: {
    title: string;
    description: string | null;
    required: boolean;
    choices?: string[];
  };
  attachments: FileAssetType[];
  type: InputType;
  section?: number;
  validations?: InputValidations;
  pdfLayout?: {
    allowWrap: boolean;
    height: number; // in relative units
    width: number; // in relative units
    x: number; // in relative units
    y: number; // in relative units
    page: number; // 1-based index
  };
  pdfStyles?: {
    color: string;
    fontFamily: string;
    fontSize: number;
    fontStyle: string;
    fontWeight: string;
  };
};

export type SectionContent = {
  data: Record<string, unknown>;
  title: string;
  nameId: string;
  description?: string;
  action: SectionAction;
  components: SectionComponent[];
};

export type Section = {
  content: SectionContent;
  createdAt: string;
  deletedAt: string | null;
  formId: number;
  formTag: string;
  id: number;
  nameId: string;
  position: number;
  updatedAt: string;
};

export type FormSection = {
  createdAt: string;
  deletedAt: string | null;
  currentSection: string;
  firstSection: string;
  finished: boolean;
  form: Form | null;
  formAnswer: FormAnswer | null;
  formAnswerId: number | null;
  formId: number | null;
  formTag: string;
  id: string;
  nextSection: Section;
  updatedAt: string;
  sections: Section[];
  pdfFileAsset: FileAssetType | null;
};

export type FormSectionBodyAnswer = {
  answer: string | number | undefined;
  componentNameId: string;
};

export type FormSectionBody = {
  answers: FormSectionBodyAnswer[];
  fillingTime: number;
  sectionEvent: SectionEvent;
  sectionNameId: string;
  fileAssetIds?: number[];
};

export type CompleteFormSection = {
  components: SectionComponent[];
  section: number;
  sectionName: string;
};

export type AutocompleteAnswer = {
  fieldName: string;
  fieldType: string;
  fieldValue: any;
};
