import {filterParam, getAllEntries, getEntry} from '../get';
import {DBMigration} from '../migration';
import {setEntry} from '../set';

import {databaseConf, possibleDBKeys} from '../types';

enum keys {
  RoomId = 'a',
  syncId = 'b',
  initSyncId = 'c',
  unreadMessages = 'd',
  users = 'e',
  timestamp = 'f',
}

const name = 'chatRoomInfos';
const primaryKey: keyof typeof propsDefault = keys.RoomId;

const propsType: {[key in keyof typeof propsDefault]: string} = {
  [keys.RoomId]: 'string',
  [keys.syncId]: 'int',
  [keys.initSyncId]: 'int',
  [keys.unreadMessages]: 'int',
  [keys.users]: 'string[]',
  [keys.timestamp]: 'int',
};

const propsDefault = {
  [keys.RoomId]: '',
  [keys.syncId]: 0,
  [keys.initSyncId]: 0,
  [keys.unreadMessages]: 0,
  [keys.users]: ['none'],
  [keys.timestamp]: 0,
};

const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
  filePath: name,
  version: 1,
  keys,
  migration: () => {
    return DBMigration[name](thisSchema);
  },
  setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => {
    return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
      thisSchema,
      val,
      suffix,
    );
  },
  getEntry: (key: possibleDBKeys, suffix?: string) => {
    return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
      thisSchema,
      key,
      suffix,
    );
  },
  getAllEntries: (filter?: filterParam, suffix?: string) => {
    return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
      thisSchema,
      filter,
      suffix,
    );
  },
  defaultProps: propsDefault,
  details: {
    name,
    properties: propsType,
    primaryKey,
  },
};

export default thisSchema;
