import Dexie from 'dexie'
import { DisplayMessage } from '@/store/messagesStore'
import { iContact, iContact2, iUserinfo } from '@/service/UserService/types'

export interface iChatMessage extends DisplayMessage {
  masterId: string // 这条消息展示在谁的页面上
}

export interface iChatListCard {
  masterId: string
  targetId: string
  unreadMsgCount: number
  content: string
  datetime: number
  inChat: boolean // 会话状态，会话中？
  isRobootCard?: boolean
}

export interface iOutLet {
  id: number
  name: string
}


export class MyAppDatabase extends Dexie {
  chatMessage: Dexie.Table<iChatMessage, number>
  chatListCard: Dexie.Table<iChatListCard, number>
  contactPerson: Dexie.Table<iContact2, number>
  userInfo: Dexie.Table<iUserinfo, number>
  outlet: Dexie.Table<iOutLet, number>

  constructor() {
    super('MyAppDatabase')

    this.version(1.4).stores({
      chatMessage:
        '++id, content, from, uuid, state, uploadProgress, type, datetime, hideDatetime, logid, masterId, readed',
      chatListCard: '++id, masterId, targetId, unreadMsgCount, content, inChat, isRobootCard',
      contactPerson: '++id, addr, bank, phone, user_name, out_let_name',
      userInfo: '++id, created_at, phone, remark, user_name, uuid, addr',
      outlet: 'id, name',
    })

    this.chatMessage = this.table('chatMessage')
    this.chatListCard = this.table('chatListCard')
    this.contactPerson = this.table('contactPerson')
    this.userInfo = this.table('userInfo')
    this.outlet = this.table('outlet')
  }
}
