"""
Network checks module - verifies static IP configuration
"""
from tests.utils.logger import Logger
from tests.utils.docker_utils import DockerUtils


class NetworkChecks:
    """Check network configuration and static IP assignment"""
    
    def __init__(self, logger: Logger, docker_utils: DockerUtils, version: str = "unknown"):
        self.logger = logger
        self.version = version
        self.docker_utils = docker_utils
    
    def run_all_checks(self) -> bool:
        """Run all network checks"""
        self.logger.subheader(f"Network Configuration Check (v{self.version})")
        
        all_ok = True
        
        # Check static IP assignment
        if not self._check_static_ips():
            all_ok = False
        
        if all_ok:
            self.logger.success("NETWORK", "All network checks passed")
        else:
            self.logger.error("NETWORK", "Some network checks failed")
        
        return all_ok
    
    def _check_static_ips(self) -> bool:
        """Check if containers are connected to the correct network"""
        self.logger.info("NETWORK", "Checking network connectivity...")
        
        # Expected containers
        expected_containers = {
            "streama2026-postgres": "postgres",
            "streama2026-redis": "redis",
            "streama2026-backend": "backend",
            "streama2026-daemon": "daemon",
            "streama2026-frontend": "frontend",
            "streama2026-caddy": "caddy",
            "streama2026-nginx": "nginx",
            "streama2026-backup": "backup"
        }
        
        all_ok = True
        for container_name, service_name in expected_containers.items():
            actual_ip = self.docker_utils.get_container_ip(container_name)
            
            if actual_ip:
                self.logger.success("NETWORK", f"{service_name}: connected to network ({actual_ip}) ✓")
            else:
                status = self.docker_utils.get_container_status(container_name)
                if status == "running":
                    self.logger.warning("NETWORK", f"{service_name}: running but not connected to network - attempting to reconnect")
                    # Try to reconnect to network
                    if self.docker_utils.connect_to_network(container_name, "streama-network"):
                        # Check again after reconnect
                        actual_ip = self.docker_utils.get_container_ip(container_name)
                        if actual_ip:
                            self.logger.success("NETWORK", f"{service_name}: reconnected to network ({actual_ip}) ✓")
                        else:
                            self.logger.error("NETWORK", f"{service_name}: failed to reconnect to network")
                            all_ok = False
                    else:
                        self.logger.error("NETWORK", f"{service_name}: running but not connected to network")
                        all_ok = False
                else:
                    self.logger.warning("NETWORK", f"{service_name}: not running (status: {status})")
        
        return all_ok
