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

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

const name = 'users';
const primaryKey: keyof typeof propsDefault = 'UserId';

const propsType: {[key in keyof typeof propsDefault]: string} = {
  UserId: 'string',
  AccountName: 'string',
  Username: 'string',
  Description: 'string',
  FollowersCount: 'int',
  FollowingCount: 'int',
  lastUpdateTimestamp: 'int',
  ProfilePicture: 'string', //URL
  ProfilePictureBinaryLQ: 'data',
  ProfilePictureBinaryHQ: 'data',

  XpLevel: 'int',
  XpPoints: 'int',
};

const propsDefault = {
  UserId: '',
  AccountName: '',
  Username: '',
  Description: '',
  FollowersCount: 0,
  FollowingCount: 0,
  lastUpdateTimestamp: 0,
  ProfilePicture: '', //URL
  ProfilePictureBinaryLQ: new ArrayBuffer(0),
  ProfilePictureBinaryHQ: new ArrayBuffer(0),
  XpLevel: 0,
  XpPoints: 0,
};

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

export default thisSchema;
