const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const Order = sequelize.define('Order', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  userId: { type: DataTypes.INTEGER, field: 'user_id' },
  orderNumber: { type: DataTypes.STRING(20), allowNull: false, unique: true, field: 'order_number' },
  status: {
    type: DataTypes.ENUM('pending', 'confirmed', 'processing', 'shipped', 'delivered', 'cancelled', 'refunded'),
    defaultValue: 'pending',
  },
  subtotal: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0 },
  taxAmount: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0, field: 'tax_amount' },
  shippingAmount: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0, field: 'shipping_amount' },
  discountAmount: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0, field: 'discount_amount' },
  total: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0 },
  paymentMethod: { type: DataTypes.STRING(50), field: 'payment_method' },
  paymentStatus: {
    type: DataTypes.ENUM('pending', 'paid', 'failed', 'refunded'),
    defaultValue: 'pending',
    field: 'payment_status',
  },
  razorpayOrderId: { type: DataTypes.STRING(100), field: 'razorpay_order_id' },
  razorpayPaymentId: { type: DataTypes.STRING(100), field: 'razorpay_payment_id' },
  couponId: { type: DataTypes.INTEGER, field: 'coupon_id' },
  shippingName: { type: DataTypes.STRING(100), field: 'shipping_name' },
  shippingPhone: { type: DataTypes.STRING(20), field: 'shipping_phone' },
  shippingAddress: { type: DataTypes.TEXT, field: 'shipping_address' },
  shippingCity: { type: DataTypes.STRING(100), field: 'shipping_city' },
  shippingState: { type: DataTypes.STRING(100), field: 'shipping_state' },
  shippingPincode: { type: DataTypes.STRING(10), field: 'shipping_pincode' },
  shippingCountry: { type: DataTypes.STRING(100), defaultValue: 'India', field: 'shipping_country' },
  billingName: { type: DataTypes.STRING(100), field: 'billing_name' },
  billingAddress: { type: DataTypes.TEXT, field: 'billing_address' },
  notes: { type: DataTypes.TEXT },
  adminNotes: { type: DataTypes.TEXT, field: 'admin_notes' },
  shippedAt: { type: DataTypes.DATE, field: 'shipped_at' },
  deliveredAt: { type: DataTypes.DATE, field: 'delivered_at' },
}, {
  tableName: 'orders',
  timestamps: true,
  underscored: true,
});

module.exports = Order;
