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

export interface PermissionAttributes extends Optional<PermissionInterface, 'id'> { }

export class Permission extends Model<PermissionInterface, PermissionAttributes> implements PermissionInterface {
    public id!: number;
    public categoryId!: number;
    public userId!: number;
    public lastUpdatedBy!: number;
    public code!: string;
    public isRevision!: boolean;
    public revisionId!: number;
    public adminOnly!: boolean;
    public status!: number;
    public sortOrder!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

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

            }
        );
        return Permission;
    }

    public static associate(models: any) {
        Permission.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
        Permission.belongsTo(models.User, { foreignKey: 'lastUpdatedBy', as: 'updatedBy' });
        Permission.belongsTo(models.Category, { foreignKey: "categoryId", as: "permissionCategory" });
        Permission.hasOne(models.PermissionContent, { foreignKey: 'permissionId', as: 'defaultContent', onDelete: 'cascade', hooks: true });
        Permission.hasOne(models.PermissionContent, { foreignKey: 'permissionId', as: 'content', onDelete: 'cascade', hooks: true });
        Permission.hasOne(models.PermissionContent, { foreignKey: 'permissionId', as: 'permissionContent', onDelete: 'cascade', hooks: true });
        Permission.hasMany(models.PermissionContent, { foreignKey: 'permissionId', as: 'permissionContents', onDelete: 'cascade', hooks: true });
        Permission.belongsToMany(models.Role, { through: "role_permissions", foreignKey: "permissionId", otherKey: "roleId" });
    }
}
