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

export interface SeaQAAnswerAttributes extends Omit<SeaQAAnswerInterface, 'body'> {}
export interface SeaQAAnswerCreationAttributes extends Optional<SeaQAAnswerAttributes, 'id' | 'isFeatured' | 'requiresApproval' | 'likesCount' | 'bookmarksCount' | 'status' | 'featuredAt' | 'editedAt' | 'wordCount' | 'rejectedReason'> {}

export class SeaQAAnswer extends Model<SeaQAAnswerAttributes, SeaQAAnswerCreationAttributes> implements SeaQAAnswerAttributes {
    public id!: number;
    public questionId!: number;
    public userId!: number;
    public status!: 'draft' | 'pending_approval' | 'published' | 'rejected';
    public rejectedReason!: string | null;
    public isFeatured!: boolean;
    public featuredAt!: Date | null;
    public editedAt!: Date | null;
    public requiresApproval!: boolean;
    public wordCount!: number;
    public likesCount!: number;
    public bookmarksCount!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;
    public readonly deletedAt!: Date | null;

    static initModel(sequelize: Sequelize): typeof SeaQAAnswer {
        SeaQAAnswer.init({
            id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
            questionId: { type: DataTypes.BIGINT, allowNull: false, comment: "answer reference" },
            userId: { type: DataTypes.BIGINT, allowNull: false, comment: "author reference" },
            status: { type: DataTypes.STRING, allowNull: false, defaultValue: 'published', comment: "draft, pending_approval, published" },
            rejectedReason: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "reason for rejection" },
            isFeatured: { type: DataTypes.BOOLEAN, defaultValue: false, comment: "marked as featured by admin" },
            featuredAt: { type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: "featured timestamp" },
            editedAt: { type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: "last edit timestamp" },
            requiresApproval: { type: DataTypes.BOOLEAN, defaultValue: true, comment: "does it need approval after 48h edit?" },
            wordCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "cached word count" },
            likesCount: { type: DataTypes.INTEGER, defaultValue: 0, comment: "cached like count" },
            bookmarksCount: { type: DataTypes.INTEGER, defaultValue: 0, comment: "cached bookmark count" }
        }, {
            sequelize,
            tableName: 'sea_qa_answers',
            timestamps: true,
            paranoid: true,
            underscored: true,
            indexes: [
                { name: 'sea_qa_answer_question_index', fields: ['question_id'] },
                { name: 'sea_qa_answer_user_index', fields: ['user_id'] },
                { name: 'sea_qa_answer_status_index', fields: ['status'] }
            ]
        });

        return SeaQAAnswer;
    }

    public static associate(models: any) {
        SeaQAAnswer.belongsTo(models.SeaQAQuestion, { foreignKey: 'questionId', as: 'question' });
        SeaQAAnswer.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
        SeaQAAnswer.hasMany(models.SeaQAComment, { foreignKey: 'answerId', as: 'comments' });
        SeaQAAnswer.hasOne(models.SeaQAAnswerContent, { foreignKey: 'answerId', as: 'defaultContent' });
        SeaQAAnswer.hasOne(models.SeaQAAnswerContent, { foreignKey: 'answerId', as: 'content' });
        SeaQAAnswer.hasMany(models.SeaQAAnswerContent, { foreignKey: 'answerId', as: 'answerContents' });
    }
}
