module.exports = (sequelize, DataTypes) => {
  const Delivery = sequelize.define('Delivery', {
    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'
      }
    },
    delivery_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    delivery_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'
      }
    },
    so_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'SalesOrder',
        key: 'id'
      }
    },
    customer_po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CustomerPO',
        key: 'id'
      },
      comment: 'Reference to Customer PO if delivery is created from Customer PO'
    },
    storage_location: {
      type: DataTypes.STRING(10),
      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', 'POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    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: 'delivery',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'fiscal_year_id', 'delivery_number']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['so_id']
      },
      {
        fields: ['delivery_date']
      },
      {
        fields: ['status']
      }
    ]
  });

  Delivery.associate = function(models) {
    Delivery.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    Delivery.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    Delivery.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    Delivery.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    Delivery.belongsTo(models.BPAddress, { foreignKey: 'customer_address_id', as: 'customerAddress' });
    Delivery.belongsTo(models.SalesOrder, { foreignKey: 'so_id', as: 'salesOrder' });
    Delivery.belongsTo(models.CustomerPO, { foreignKey: 'customer_po_id', as: 'customerPO' });
    Delivery.hasMany(models.DeliveryItem, { foreignKey: 'delivery_id', as: 'items' });
    Delivery.hasMany(models.SalesInvoice, { foreignKey: 'delivery_id', as: 'invoices' });
    Delivery.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    Delivery.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    Delivery.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
  };

  return Delivery;
};

