import { DataTypes, Model, Optional, Sequelize } from 'sequelize';
import * as Constants from "../config/constants"

export interface FaqAttributes extends Optional<FaqInterface, 'id'> { }

export class Faq extends Model<FaqInterface, FaqAttributes> implements FaqInterface {
    public id!: number;
    public accountId!: number | null;
    public categoryId!: number;
    public userId!: number;
    public lastUpdatedBy!: number;
    public code!: string;
    public isRevision!: boolean;
    public revisionId!: number;
    public status!: number;
    public sortOrder!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof Faq {
        Faq.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                categoryId: { type: DataTypes.BIGINT, allowNull: false, comment: "faq category" },
                code: { type: DataTypes.STRING, allowNull: false, unique: "unique-faq", comment: "faq-code" },
                userId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, unique: "unique-faq", defaultValue: null, comment: "user Account identifier" },
                lastUpdatedBy: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                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: "faq sort order" },
                status: { type: DataTypes.INTEGER, allowNull: false, defaultValue: Constants.PERMISSION.STATUS.ACTIVE, comment: "status of record" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'faqs',
                timestamps: true,
                indexes: [
                    { name: 'faq-revision-index', fields: ['is_revision'] },
                    { name: 'faq-index', fields: ['status'] },
                    { name: 'faq-code-index', fields: ['code'] }
                ]

            }
        );
        return Faq;
    }

    public static associate(models: any) {
        Faq.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
        Faq.belongsTo(models.User, { foreignKey: 'lastUpdatedBy', as: 'updatedBy' });
        Faq.belongsTo(models.Category, { foreignKey: "categoryId", as: "faqCategory" });
        Faq.hasOne(models.FaqContent, { foreignKey: 'faqId', as: 'defaultContent', onDelete: 'cascade', hooks: true });
        Faq.hasOne(models.FaqContent, { foreignKey: 'faqId', as: 'content', onDelete: 'cascade', hooks: true });
        Faq.hasOne(models.FaqContent, { foreignKey: 'faqId', as: 'faqContent', onDelete: 'cascade', hooks: true });
        Faq.hasMany(models.FaqContent, { foreignKey: 'faqId', as: 'faqContents', onDelete: 'cascade', hooks: true });
    }
}
