import { type AgentGroup } from './agent';
import { type ServiceItemTask, type ServiceItemTypes } from './serviceItem';
import { type State } from './state';
import { type FilterFormValues } from './view';

export enum TasksType {
  USER = 'USER',
  AGENT = 'AGENT',
}

export enum ServiceItemSla {
  RESPONSE = 'Tiempo de Atención',
  RESOLUTION = 'Tiempo de Resolución',
}

export type UserTask = {
  employeeInternalId: string;
  firstName: string;
  id: number;
  lastName: string;
  profilePicture?: string;
  email?: string;
};

export type SLA = {
  createdAt: string;
  deadline: string;
  definitionDescription: string;
  definitionName: string;
  elapsedMillis: number;
  expired: boolean;
  id: string;
  running: string;
  slaTotalMillis: number;
  updatedAt: string;
};

export type Task = {
  id: string;
  catalogItem: ServiceItemTask;
  name?: string;
  assignee?: UserTask;
  initiator?: UserTask;
  type: ServiceItemTypes;
  taskNumber: number;
  createdAt: string;
  updatedAt: string;
  formAnswerId?: number;
  formAnswerToken?: string;
  state: State;
  taskSlas: SLA[];
  unreadCommentsCount: number;
  bubbleCount: number;
  csatResponseId?: string;
  isCoordinator: boolean; // The current user is a coordinator of the agent group which the task belongs to
  isAssignableAgent: boolean; // The current user is a possible agent candidate for the task - based on the service item configuration
  parentTask?: Task;
  approvalGroupTask?: { message: string };
  canCommentTask?: boolean;
  description?: string;
};

export type HydratedTask = Task & {
  agentGroup: AgentGroup;
};

export type TaskEvent = {
  id: string;
  name?: string;
  type: ServiceItemTypes;
  state: State;
  taskNumber: number;
  assignee?: UserTask;
};

export type TicketListParams =
  | {
      q: string | undefined;
      page: number;
      limit: number;
      order: 'ASC' | 'DESC';
      orderBy: string;
      role: TasksType;
    }
  | FilterFormValues;

export type TaskChild = {
  initiator: UserTask;
  assignee: UserTask;
  assigning: boolean;
  createdAt: string;
  updatedAt: string;
  id: string;
  type: ServiceItemTypes;
  state: State;
  taskNumber: number;
  name: string;
  description: string;
  unreadCommentsCount?: number;
  bubbleCount?: number;
};

// Either acknowledge a specific comment as the last-read one, or mark all
// comments as viewed in bulk. The two variants are mutually exclusive.
export type VisitTaskBody =
  | { lastReadCommentId: string; commentsViewed?: never }
  | { commentsViewed: true; lastReadCommentId?: never };
