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

export interface SettingContentAttributes extends Optional<SettingContentInterface, 'id'> { }

export class SettingContent extends Model<SettingContentInterface, SettingContentAttributes> implements SettingContentInterface {
    public id!: number;
    public settingId!: number;
    public languageId!: number;
    public label!: string;
    public valueString!: string;
    public valueText!: Text;
    public valueBoolean!: boolean;
    public valueNumeric!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof SettingContent {
        SettingContent.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                settingId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-Setting-content", comment: "Setting identifier" },
                languageId: { type: DataTypes.BIGINT, allowNull: false, unique: "unique-Setting-content", comment: "Content language identifier" },
                label: { type: DataTypes.STRING, allowNull: false, comment: "Setting label" },
                valueString: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "Setting string value" },
                valueText: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "Setting text value" },
                valueBoolean: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: null, comment: "Setting boolean value" },
                valueNumeric: { type: DataTypes.DECIMAL(10,2), allowNull: true, defaultValue: null, comment: "Setting numeric value" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'setting_content',
                timestamps: true,
                indexes: [
                    { name: 'setting-index', fields: ['label', 'value_text','value_string'], type: 'FULLTEXT' }
                ]
            }
        );
        return SettingContent;
    }

    public static associate(models: any) {
        SettingContent.belongsTo(models.Setting, { foreignKey: 'settingId', as: 'setting' });
        SettingContent.belongsTo(models.Language, { foreignKey: 'languageId', as: 'language' });
    }
}
