// src/shared/contracts/events.ts
// Events Contract - centralized event names and payload types for backend and frontend

/**
 * Centralized event names for EventEmitter communication
 * Used by backend slices to emit events for cross-slice communication
 * Used by frontend to handle socket events
 */
export const EVENTS = {
  // Check-in events
  CHECKIN_CREATED: 'checkin:created',
  CHECKIN_UPDATED: 'checkin:updated',
  CHECKIN_DELETED: 'checkin:deleted',

  // Employee events
  EMPLOYEE_CREATED: 'employee:created',
  EMPLOYEE_UPDATED: 'employee:updated',
  EMPLOYEE_FIRED: 'employee:fired',
  EMPLOYEE_REHIRED: 'employee:rehired',
  EMPLOYEE_DELETED: 'employee:deleted',

  // Schedule events
  SHIFT_UPDATED: 'shift:updated',
  SHIFT_APPROVED: 'shift:approved',
  SHIFT_REJECTED: 'shift:rejected',
  SHIFT_CREATED: 'shift:created',
  SHIFT_DELETED: 'shift:deleted',

  // Inventory events
  INVENTORY_CREATED: 'inventory:created',
  INVENTORY_UPDATED: 'inventory:updated',
  INVENTORY_DELETED: 'inventory:deleted',

  // Note events
  NOTE_CREATED: 'note:created',
  NOTE_UPDATED: 'note:updated',
  NOTE_DELETED: 'note:deleted',
  NOTE_COMMENTED: 'note:commented',
  NOTE_PRIORITY_CHANGED: 'note:priority_changed',
  MENTION_NOTIFICATION: 'mention:notification',

  // Room events
  ROOM_CREATED: 'room:created',
  ROOM_UPDATED: 'room:updated',
  ROOM_DELETED: 'room:deleted',

  // Notification events
  NOTIFICATION_CREATED: 'notification:created',
  NOTIFICATION_READ: 'notification:read',
  NOTIFICATION_DELETED: 'notification:deleted',

  // Blacklist events
  BLACKLIST_CREATED: 'blacklist:created',
  BLACKLIST_UPDATED: 'blacklist:updated',
  BLACKLIST_DELETED: 'blacklist:deleted',

  // Contractor events
  CONTRACTOR_CREATED: 'contractor:created',
  CONTRACTOR_UPDATED: 'contractor:updated',
  CONTRACTOR_DELETED: 'contractor:deleted',

  // Access events
  ACCESS_CREATED: 'access:created',
  ACCESS_UPDATED: 'access:updated',
  ACCESS_DELETED: 'access:deleted',

  // Session events
  SESSION_KILL: 'session:kill',
  SESSION_POLICY_UPDATED: 'session:policy_updated',
} as const;

/**
 * Event payload types for type-safe event handling
 */
export type EventPayloads = {
  // Check-in events
  [EVENTS.CHECKIN_CREATED]: {
    checkinId: string;
    roomId: string;
    date: string;
  };
  [EVENTS.CHECKIN_UPDATED]: {
    checkinId: string;
    roomId: string;
    date: string;
  };
  [EVENTS.CHECKIN_DELETED]: {
    checkinId: string;
    roomId: string;
  };

  // Employee events
  [EVENTS.EMPLOYEE_CREATED]: {
    employeeId: string;
    fullName: string;
  };
  [EVENTS.EMPLOYEE_UPDATED]: {
    employeeId: string;
    fullName: string;
  };
  [EVENTS.EMPLOYEE_FIRED]: {
    employeeId: string;
    fullName: string;
    reason: string;
    archivedAt: string;
  };
  [EVENTS.EMPLOYEE_REHIRED]: {
    employeeId: string;
    fullName: string;
    reason: string;
  };
  [EVENTS.EMPLOYEE_DELETED]: {
    employeeId: string;
  };

  // Schedule events
  [EVENTS.SHIFT_UPDATED]: {
    shiftId: string;
    userId: string;
    date: string;
    roleAtShift: string;
    status: string;
  };
  [EVENTS.SHIFT_APPROVED]: {
    month: number;
    year: number;
  };
  [EVENTS.SHIFT_REJECTED]: {
    month: number;
    year: number;
  };
  [EVENTS.SHIFT_CREATED]: {
    shiftId: string;
    userId: string;
    date: string;
  };
  [EVENTS.SHIFT_DELETED]: {
    shiftId: string;
  };

  // Inventory events
  [EVENTS.INVENTORY_CREATED]: {
    inventoryId: string;
    name: string;
    category: string;
  };
  [EVENTS.INVENTORY_UPDATED]: {
    inventoryId: string;
    name: string;
    category: string;
  };
  [EVENTS.INVENTORY_DELETED]: {
    inventoryId: string;
  };

  // Note events
  [EVENTS.NOTE_CREATED]: {
    noteId: string;
    title: string;
    createdBy: string;
  };
  [EVENTS.NOTE_UPDATED]: {
    noteId: string;
    title: string;
  };
  [EVENTS.NOTE_DELETED]: {
    noteId: string;
  };
  [EVENTS.NOTE_COMMENTED]: {
    noteId: string;
    noteTitle: string;
    message: string;
    authorName: string;
    lastCommentAt: string;
    link: { path: string; query: { noteId: string } };
  };
  [EVENTS.NOTE_PRIORITY_CHANGED]: {
    noteId: string;
    noteTitle: string;
    message: string;
    authorName: string;
    oldPriority: string;
    newPriority: string;
    link: { path: string; query: { noteId: string } };
  };
  [EVENTS.MENTION_NOTIFICATION]: {
    noteId: string;
    mention: string;
    authorName: string;
    performerName: string;
    noteTitle: string;
    message: string;
    isPersonalMention: boolean;
    link: { path: string; query: { noteId: string } };
  };

  // Room events
  [EVENTS.ROOM_CREATED]: {
    roomId: string;
    number: string;
  };
  [EVENTS.ROOM_UPDATED]: {
    roomId: string;
    number: string;
  };
  [EVENTS.ROOM_DELETED]: {
    roomId: string;
  };

  // Notification events
  [EVENTS.NOTIFICATION_CREATED]: {
    notificationId: string;
    userId: string;
    type: string;
  };
  [EVENTS.NOTIFICATION_READ]: {
    notificationId: string;
  };
  [EVENTS.NOTIFICATION_DELETED]: {
    notificationId: string;
  };

  // Blacklist events
  [EVENTS.BLACKLIST_CREATED]: {
    entryId: string;
    name: string;
  };
  [EVENTS.BLACKLIST_UPDATED]: {
    entryId: string;
    name: string;
  };
  [EVENTS.BLACKLIST_DELETED]: {
    entryId: string;
  };

  // Contractor events
  [EVENTS.CONTRACTOR_CREATED]: {
    entryId: string;
    name: string;
  };
  [EVENTS.CONTRACTOR_UPDATED]: {
    entryId: string;
    name: string;
  };
  [EVENTS.CONTRACTOR_DELETED]: {
    entryId: string;
  };

  // Access events
  [EVENTS.ACCESS_CREATED]: {
    entryId: string;
    resourceName: string;
  };
  [EVENTS.ACCESS_UPDATED]: {
    entryId: string;
    resourceName: string;
  };
  [EVENTS.ACCESS_DELETED]: {
    entryId: string;
  };
};

/**
 * Type for event name
 */
export type EventName = keyof typeof EVENTS;

/**
 * Type for event handler callback
 */
export type EventHandler = (
  eventName: string,
  payload: any
) => void | Promise<void>;
