import { WS } from '@acrd/types';
import { Socket } from 'socket.io';
import { WebSocket } from '../websocket';
import { WSEvent } from './ws-event';

export default class implements WSEvent<'CHANNEL_CREATE'> {
  public on = 'CHANNEL_CREATE' as const;

  public async invoke(ws: WebSocket, client: Socket, params: WS.Params.ChannelCreate) {
    const { name, guildId, type, userIds } = params;
    
    if (type === 'DM') {
      if (!userIds) throw new TypeError('userIds required for DM creation');
      const channel = await deps.channels.createDM(userIds);
      return [{
        emit: this.on,
        to: userIds,
        send: {
          channel,
          creatorId: ws.sessions.get(client.id),
        },
      }];
    }

    if (!name || !guildId || !type)
      throw new TypeError('Not enough options were provided');

    await deps.wsGuard.validateCan(client, guildId, 'MANAGE_CHANNELS');
    const channel = await deps.channels.create({ name, guildId, type });

    return [{
      emit: this.on,
      to: [guildId],
      send: {
        channel,
        creatorId: ws.sessions.get(client.id),
        guildId,
      },
    }];
  }
}