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

import { createIdGenerator } from '../../libs/util';

export const nextProductVendorId = createIdGenerator('pv', 24);

export class ProductVendor extends Model<InferAttributes<ProductVendor>, InferCreationAttributes<ProductVendor>> {
  declare id: CreationOptional<string>;
  declare vendor_key: string;
  declare vendor_type: string;
  declare name: string;
  declare description: string;

  declare app_url: string;
  declare app_pid?: string;
  declare app_logo?: string;
  declare vendor_did?: string;

  declare status: 'active' | 'inactive';
  declare metadata: Record<string, any>;
  declare extends: Record<string, any>;

  declare created_by: string;
  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: nextProductVendorId,
    },
    vendor_key: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
    },
    vendor_type: {
      type: DataTypes.STRING(50),
      allowNull: false,
      defaultValue: 'launcher',
    },
    name: {
      type: DataTypes.STRING(255),
      allowNull: false,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    app_url: {
      type: DataTypes.STRING(512),
      allowNull: false,
    },
    app_pid: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    app_logo: {
      type: DataTypes.STRING(512),
      allowNull: true,
    },
    vendor_did: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    status: {
      type: DataTypes.ENUM('active', 'inactive'),
      allowNull: false,
      defaultValue: 'active',
    },
    metadata: {
      type: DataTypes.JSON,
      allowNull: true,
      defaultValue: {},
    },
    extends: {
      type: DataTypes.JSON,
      allowNull: true,
      defaultValue: {},
    },
    created_by: {
      type: DataTypes.STRING(30),
      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(ProductVendor.GENESIS_ATTRIBUTES, {
      sequelize,
      modelName: 'ProductVendor',
      tableName: 'product_vendors',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    });
  }
}

export type TProductVendor = InferAttributes<ProductVendor>;
