/**
 * Rooms store actions using `createActionGroup`.
 */
import {
  createActionGroup,
  emptyProps,
  props
} from '@ngrx/store';
import {
  Room,
  RoomSettings,
  RoomPermissions,
  Channel
} from '../../shared-kernel';
import { type ServerInfo } from '../../domains/server-directory';

export const RoomsActions = createActionGroup({
  source: 'Rooms',
  events: {
    'Load Rooms': emptyProps(),
    'Load Rooms Success': props<{ rooms: Room[] }>(),
    'Load Rooms Failure': props<{ error: string }>(),

    'Search Servers': props<{ query: string }>(),
    'Search Servers Success': props<{ servers: ServerInfo[] }>(),
    'Search Servers Failure': props<{ error: string }>(),

    'Create Room': props<{
      name: string;
      description?: string;
      topic?: string;
      isPrivate?: boolean;
      password?: string;
      sourceId?: string;
      sourceUrl?: string;
    }>(),
    'Create Room Success': props<{ room: Room }>(),
    'Create Room Failure': props<{ error: string }>(),

    'Join Room': props<{
      roomId: string;
      password?: string;
      serverInfo?: Partial<ServerInfo> & { name: string; signalingUrl?: string };
    }>(),
    'Join Room Success': props<{ room: Room }>(),
    'Join Room Failure': props<{ error: string }>(),

    'Leave Room': emptyProps(),
    'Leave Room Success': emptyProps(),

    'View Server': props<{ room: Room; skipBanCheck?: boolean }>(),
    'View Server Success': props<{ room: Room }>(),

    'Delete Room': props<{ roomId: string }>(),
    'Delete Room Success': props<{ roomId: string }>(),

    'Forget Room': props<{ roomId: string; nextOwnerKey?: string }>(),
    'Forget Room Success': props<{ roomId: string }>(),

    'Reset Rooms State': emptyProps(),

    'Update Room Settings': props<{ roomId: string; settings: Partial<RoomSettings> }>(),
    'Update Room Settings Success': props<{ roomId: string; settings: RoomSettings }>(),
    'Update Room Settings Failure': props<{ error: string }>(),

    'Update Room Permissions': props<{ roomId: string; permissions: Partial<RoomPermissions> }>(),
    'Update Room Access Control': props<{
      roomId: string;
      changes: Pick<Partial<Room>, 'roles' | 'roleAssignments' | 'channelPermissions' | 'slowModeInterval'>;
    }>(),

    'Update Server Icon': props<{ roomId: string; icon: string }>(),
    'Update Server Icon Success': props<{ roomId: string; icon: string; iconUpdatedAt: number }>(),
    'Update Server Icon Failure': props<{ error: string }>(),
    'Receive Search Server Icon': props<{ roomId: string; icon: string; iconUpdatedAt: number }>(),

    'Set Current Room': props<{ room: Room }>(),
    'Clear Current Room': emptyProps(),

    'Update Room': props<{ roomId: string; changes: Partial<Room> }>(),
    'Receive Room Update': props<{ room: Partial<Room> }>(),

    'Select Channel': props<{ channelId: string }>(),
    'Add Channel': props<{ channel: Channel }>(),
    'Remove Channel': props<{ channelId: string }>(),
    'Rename Channel': props<{ channelId: string; name: string }>(),

    'Clear Search Results': emptyProps(),
    'Set Connecting': props<{ isConnecting: boolean }>(),
    'Set Signal Server Reconnecting': props<{ isReconnecting: boolean }>(),
    'Set Signal Server Compatibility Error': props<{ message: string | null }>()
  }
});
