import { Chat, ChatListItem, ChatType } from 'src/types/chats';
import { Position } from 'src/types/position';
import { Pagination } from 'src/types/services';
import { User } from 'src/types/user';

export enum FormStatus {
  UNASSIGNED = 'UNASSIGNED',
  PENDING = 'PENDING',
  APPROVED = 'APPROVED',
  REJECTED = 'REJECTED',
  CANCELLED = 'CANCELLED',
}

export enum FormPossibleStatus {
  UNASSIGNED = 'UNASSIGNED',
  ASSIGNED = 'ASSIGNED',
  IN_PROGRESS = 'IN_PROGRESS',
  ON_HOLD = 'ON_HOLD',
  CANCELLED = 'CANCELLED',
  CLOSED = 'CLOSED',
}

export enum LastTransactionStatus {
  CREATED = 'CREATED',
  ASSIGNED = 'ASSIGNED',
  APPROVED = 'APPROVED',
  REJECTED = 'REJECTED',
  CANCELLED = 'CANCELLED',
}

export enum FormType {
  SURVEY = 'SURVEY',
  FORM = 'FORM',
  QUIZ = 'QUIZ',
}

export enum FormStepType {
  REGULAR = 'REGULAR',
  FILE = 'FILE',
}

export enum InputType {
  DROPDOWN = 'DROPDOWN',
  CHECKBOX = 'CHECKBOX',
  INFO = 'INFO',
  TEXT = 'TEXT',
  AUTOCOMPLETE = 'AUTOCOMPLETE',
  INTEGER = 'INTEGER',
  DECIMAL = 'DECIMAL',
  TIME = 'TIME',
  DATE = 'DATE',
  DATE_MMAAAA = 'DATE_MMAAAA',
  DATE_MMDDYYYY = 'DATE_MMDDYYYY',
  RATE = 'RATE',
  DIGITAL_SIGN = 'DIGITAL_SIGN',
  MEDIA = 'MEDIA',
  FILE = 'FILE',
  PHONE = 'PHONE',
  CHECKBOX_GRID = 'CHECKBOX_GRID',
  TEXT_AREA = 'TEXT_AREA',
  STAR_RATING = 'STAR_RATING',
  MULTIPLE_CHOICE = 'MULTIPLE_CHOICE',
  FLOAT = 'FLOAT',
}

export enum FormSkipTypes {
  END = 'END',
  STEP = 'STEP',
}

export type AssignBulkBody = {
  chatIds: number[];
  approvalWorkflows: {
    formId: number;
    stepId: number;
  }[];
};

export type ReviewBulkBody = {
  decision: FormStatus.APPROVED | FormStatus.REJECTED;
  message?: string;
  approvalWorkflows: {
    formId: number;
    stepId: number;
  }[];
};

export type FormListItem = {
  id: number;
  pictureUrl: string;
  title: string;
  canEditAnswer?: boolean;
  approvalStatus?: string;
  approvalStatusColor?: string;
  templateId: string;
  type: string;
  stepsType: string;
  needApproval: boolean;
  withInProgressStatus?: boolean;
  isAnonymous?: boolean;
};

export type FormAvailableItem = {
  id: number;
  url: string | null;
  pictureUrl: string;
  type: FormType;
  status: string;
  approvalStatus: string | null;
  title: string;
  createdAt: string;
  updatedAt: string;
  creator: User;
  numberOfAttemptsAllowed: number;
};

export const CLOSED = 'CLOSED';
export const PENDING = 'PENDING';

export const NOT_ASSIGNED_TO_APPROVAL_STEP = 'NOT_ASSIGNED_TO_APPROVAL_STEP';

export type LastTransaction = {
  id: number;
  formId: number;
  stepId: number;
  transactionType: LastTransactionStatus;
  message: string;
  authorId: number;
  timestamp: string;
};

export type StepType = {
  stepId: number;
  approvalStatus: FormStatus;
  responsibleId: number;
  lastTransaction: LastTransaction;
};

export type Option = Position & {
  id: number;
  description: string;
  questionId: number;
  correctAnswerId: number | null;
  skip: Skip;
};

export type Row = Position & {
  id: number;
  description: string;
  correctAnswers: string[];
};

export type CheckboxGridValue = Record<number, [string]>;

export type Answer = Position & {
  id: number;
  description: string;
  checked: boolean;
};

export type BoxProperty = {
  color: string;
  fontFamily: string;
  fontSize: number;
  fontStyle: string;
  fontWeight: string;
  inputLimit: number;
};

export type BoxData = {
  height: number;
  page: number;
  width: number;
  x: number;
  y: number;
};

export type Question = Position & {
  id: number;
  required: boolean;
  description: string;
  multipleAnswer: boolean;
  inputType: InputType;
  userField: string;
  options: Option[];
  boxData: BoxData;
  boxProperties: BoxProperty;
  answer: any[];
  points: number | null;
  correctAnswer: null;
  userAnswerIsCorrect: boolean | null;
  attachments: any[];
  rows: Row[] | null;
  properties?: {
    defaultCountryCode?: string;
  };
};

export type Step = Position & {
  id: number;
  title: string;
  description: string;
  source: string;
  sourceAnswered: string;
  sourceData: {
    height: number;
    width: number;
    totalPages: number;
  };
  questions: Question[];
  skip: Skip;
};

export type Skip = {
  skipType: string;
  destinationStepId: number;
};

export type StackStepFormsType = {
  stepIndex: number;
  skip: Skip;
  optionId: number;
  stepId: number;
};

export type FormAttributes = {
  id: number;
  url: string;
  pictureUrl: string;
  type: FormType;
  status: 'ENABLED';
  approvalStatus: null;
  title: string;
  createdAt: string;
  updatedAt: string;
  steps: Step[];
  questions: null;
  templateId: number;
  creator: null;
  creatorId: number;
  public: boolean;
  hasBeenEdited: boolean;
  numberOfAttemptsAllowed: number;
  needApproval: boolean;
  isAnonymous: boolean | null;
  responsible: null;
  autoMessage: string;
  totalScore: number;
  obtainedScore: number;
  notifyByEmail: boolean | null;
  hasBranching: boolean;
};

export enum DeadlineUnit {
  DAYS = 'DAYS',
  HOURS = 'HOURS',
}

// Response
export type FormItemResponse = {
  id: number;
  needApproval: boolean;
  formId: number;
  title: string;
  templateId: number;
  chatId: number;
  stepId: number | null;
  pictureUrl: string;
  type: string;
  approvalStatus: string | null;
  canEditAnswer?: boolean;
  unreadMessages: number;
  lastMessage: {
    type: string;
    text: string;
    userId: number;
    createdAt: string;
  };
  creator: User;
  lastFilledStep: {
    assignedDatetime: Date | null;
    assignedDatetimeLimit: Date | null;
    reviewDatetime: Date | null;
    reviewDatetimeLimit: Date | null;
  };
  timeToAssign: number | null;
  timeToAssignType: DeadlineUnit | null;
  timeToReview: number | null;
  timeToReviewType: DeadlineUnit | null;
  hasActiveRequests?: boolean;
  formChat?: ChatListItem;
  withInProgressStatus?: boolean;
  lastStepOfResponsible?: {
    approvalStatus: string;
  };
};

export type FormItemResponseCompleted = {
  id?: number;
  chatId: number;
  chat: Chat;
  chatType: ChatType;
  needApproval: boolean;
  type?: string;
  templateId: number;
  form: FormItemResponse;
  responsible: User;
  responsibleId: number;
  status: string;
};

export type FormStatsResponse = {
  unassignedForms: number;
  isResponsible: boolean;
  unreadFinished: number;
  unreadAssignedFormChatMessages: number;
};

export type FormPaginationResponse = Pagination<FormItemResponse[]>;

export type FormListResponse = FormItemResponse[];

export type FormPaginationAvailableListResponse = Pagination<
  FormAvailableItem[]
>;

export type FormChatsNotificationsResponse = {
  unreadUserFormChatNotifications: number;
  unreadAgentFormChatNotifications: number;
};

// Data
export type QuestionSubmitData = {
  answer?: unknown[];
  attachments?: any;
  id: number;
  inputType: any;
};

export type FormSubmitData = {
  questions: QuestionSubmitData[];
  fillingTime?: number;
};

// Socket
export type RemainingAttemptsSocket = {
  id: number;
  remainingAttempts: number;
};

export type UpdateFormChatAssigment = FormItemResponseCompleted;

export type PanalabTotalAmountState = {
  [key: string]: {
    amount: number;
    step: string;
  };
} | null;
