module.exports = (sequelize, DataTypes) => {
  const InventoryStockTransfer = sequelize.define('InventoryStockTransfer', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    from_company_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Company',
        key: 'id'
      },
      comment: 'Optional: source company for cross-company transfers'
    },
    to_company_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Company',
        key: 'id'
      },
      comment: 'Optional: destination company for cross-company transfers'
    },
    from_plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    to_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'
      }
    },
    transfer_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    transfer_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    movement_type: {
      type: DataTypes.STRING(10),
      allowNull: false,
      defaultValue: '301',
      comment: 'SAP Movement Type: 301=Transfer between Plants, 311=Transfer within Plant'
    },
    from_storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    to_storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    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: 'inventory_stock_transfer',
    indexes: [
      {
        fields: ['company_id', 'fiscal_year_id']
      },
      {
        fields: ['transfer_number']
      },
      {
        fields: ['transfer_date']
      },
      {
        fields: ['status']
      }
    ]
  });

  InventoryStockTransfer.associate = function(models) {
    InventoryStockTransfer.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    InventoryStockTransfer.belongsTo(models.Company, { foreignKey: 'from_company_id', as: 'fromCompany' });
    InventoryStockTransfer.belongsTo(models.Company, { foreignKey: 'to_company_id', as: 'toCompany' });
    InventoryStockTransfer.belongsTo(models.Plant, { foreignKey: 'from_plant_id', as: 'fromPlant' });
    InventoryStockTransfer.belongsTo(models.Plant, { foreignKey: 'to_plant_id', as: 'toPlant' });
    InventoryStockTransfer.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    InventoryStockTransfer.hasMany(models.InventoryStockTransferItem, { foreignKey: 'transfer_id', as: 'items' });
    InventoryStockTransfer.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    InventoryStockTransfer.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    InventoryStockTransfer.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return InventoryStockTransfer;
};

