export default (sequelize, DataTypes) => {
  const SubscriptionInvoice = sequelize.define("SubscriptionInvoice", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    subscriptionId: {
      type: DataTypes.UUID,
      allowNull: false,
      references: { model: "MerchantSubscriptions", key: "id" },
    },
    merchantId: {
      type: DataTypes.UUID,
      allowNull: false,
      references: { model: "Merchants", key: "id" },
    },
    planId: {
      type: DataTypes.UUID,
      allowNull: false,
      references: { model: "SubscriptionPlans", key: "id" },
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    currency: {
      type: DataTypes.STRING(10),
      allowNull: false,
      defaultValue: "NGN",
    },
    paymentMethod: {
      type: DataTypes.ENUM("wallet", "external"),
      defaultValue: "wallet",
    },
    paymentStatus: {
      type: DataTypes.ENUM("paid", "failed", "refunded"),
      defaultValue: "paid",
    },
    billingPeriodStart: {
      type: DataTypes.DATEONLY,
      allowNull: false,
    },
    billingPeriodEnd: {
      type: DataTypes.DATEONLY,
      allowNull: false,
    },
    paidAt: {
      type: DataTypes.DATE,
      allowNull: true,
    },
  }, {
    tableName: "SubscriptionInvoices",
    timestamps: true,
  });

  SubscriptionInvoice.associate = (models) => {
    SubscriptionInvoice.belongsTo(models.MerchantSubscription, {
      foreignKey: "subscriptionId",
      as: "subscription",
    });
    SubscriptionInvoice.belongsTo(models.Merchant, {
      foreignKey: "merchantId",
      as: "merchant",
    });
    SubscriptionInvoice.belongsTo(models.SubscriptionPlan, {
      foreignKey: "planId",
      as: "plan",
    });
  };

  return SubscriptionInvoice;
};
