import { DataTypes, Model, Optional, Sequelize } from 'sequelize';

export interface RoleContentAttributes extends Optional<RoleContentInterface, 'id'> { }

export class RoleContent extends Model<RoleContentInterface, RoleContentAttributes> implements RoleContentInterface {
    public id!: number;
    public roleId!: number;
    public languageId!: number;
    public name!: string;
    public description!: Text;
    public descriptionText!: Text;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof RoleContent {
        RoleContent.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                roleId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-role-content", comment: "role identifier" },
                languageId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-role-content", comment: "content language identifier" },
                name: { type: DataTypes.STRING, allowNull: false, comment: "role name"},
                description: { type: DataTypes.TEXT, allowNull: true,defaultValue:null, comment: "Permission description" },
                descriptionText: { type: DataTypes.TEXT, allowNull: true,defaultValue:null, comment: "Permission description" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'role_content',
                timestamps: true,
                indexes: [
                    { name: 'name', fields: ['name','description_text'], type: 'FULLTEXT' }
                ]
            }
        );
        return RoleContent;
    }

    public static associate(models: any) {
        RoleContent.belongsTo(models.Role, {foreignKey: 'roleId',as: 'role'});
        RoleContent.belongsTo(models.Language, {foreignKey: 'languageId', as: 'language'});
    }
}
