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

export interface UserProfileAttributes extends Optional<UserProfileInterface, 'id'> { }

export class UserProfile extends Model<UserProfileInterface, UserProfileAttributes> implements UserProfileInterface {
    public id!: number;
    public userId!: number;
    public referralCode!: string | null;
    public referralPoints!: number | null;
    public redeemedReferralPoints!: number | null;
    public promptionalPoints!: number | null;
    public redeemedPromptionalPoints!: number | null;
    public firstName!: string | null;
    public lastName!: string | null;
    public name!: string;
    public rankId!: number | null;
    public shipTypeId!: number | null;
    public birthYear!: number | null;
    public profileImageId!: number | null;
    public lastCommencingDate!: Date;
    public menstruationCycle!: number;
    public menstruationDuration!: number;
    public accuracy!: number;
    public obesity!: number;
    public diabetic!: number;
    public sessionCount!: number;
    public presentRankSince!: Date | null;
    public presentCompany!: string | null;
    public presentCompanySince!: Date | null;
    public aboutMe!: string | null;
    public areaOfExpertise!: any | null;
    public nationality!: string | null;
    public followersCount!: number;
    public followingCount!: number;
    public socialMediaLinks!: SocialMediaLinkInterface[] | null;
    public userType!: string | null;
    public questionCount!: number;
    public answerCount!: number;
    public answerCommentCount!: number;
    public postCommentCount!: number;
    public seaQALikesCount!: number;
    public seaQABookmarksCount!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof UserProfile {
        UserProfile.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: false, comment: "ref to User" },
                referralCode: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "user referral code" },
                referralPoints: { type: DataTypes.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: "earned referral points" },
                redeemedReferralPoints: { type: DataTypes.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: "redeemed referral points" },
                promptionalPoints: { type: DataTypes.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: "earned referral points" },
                redeemedPromptionalPoints: { type: DataTypes.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: "earned referral points" },
                firstName: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "User's first name" },
                lastName: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "User's last name" },
                name: { type: DataTypes.STRING, allowNull: false, comment: "display name" },
                rankId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "ref to Category (Rank)" },
                shipTypeId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "ref to Category (Ship Type)" },
                birthYear: { type: DataTypes.INTEGER, allowNull: true, defaultValue: null, comment: "birth year" },
                profileImageId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "ref to Attachment" },
                lastCommencingDate: { type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: "last commencing date" },
                menstruationCycle: { type: DataTypes.INTEGER, allowNull: true, defaultValue: null, comment: "menstruation cycle" },
                menstruationDuration: { type: DataTypes.INTEGER, allowNull: true, defaultValue: null, comment: "menstruation duration" },
                dob: {type: DataTypes.DATEONLY, allowNull: true, defaultValue: null, comment: "User's date of birth"},
                gender: {type: DataTypes.STRING,allowNull: true, defaultValue: null, comment: "User's gender"},
                fitnessLevel: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "User's fitness level" },
                nationality: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "User's nationality" },
                accuracy: { type: DataTypes.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: "accuracy levels" },
                obesity: { type: DataTypes.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: "obesity levels" },
                diabetic: { type: DataTypes.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: "diabetic levels" },
                sessionCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "diabetic levels" },
                presentRankSince: { type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: "Present rank since date" },
                presentCompany: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "Present company" },
                presentCompanySince: { type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: "Present company since date" },
                aboutMe: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "About me" },
                areaOfExpertise: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "Area of expertise" },
                followersCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "Followers count" },
                followingCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "Following count" },
                socialMediaLinks: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "Social media links" },
                userType: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "User type label" },
                questionCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "Approved SeaQA questions count" },
                answerCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "Published SeaQA answers count" },
                answerCommentCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "SeaQA answer comments count" },
                postCommentCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "Blog post comments count" },
                seaQALikesCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, field: 'sea_qa_likes_count', comment: "SeaQA entities liked by user count" },
                seaQABookmarksCount: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, field: 'sea_qa_bookmarks_count', comment: "SeaQA entities bookmarked by user count" },
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'user_profile',
                timestamps: true,
                indexes: [
                    { name: 'user-profile', fields: ['name'], type: 'FULLTEXT' }
                ]
            }
        );
        return UserProfile;
    }

    public static associate(models: any) {
        UserProfile.belongsTo(models.User, { foreignKey: 'userId' });
        UserProfile.belongsTo(models.Attachment, { foreignKey: 'profileImageId', as: 'profileImage' });
        UserProfile.belongsTo(models.Category, { foreignKey: 'rankId', as: 'rank' });
        UserProfile.belongsTo(models.Category, { foreignKey: 'shipTypeId', as: 'shipType' });
    }
}
