module.exports = (sequelize, DataTypes) => {
  const GoodsReceiptNote = sequelize.define('GoodsReceiptNote', {
    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'
      }
    },
    grn_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    grn_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseOrder',
        key: 'id'
      }
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    invoice_number: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    invoice_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    lr_number: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    lr_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    transporter_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    vehicle_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    storage_location_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'StorageLocation',
        key: 'id'
      },
      comment: 'Common storage location for all items (can be overridden per item)'
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_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: 'goods_receipt_note',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'grn_number']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        fields: ['po_id']
      },
      {
        fields: ['vendor_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['grn_date']
      }
    ]
  });

  GoodsReceiptNote.associate = function(models) {
    GoodsReceiptNote.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    GoodsReceiptNote.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    GoodsReceiptNote.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    GoodsReceiptNote.belongsTo(models.PurchaseOrder, { foreignKey: 'po_id', as: 'purchaseOrder' });
    GoodsReceiptNote.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    GoodsReceiptNote.belongsTo(models.StorageLocation, { foreignKey: 'storage_location_id', as: 'storageLocation' });
    GoodsReceiptNote.hasMany(models.GoodsReceiptNoteItem, { foreignKey: 'grn_id', as: 'items' });
    GoodsReceiptNote.hasMany(models.GateEntry, { foreignKey: 'grn_id', as: 'gateEntries' });
    GoodsReceiptNote.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    GoodsReceiptNote.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    GoodsReceiptNote.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return GoodsReceiptNote;
};

