import { DataTypes } from "sequelize";

export default (sequelize) => {
  const SystemProfit = sequelize.define("SystemProfit", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    merchantId: {
      type: DataTypes.UUID,
      allowNull: false,
    },
    customerId: {
      type: DataTypes.UUID,
      allowNull: true,
    },
    transactionId: {
      type: DataTypes.STRING,
      allowNull: false, // Internal reference or session ID
    },
    amount: {
      type: DataTypes.DECIMAL(20, 2),
      allowNull: false, // Total profit (feeProfit + marginProfit)
    },
    feeProfit: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
    },
    marginProfit: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
    },
    currency: {
      type: DataTypes.STRING(10),
      defaultValue: "NGN",
    },
    providerFee: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
    },
    customerFee: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
    },
    source: {
      type: DataTypes.STRING(50),
      allowNull: false, // E.g., 'deposit_fee', 'payout_fee' (legacy and new)
    },
    serviceCategory: {
      type: DataTypes.STRING(50),
      allowNull: true, // E.g., 'deposit', 'payout', 'swap', 'card_topup'
    },
    marginStatus: {
      type: DataTypes.STRING(20),
      defaultValue: "profit",
    },
    metadata: {
      type: DataTypes.JSON,
      allowNull: true,
    },
  }, {
    tableName: "SystemProfits",
    timestamps: true,
  });

  SystemProfit.associate = (models) => {
    SystemProfit.belongsTo(models.Merchant, { foreignKey: "merchantId" });
    SystemProfit.belongsTo(models.Customer, { foreignKey: "customerId" });
  };

  return SystemProfit;
};
