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

export interface TestimonialAttributes extends Optional<TestimonialInterface, 'id'> { }

export class Testimonial extends Model<TestimonialInterface, TestimonialAttributes> implements TestimonialInterface {
    public id!: number;
    public accountId!: number | null;
    public userId!: number | null;
    public lastUpdatedBy!: number | null;
    public name!: string;
    public designation!: string | null;
    public imageId!: number | null;
    public status!: number;
    public sortOrder!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof Testimonial {
        Testimonial.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                name: { type: DataTypes.STRING(255), allowNull: false, comment: "testimonial name" },
                designation: { type: DataTypes.STRING(255), allowNull: true, defaultValue: null, comment: "testimonial designation" },
                imageId: { type: DataTypes.BIGINT, allowNull: false, comment: "featured image" },
                userId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user Account identifier" },
                lastUpdatedBy: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                sortOrder: { type: DataTypes.INTEGER, defaultValue: 0, comment: "sort order" },
                status: { type: DataTypes.INTEGER, allowNull: false, defaultValue: Constants.PERMISSION.STATUS.ACTIVE, comment: "status of record" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'testimonials',
                timestamps: true,
                indexes: [
                    { name: 'testimonial-index', fields: ['status'] },
                ]
            }
        );
        return Testimonial;
    }

    public static associate(models: any) {
        Testimonial.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
        Testimonial.belongsTo(models.User, { foreignKey: 'lastUpdatedBy', as: 'updatedBy' });
        Testimonial.belongsTo(models.Attachment, { foreignKey: 'imageId', as: 'featuredImage' });
        Testimonial.hasOne(models.TestimonialContent, { foreignKey: 'testimonialId', as: 'defaultContent', onDelete: 'cascade', hooks: true });
        Testimonial.hasOne(models.TestimonialContent, { foreignKey: 'testimonialId', as: 'localizedContent', onDelete: 'cascade', hooks: true });
        Testimonial.hasMany(models.TestimonialContent, { foreignKey: 'testimonialId', as: 'testimonialContents', onDelete: 'cascade', hooks: true });
    }
}
