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

export interface SettingAttributes extends Optional<SettingInterface, 'id'> { }

export class Setting extends Model<SettingInterface, SettingAttributes> implements SettingInterface {
    public id!: number;
    public code!: string;
    public categoryId!: number;
    public isRevision!: boolean;
    public revisionId!: number;
    public userId!: number;
    public accountId!: number;
    public lastUpdatedBy!: number;
    public status!: number;
    public sortOrder?: number;
    public valueType!: string;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof Setting {
        Setting.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: true, comment: "User identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, unique: "unique-setting", comment: "User's identifier" },
                lastUpdatedBy: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "User identifier" },
                categoryId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "Category identifier" },
                code: { type: DataTypes.STRING, allowNull: true, defaultValue: null, unique: "unique-setting", comment: "setting code" },
                isRevision: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, comment: "Is Revision?" },
                revisionId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "Revision ref identifier" },
                sortOrder: { type: DataTypes.INTEGER, defaultValue: 0, comment: "setting sort order" },
                status: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "setting status" },
                valueType: { type: DataTypes.STRING, allowNull: false, defaultValue: 'string', comment: "setting value type" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'settings',
                timestamps: true
            }
        );
        return Setting;
    }

    public static associate(models: any) {
        Setting.belongsTo(models.Category, { foreignKey: 'categoryId', as: 'settingCategory', onDelete: 'cascade', hooks: true });
        Setting.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
        Setting.belongsTo(models.User, { foreignKey: 'lastUpdatedBy', as: 'updatedBy' });
        Setting.hasOne(models.SettingContent, { foreignKey: 'settingId', as: 'defaultContent', onDelete: 'cascade', hooks: true });
        Setting.hasOne(models.SettingContent, { foreignKey: 'settingId', as: 'content', onDelete: 'cascade', hooks: true });
        Setting.hasMany(models.SettingContent, { foreignKey: 'settingId', as: 'settingContents', onDelete: 'cascade', hooks: true });
    }
}
