import { type Icon } from 'src/types/icons';

import { type ValueOf } from './journey';

export type Asset = {
  url: string;
  size: string;
  type: string;
  name: string;
};

type BaseTaskInfo<T extends JourneyTaskType> = {
  type: T;
};

export enum JourneyTaskType {
  CUSTOM = 'CUSTOM',
  FORM = 'FORM',
  DOCUMENT = 'DOCUMENT',
  SURVEY = 'SURVEY',
}

type BaseTask<T extends JourneyTaskType> = {
  type: T;
  description: string;
  coverImage: Asset;
  attachments: Asset[];
};

type CustomTaskDetail = JourneyTask & {
  taskInfo: BaseTaskInfo<JourneyTaskType.CUSTOM>;
  task: BaseTask<JourneyTaskType.CUSTOM>;
};

export type FormTaskDetail = JourneyTask & {
  taskInfo: BaseTaskInfo<JourneyTaskType.FORM> & {
    progressionId: string;
    answerId: string;
    token: string;
  };
  task: BaseTask<JourneyTaskType.FORM> & {
    formId: number;
    formTag: string;
  };
};

export type UploadedDocumentInfo = {
  documentId: number;
  documentName: string;
  size: string;
};

export type DocumentTaskDetail = JourneyTask & {
  taskInfo: BaseTaskInfo<JourneyTaskType.DOCUMENT> & {
    documents?: UploadedDocumentInfo[];
  };
  task: BaseTask<JourneyTaskType.DOCUMENT> & {
    documentNames: string[];
    targetFolderId: number;
  };
};

export type SurveyTaskDetail = JourneyTask & {
  // taskInfo.type comes as "CUSTOM" from the backend for SURVEY tasks.
  // Discrimination happens via task.type === "SURVEY".
  taskInfo: BaseTaskInfo<JourneyTaskType.CUSTOM> & {
    // Whether the user has started the survey.
    participantAdded: boolean;
    // Whether the user has completed the survey.
    completed: boolean;
  };
  task: BaseTask<JourneyTaskType.SURVEY> & {
    // The survey's unique identifier in PX.
    surveyId: number;
    // Indicates the survey is linked to an employee lifecycle journey.
    registered: boolean;
  };
};

export type JourneyTaskDetail =
  | FormTaskDetail
  | CustomTaskDetail
  | DocumentTaskDetail
  | SurveyTaskDetail;

export type JourneyTask = {
  id: number;
  userJourneyId: number;
  processTemplateId: number;
  stepId: number;
  actionId: number;
  actionTitle: string;
  actionType: JourneyTaskType;
  taskAssignee: number;
  isAvailable: boolean;
  status: JourneyTaskStatus;
  journeyAssignee: JourneyAssignee;
  expiresAfter?: string;
  icon?: Icon;
  dependsOn?: number;
};

export type JourneyAssignee = {
  id: number;
  fullName: string;
  avatar: {
    url: string;
    alt: string;
  };
};

export type JourneyTasksSections = {
  availableSteps: JourneyTask[];
  notAvailableSteps: JourneyTask[];
};

export type JourneyTasksParams = {
  journeyId?: number;
  stepId?: number;
  progressStatus?: string;
  search?: string;
  expirationStatus?: JourneyTaskExpirationStatusType | null;
  journeyAssignees?: string;
};

export const JourneyTaskStatus = {
  NOT_STARTED: 'NOT_STARTED',
  IN_PROGRESS: 'IN_PROGRESS',
  PAUSED: 'PAUSED',
  DONE: 'DONE',
} as const;

export const JourneyTaskExpirationStatus = {
  EXPIRED: 'EXPIRED',
  NOT_EXPIRED: 'NOT_EXPIRED',
  NO_EXPIRATION: 'NO_EXPIRATION',
} as const;

export const StepTasksTabs = {
  PENDING_TASKS: 'PENDING_TASKS',
  COMPLETED_TASKS: 'COMPLETED_TASKS',
} as const;

export type StepTasksTabsType = ValueOf<typeof StepTasksTabs>;
export type JourneyTaskExpirationStatusType = ValueOf<
  typeof JourneyTaskExpirationStatus
>;
export type JourneyTaskStatus = ValueOf<typeof JourneyTaskStatus>;

type CustomTaskData = {
  type: JourneyTaskType.CUSTOM;
};

type FormTaskData = {
  type: JourneyTaskType.FORM;
  progressionId: string;
  answerId: string;
};

type DocumentTaskData = {
  type: JourneyTaskType.DOCUMENT;
  documents: { fileAssetId: number }[];
};

type Data = FormTaskData | CustomTaskData | DocumentTaskData;

export type MarkJourneyTaskAsDoneParams = {
  taskId: number;
  data?: Data;
};
