module.exports = (sequelize, DataTypes) => {
  const SalesOrder = sequelize.define('SalesOrder', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    so_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    so_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    customer_address_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BPAddress',
        key: 'id'
      }
    },
    sq_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'SalesQuotation',
        key: 'id'
      }
    },
    customer_po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CustomerPO',
        key: 'id'
      },
      comment: 'Reference to Customer PO if created from Sales Order'
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    payment_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    delivery_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    delivery_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    sub_total: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    discount_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'RELEASED', 'REJECTED', 'PARTIALLY_DELIVERED', 'DELIVERED', 'CLOSED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    submitted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    submitted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'sales_order',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'fiscal_year_id', 'so_number']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['sq_id']
      },
      {
        fields: ['so_date']
      },
      {
        fields: ['status']
      }
    ]
  });

  SalesOrder.associate = function(models) {
    SalesOrder.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    SalesOrder.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    SalesOrder.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    SalesOrder.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    SalesOrder.belongsTo(models.BPAddress, { foreignKey: 'customer_address_id', as: 'customerAddress' });
    SalesOrder.belongsTo(models.SalesQuotation, { foreignKey: 'sq_id', as: 'salesQuotation' });
    SalesOrder.belongsTo(models.CustomerPO, { foreignKey: 'customer_po_id', as: 'customerPO' });
    SalesOrder.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    SalesOrder.hasMany(models.SalesOrderItem, { foreignKey: 'so_id', as: 'items' });
    SalesOrder.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    SalesOrder.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    SalesOrder.belongsTo(models.User, { foreignKey: 'submitted_by', as: 'submittedByUser' });
    SalesOrder.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
  };

  return SalesOrder;
};

