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

export interface ContactUsAttributes extends Optional<ContactUsInterface, 'id'> { }

export class ContactUs extends Model<ContactUsInterface, ContactUsAttributes> implements ContactUsInterface {
    public id!: number;
    public accountId!: number | null;
    public userId!: number;
    public message!: string;
    public name!: string;
    public email!: string;
    public subject!: string;
    public lastUpdatedBy!: number | null;
    public status!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof ContactUs {
        ContactUs.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user Account identifier" },
                name: { type: DataTypes.STRING, allowNull: false, comment: "name of the user" },
                email: { type: DataTypes.STRING, allowNull: false, comment: "email of the user" },
                subject: { type: DataTypes.STRING, allowNull: false, comment: "subject of the contact request" },
                message: {type: DataTypes.TEXT, allowNull: false, defaultValue: null, comment: "message query by user"},
                lastUpdatedBy: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "user identifier" },
                status: { type: DataTypes.INTEGER, allowNull: false, defaultValue: Constants.PERMISSION.STATUS.ACTIVE, comment: "status of record" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'contactus',
                timestamps: true,
                indexes: [
                     { name: 'contact-search-index', fields: ['message'], type: 'FULLTEXT' }
                ]

            }
        );
        return ContactUs;
    }

    public static associate(models: any) {
        ContactUs.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
        ContactUs.belongsTo(models.User, { foreignKey: 'lastUpdatedBy', as: 'updatedBy' });
        ContactUs.belongsToMany(models.Attachment, { through: models.ContactUsAttachment, foreignKey: "contactusId", otherKey: "attachmentId", as: "attachments" });
    }
}
