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

import { type User } from 'src/types/user';

import { type State } from './state';
import { type Task, type TaskEvent, type UserTask } from './task';

export enum TaskEventsViews {
  /** Parent request events (taskType = INCIDENT): TASK_STARTED, TASK_ASSIGNED, TASK_REASSIGNED, TASK_UPDATED (TASK_STATE_CHANGED) */
  INCIDENT_EVENTS = 'INCIDENT_EVENTS',
  /** Approval events: APPROVAL_GROUP_CREATED, APPROVAL_GROUP_APPROVED, APPROVAL_GROUP_REJECTED, APPROVAL_GROUP_CANCELLED, APPROVAL_TASK_APPROVED, APPROVAL_TASK_REJECTED (TASK_CLOSED_MANUALLY with taskType = APPROVAL) */
  APPROVAL_EVENTS = 'APPROVAL_EVENTS',
  /** Subtask events for agents/coordinators (taskType = SUBTASK): TASK_STARTED, TASK_ASSIGNED, TASK_REASSIGNED, TASK_UPDATED (TASK_STATE_CHANGED) */
  SUBTASK_DETAILED_EVENTS = 'SUBTASK_DETAILED_EVENTS',
  /** Subtask events for initiators (taskType = SUBTASK): TASK_STARTED, TASK_ASSIGNED, TASK_REASSIGNED, TASK_CLOSED_MANUALLY, TASK_CLOSED_AUTOMATICALLY */
  SUBTASK_SIMPLIFIED_EVENTS = 'SUBTASK_SIMPLIFIED_EVENTS',
  /** All events for agents/coordinators: INCIDENT_EVENTS + APPROVAL_EVENTS + SUBTASK_DETAILED_EVENTS */
  ALL_DETAILED_EVENTS = 'ALL_DETAILED_EVENTS',
  /** All events for initiators: INCIDENT_EVENTS + APPROVAL_EVENTS + SUBTASK_SIMPLIFIED_EVENTS */
  ALL_SIMPLIFIED_EVENTS = 'ALL_SIMPLIFIED_EVENTS',
}

export enum EventsType {
  COMMENT = 'COMMENT',
  TASK_STARTED = 'TASK_STARTED',
  TASK_UPDATED = 'TASK_UPDATED',
  TASK_ASSIGNED = 'TASK_ASSIGNED',
  TASK_REASSIGNED = 'TASK_REASSIGNED',
  // Approval group events
  APPROVAL_GROUP_APPROVED = 'APPROVAL_GROUP_APPROVED',
  APPROVAL_GROUP_CREATED = 'APPROVAL_GROUP_CREATED',
  APPROVAL_GROUP_REJECTED = 'APPROVAL_GROUP_REJECTED',
  APPROVAL_GROUP_CANCELLED = 'APPROVAL_GROUP_CANCELLED',
  // Approval task events
  APPROVAL_TASK_APPROVED = 'APPROVAL_TASK_APPROVED',
  APPROVAL_TASK_REJECTED = 'APPROVAL_TASK_REJECTED',
  APPROVAL_TASK_CANCELLED = 'APPROVAL_TASK_CANCELLED',
  // Subtask events
  TASK_CLOSED_MANUALLY = 'TASK_CLOSED_MANUALLY',
  TASK_CLOSED_AUTOMATICALLY = 'TASK_CLOSED_AUTOMATICALLY',
}

export type EventMetadata = {
  approvalName: string;
  approvalsNeeded?: number;
  taskCount?: number;
  message?: string;
};

export type Event = {
  id: string;
  user: User;
  assignee?: User;
  type: EventsType;
  message?: string;
  attachments: FileAssetType[];
  timestamp: string;
  task: TaskEvent;
  taskState: State;
  taskType: TaskEvent['type'];
  read?: boolean;
  metadata?: EventMetadata;
};

export type Events = {
  items: Event[];
  cursor?: string;
};

export type Activity = {
  id: string;
  task: Task;
  taskState: State;
  timestamp: string;
  type: EventsType;
  user: UserTask;
};
