'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Order extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      // Order → OrderItems
  Order.hasMany(models.OrderItem, {
    foreignKey: 'orderId',
    as: 'items',
    onDelete: 'CASCADE'
  });

  Order.belongsTo(models.User, {
  foreignKey: 'userId',
  as: 'user'
});

  // Order → OrderStatuses
  Order.hasMany(models.OrderStatus, {
    foreignKey: 'orderId',
    as: 'statuses',
    onDelete: 'CASCADE'
  });
    }
  }
 Order.init({
  userId: DataTypes.INTEGER,
  orderNumber: DataTypes.STRING,
  status: {
    type: DataTypes.ENUM(
      'pending',
      'confirmed',
      'processing',
      'shipped',
      'delivered',
      'cancelled',
      'refunded',
      'failed'
    ),
    allowNull: false,
    defaultValue: 'pending'
  },
  subtotal: DataTypes.DECIMAL,
  tax: DataTypes.DECIMAL,
  shippingCost: DataTypes.DECIMAL,
  discount: DataTypes.DECIMAL,
  total: DataTypes.DECIMAL,
  paymentMethod: DataTypes.STRING,
  paymentStatus: {
    type: DataTypes.ENUM(
      'pending',
      'authorized',
      'paid',
      'failed',
      'refunded',
      'partially_refunded',
      'cancelled'
    ),
    allowNull: false,
    defaultValue: 'pending'
  },
  shippingAddressId: DataTypes.INTEGER,
  billingAddressId: DataTypes.INTEGER
}, {
  sequelize,
  modelName: 'Order',
});
  return Order;
};