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

export interface PriceAttributes extends Optional<PriceInterface, 'id'> { }

export class Price extends Model<PriceInterface, PriceAttributes> implements PriceInterface {
    public id!: number;
    public planId!: number;
    public amount!: number;
    public currency!: string;
    public externalPriceId!: string;
    public duration!: number;
    public frequency!: 'days' | 'weeks' | 'months' | 'years';
    public autoRenew!: 0 | 1;
    public isDefault!: boolean;
    public status!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof Price {
        Price.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                planId: { type: DataTypes.BIGINT, allowNull: false, comment: "reference of plan id" },
                externalPriceId: { type: DataTypes.STRING, allowNull: true, comment: "reference of plan id at gateway" },
                amount: { type: DataTypes.DECIMAL(10,2), allowNull: false, comment: "total amount of the plan created on the gateway in cents" },
                currency: { type: DataTypes.STRING, allowNull: false, comment: "currency to be selected default is usd" },
                duration: { type: DataTypes.INTEGER, allowNull: false, comment: "duration of the pricing window" },
                frequency: { type: DataTypes.STRING, allowNull: false, comment: "days, weeks, months, years" },
                autoRenew: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true, comment: "auto renew toggle" },
                isDefault: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true, comment: ""},
                status: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 1, comment: "User's Account status" },
            },
            { 
                paranoid: true, 
                underscored: true, 
                sequelize, 
                tableName: 'prices', 
                timestamps: true,
                indexes: [
                    
                ]
            }
        );
        return Price;
    }

    public static associate(models: any) {
        Price.belongsTo(models.Plan, { foreignKey: 'planId', as: 'pricePlan' });
    }
}
