export default (sequelize, DataTypes) => {
  const SubscriptionPlan = sequelize.define("SubscriptionPlan", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: "Plan name, e.g., Starter, Growth, Enterprise",
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: "Detailed plan description",
    },
    monthlyPrice: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      comment: "Base price for 1 month",
    },
    currency: {
      type: DataTypes.STRING(10),
      allowNull: false,
      defaultValue: "NGN",
      comment: "Currency code from Currencies table",
    },
    annualDiscountPercentage: {
      type: DataTypes.DECIMAL(5, 2),
      defaultValue: 0.00,
      comment: "Percentage discount applied if billed annually (e.g. 10.00 = 10%)",
    },
    servicePricingOverrides: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: "Array of service pricing configurations specific to this plan",
    },
    transactionLimit: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: "Max monthly transactions. NULL = unlimited.",
    },
    benefits: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: "Array of free-text benefit strings for display",
    },
    includedServices: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: "Array of SystemService codes this plan grants access to",
    },
    sortOrder: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: "Display order on the pricing page",
    },
    status: {
      type: DataTypes.ENUM("active", "inactive"),
      defaultValue: "active",
      allowNull: false,
    },
  }, {
    tableName: "SubscriptionPlans",
    timestamps: true,
  });

  SubscriptionPlan.associate = (models) => {
    SubscriptionPlan.hasMany(models.MerchantSubscription, {
      foreignKey: "planId",
      as: "subscriptions",
    });
  };

  return SubscriptionPlan;
};
