// Copyright 2025 Daytona Platforms Inc.
// SPDX-License-Identifier: AGPL-3.0

package netrules

// AssignNetworkRules assigns network rules to a container by inserting a rule in DOCKER-USER chain
func (manager *NetRulesManager) AssignNetworkRules(name string, sourceIp string) error {
	// Add prefix to chain name
	chainName := formatChainName(name)

	manager.mu.Lock()
	defer manager.mu.Unlock()

	// check does the chain exist
	exists, err := manager.ipt.ChainExists("filter", chainName)
	if err != nil {
		return err
	}

	// return if the chain does not exist
	if !exists {
		return nil
	}

	return manager.ipt.InsertUnique("filter", "DOCKER-USER", 1, "-j", chainName, "-s", sourceIp, "-p", "all")
}
