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

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

enum keys {
  syncID = "a",
  UserId = 'b',
  data = 'c',
  massageType = "d",
  created_at = "e",
  received_by_server = "f",
  received_by_target_clients = "g",
  reply = "h",
  reactions = "i",
  hasRead = "j",
}

const name = 'chat';
const primaryKey: keyof typeof propsDefault = keys.UserId;

const propsType: {[key in keyof typeof propsDefault]: string} = {
  
  [keys.syncID]: "int",
  [keys.UserId]: 'string',
  [keys.data]: 'data',
  [keys.massageType]: "int",
  [keys.created_at]: "int",
  [keys.received_by_server]: "int",
  [keys.received_by_target_clients]: "int",
  [keys.reply]: "int",
  [keys.reactions]: "i",
  [keys.hasRead]: "j",
};

const propsDefault = {
  [keys.syncID]: "a",
  [keys.UserId]: 'b',
  [keys.data]: 'c',
  [keys.massageType]: "d",
  [keys.created_at]: "e",
  [keys.received_by_server]: "f",
  [keys.received_by_target_clients]: "g",
  [keys.reply]: "h",
  [keys.reactions]: "i",
  [keys.hasRead]: "j",
};

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;
