/* eslint-disable @typescript-eslint/lines-between-class-members */
import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from 'sequelize';

import { createIdGenerator } from '../../libs/util';
import type { SettingType } from './types';

const nextId = createIdGenerator('set', 24);

// eslint-disable-next-line prettier/prettier
export class Setting extends Model<InferAttributes<Setting>, InferCreationAttributes<Setting>> {
  declare id: CreationOptional<string>;

  declare livemode: boolean;

  declare type: SettingType;
  declare mount_location: string;
  declare active: boolean;
  declare component_did?: string;

  declare settings: Record<string, any>;

  declare created_at: CreationOptional<Date>;
  declare updated_at: CreationOptional<Date>;

  public static readonly GENESIS_ATTRIBUTES = {
    id: {
      type: DataTypes.STRING(30),
      primaryKey: true,
      allowNull: false,
      defaultValue: nextId,
    },
    livemode: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
    },
    active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
    type: {
      type: DataTypes.STRING(64),
      allowNull: false,
    },
    mount_location: {
      type: DataTypes.STRING(255),
      allowNull: false,
    },
    component_did: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    description: {
      type: DataTypes.STRING(255),
      allowNull: false,
    },
    settings: {
      type: DataTypes.JSON,
      allowNull: true,
    },
    created_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      allowNull: false,
    },
    updated_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      allowNull: false,
    },
  };

  public static initialize(sequelize: any) {
    this.init(Setting.GENESIS_ATTRIBUTES, {
      sequelize,
      modelName: 'Setting',
      tableName: 'settings',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    });
  }
}

export type TSetting = InferAttributes<Setting>;
