/**
 * Root state definition and barrel exports for the NgRx store.
 *
 * Three feature slices:
 *   - **messages** - chat messages, reactions, sync state
 *   - **users**    - online users, bans, roles, voice state
 *   - **rooms**    - servers / rooms, channels, search results
 */
import { isDevMode } from '@angular/core';
import { ActionReducerMap, MetaReducer } from '@ngrx/store';
import { messagesReducer, MessagesState } from './messages/messages.reducer';
import { usersReducer, UsersState } from './users/users.reducer';
import { roomsReducer, RoomsState } from './rooms/rooms.reducer';

/** Combined root state of the application. */
export interface AppState {
  /** Chat messages feature slice. */
  messages: MessagesState;
  /** Users / presence feature slice. */
  users: UsersState;
  /** Rooms / servers feature slice. */
  rooms: RoomsState;
}

/** Top-level reducer map registered with `StoreModule.forRoot()`. */
export const reducers: ActionReducerMap<AppState> = {
  messages: messagesReducer,
  users: usersReducer,
  rooms: roomsReducer
};

/** Meta-reducers (e.g. logging) enabled only in development builds. */
export const metaReducers: MetaReducer<AppState>[] = isDevMode() ? [] : [];

// Re-export actions
export { MessagesActions } from './messages/messages.actions';
export { UsersActions } from './users/users.actions';
export { RoomsActions } from './rooms/rooms.actions';

// Re-export selectors explicitly to avoid conflicts
export {
  selectMessagesState,
  selectAllMessages,
  selectCurrentRoomMessages,
  selectMessageById,
  selectMessagesLoading,
  selectCurrentRoomId as selectMessagesCurrentRoomId
} from './messages/messages.selectors';

export {
  selectUsersState,
  selectAllUsers,
  selectCurrentUser,
  selectCurrentUserId,
  selectUserById,
  selectOnlineUsers,
  selectHostId,
  selectIsCurrentUserHost as selectIsCurrentUserHostFromUsers,
  selectBannedUsers
} from './users/users.selectors';

export {
  selectRoomsState,
  selectCurrentRoom,
  selectCurrentRoomId,
  selectRoomSettings,
  selectIsCurrentUserHost,
  selectSavedRooms,
  selectRoomsLoading
} from './rooms/rooms.selectors';

// Re-export effects
export { MessagesEffects } from './messages/messages.effects';
export { MessagesSyncEffects } from './messages/messages-sync.effects';
export { UserAvatarEffects } from './users/user-avatar.effects';
export { UsersEffects } from './users/users.effects';
export { RoomsEffects } from './rooms/rooms.effects';

// Re-export types
export type { MessagesState } from './messages/messages.reducer';
export type { UsersState } from './users/users.reducer';
export type { RoomsState } from './rooms/rooms.reducer';
