import type { Badge } from './Badge'

export interface BaseMessage {
  username: string
  channel: string
  message: string
}

export type PRIVMSG_message = BaseMessage & {
  type: 'PRIVMSG'
  tags: {
    '@badge-info': string
    badges: Badge[]
    'client-nonce': string
    color: string
    'display-name': string
    emotes: string
    'first-msg': boolean
    flags: string
    id: string
    mod: boolean
    'returning-chatter': boolean
    'room-id': string
    subscriber: boolean
    'tmi-sent-ts': number
    turbo: boolean
    'user-id': string
    'user-type': string
  }
}

export type CLEARCHAT_message = BaseMessage & {
  type: 'CLEARCHAT'
  tags: {
    '@ban-duration': number
    'room-id': string
    'target-user-id': string
    'tmi-sent-ts': number
  }
}

export type USERNOTICE_message = BaseMessage & {
  type: 'USERNOTICE'
  tags: {
    '@badge-info': string
    badges: Badge[]
    color: string
    'display-name': string
    emotes: string
    flags: string
    id: string
    login: string
    mod: boolean
    'msg-id': string
    'msg-param-cumulative-months': number
    'msg-param-months': number
    'msg-param-multimonth-duration': number
    'msg-param-multimonth-tenure': number
    'msg-param-should-share-streak': number
    'msg-param-streak-months': number
    'msg-param-sub-plan': string
    'msg-param-sub-plan-name': string
    'msg-param-was-gifted': boolean
    'room-id': string
    'tmi-sent-ts': number
    'user-id': string
    vip: boolean
    'user-type': string
    subscriber: boolean
    'system-msg': string
  }
}

export interface RawMessage {
  username: string
  type: string
  channel: string
  message: string
  tags: RawTags
}

export type RawTags = Record<string, string>
export type Message = PRIVMSG_message | CLEARCHAT_message | USERNOTICE_message

/*
{"username":"tmi.twitch.tv ","type":"CLEARCHAT","channel":"kaicenat","message":"dahham22\r\n","tags":{"@ban-duration":"30","room-id":"641972806","target-user-id":"1074832162","tmi-sent-ts":"1757088444688"}}
*/

/*
{"username":"tmi.twitch.tv ","type":"USERNOTICE","channel":"kaicenat","message":"Yoooo\r\n","tags":{"@badge-info":"subscriber/6","badges":"subscriber/6,raging-wolf-helm/1","color":"#8A2BE2","display-name":"TTV_ShanBanMan","emotes":"","flags":"","id":"0d854206-63f9-4209-b6d0-fcbf4eed4a95","login":"ttv_shanbanman","mod":"0","msg-id":"resub","msg-param-cumulative-months":"6","msg-param-months":"0","msg-param-multimonth-duration":"1","msg-param-multimonth-tenure":"0","msg-param-should-share-streak":"1","msg-param-streak-months":"1","msg-param-sub-plan":"Prime","msg-param-sub-plan-name":"Channel\\sSubscription\\s(imkaicenat)","msg-param-was-gifted":"false","room-id":"641972806","subscriber":"1","system-msg":"TTV_ShanBanMan\\ssubscribed\\swith\\sPrime.\\sThey've\\ssubscribed\\sfor\\s6\\smonths,\\scurrently\\son\\sa\\s1\\smonth\\sstreak!","tmi-sent-ts":"1757088464348","user-id":"564841009","user-type":"","vip":"0"}}
*/

/*
  switch (message.type) {
    case 'PRIVMSG': {
      const data: ParsedMessage = {
        type: 'PRIVMSG',
        data: {
          message: message.message,
          username: message.username,
          color: message.tags.color,
          badges: getBadgesFromMessage(message.tags, allBadges)
        }
      }

      return data
    }
    case 'USERNOTICE': {
      const username = message.tags.login
      const resub = message.tags['msg-id'] === 'resub'
      const months = parseInt(message.tags['msg-param-cumulative-months'])
      return {
        type: 'USERNOTICE',
        data: { username, resub, months }
      }
    }
    case 'CLEARMSG': {
      return {
        type: 'CLEARMSG',
        data: {
          username: message.tags['@login']
        }
      }
    }
    case 'CLEARCHAT': {
      let duration = message.tags['@ban-duration']
      if (!message.tags['@ban-duration']) {
        duration = 0
      }

      return {
        type: 'CLEARCHAT',
        data: {
          username: message.message.replace(/(\r\n|\n|\r)/gm, ''),
          duration
        }
      }
    }
    case 'NOTICE': {
      return {
        type: 'NOTICE',
        data: {
          message: message.message
        }
      }
    }
    // Add more cases for other message types here
    default: {
      return {
        type: 'UNKNOWN',
        data: { message }
      }
    }
  }
}
  */
