"""
Tests for the domain module.
"""
import os
import tempfile
import unittest
import json
from datetime import datetime

from ..shared.domain import Domain, DomainRegistry

class TestDomain(unittest.TestCase):
    """Test suite for the domain module."""
    
    def test_domain_creation(self):
        """Test domain creation."""
        domain = Domain('test', 'example', 'com', '192.168.1.1', 8080)
        
        self.assertEqual(domain.prefix, 'test')
        self.assertEqual(domain.main, 'example')
        self.assertEqual(domain.suffix, 'com')
        self.assertEqual(domain.ip, '192.168.1.1')
        self.assertEqual(domain.port, 8080)
        self.assertEqual(domain.full_name, 'test.example.com.domain')
        self.assertEqual(domain.address, '192.168.1.1:8080')
    
    def test_domain_from_full_name(self):
        """Test creating a domain from a full name."""
        domain = Domain.from_full_name('test.example.com.domain', '192.168.1.1', 8080)
        
        self.assertEqual(domain.prefix, 'test')
        self.assertEqual(domain.main, 'example')
        self.assertEqual(domain.suffix, 'com')
        self.assertEqual(domain.ip, '192.168.1.1')
        self.assertEqual(domain.port, 8080)
        self.assertEqual(domain.full_name, 'test.example.com.domain')
    
    def test_domain_to_dict(self):
        """Test converting a domain to a dictionary."""
        domain = Domain('test', 'example', 'com', '192.168.1.1', 8080)
        domain.title = "Test Domain"
        domain.favicon = "test_favicon"
        domain.banner = "test_banner"
        domain.file_count = 10
        
        domain_dict = domain.to_dict()
        
        self.assertEqual(domain_dict['prefix'], 'test')
        self.assertEqual(domain_dict['main'], 'example')
        self.assertEqual(domain_dict['suffix'], 'com')
        self.assertEqual(domain_dict['full_name'], 'test.example.com.domain')
        self.assertEqual(domain_dict['ip'], '192.168.1.1')
        self.assertEqual(domain_dict['port'], 8080)
        self.assertEqual(domain_dict['address'], '192.168.1.1:8080')
        self.assertEqual(domain_dict['title'], 'Test Domain')
        self.assertEqual(domain_dict['favicon'], 'test_favicon')
        self.assertEqual(domain_dict['banner'], 'test_banner')
        self.assertEqual(domain_dict['file_count'], 10)
    
    def test_domain_from_dict(self):
        """Test creating a domain from a dictionary."""
        domain_dict = {
            'prefix': 'test',
            'main': 'example',
            'suffix': 'com',
            'ip': '192.168.1.1',
            'port': 8080,
            'title': 'Test Domain',
            'favicon': 'test_favicon',
            'banner': 'test_banner',
            'file_count': 10
        }
        
        domain = Domain.from_dict(domain_dict)
        
        self.assertEqual(domain.prefix, 'test')
        self.assertEqual(domain.main, 'example')
        self.assertEqual(domain.suffix, 'com')
        self.assertEqual(domain.full_name, 'test.example.com.domain')
        self.assertEqual(domain.ip, '192.168.1.1')
        self.assertEqual(domain.port, 8080)
        self.assertEqual(domain.title, 'Test Domain')
        self.assertEqual(domain.favicon, 'test_favicon')
        self.assertEqual(domain.banner, 'test_banner')
        self.assertEqual(domain.file_count, 10)


class TestDomainRegistry(unittest.TestCase):
    """Test suite for the domain registry."""
    
    def setUp(self):
        """Set up test environment."""
        self.temp_dir = tempfile.TemporaryDirectory()
        self.registry_file = os.path.join(self.temp_dir.name, 'registry.json')
        self.registry = DomainRegistry(self.registry_file)
    
    def tearDown(self):
        """Clean up test environment."""
        self.temp_dir.cleanup()
    
    def test_registry_add_remove(self):
        """Test adding and removing domains from the registry."""
        # Create domain
        domain = Domain('test', 'example', 'com', '192.168.1.1', 8080)
        
        # Add to registry
        self.registry.register(domain)
        
        # Check that domain was added
        self.assertIn(domain.full_name, self.registry.domains)
        
        # Check that file was created
        self.assertTrue(os.path.exists(self.registry_file))
        
        # Remove from registry
        self.registry.unregister(domain.full_name)
        
        # Check that domain was removed
        self.assertNotIn(domain.full_name, self.registry.domains)
    
    def test_registry_persistence(self):
        """Test registry persistence."""
        # Create domain
        domain = Domain('test', 'example', 'com', '192.168.1.1', 8080)
        
        # Add to registry
        self.registry.register(domain)
        
        # Create new registry instance with same file
        new_registry = DomainRegistry(self.registry_file)
        
        # Check that domain was loaded
        self.assertIn(domain.full_name, new_registry.domains)
        
        # Check that domain properties match
        loaded_domain = new_registry.domains[domain.full_name]
        self.assertEqual(loaded_domain.prefix, domain.prefix)
        self.assertEqual(loaded_domain.main, domain.main)
        self.assertEqual(loaded_domain.suffix, domain.suffix)
        self.assertEqual(loaded_domain.ip, domain.ip)
        self.assertEqual(loaded_domain.port, domain.port)
    
    def test_registry_update(self):
        """Test updating domain in registry."""
        # Create domain
        domain = Domain('test', 'example', 'com', '192.168.1.1', 8080)
        
        # Add to registry
        self.registry.register(domain)
        
        # Update domain
        self.registry.update_domain(domain.full_name, ip='192.168.1.2', port=8081, title='Updated Domain')
        
        # Check that domain was updated
        updated_domain = self.registry.domains[domain.full_name]
        self.assertEqual(updated_domain.ip, '192.168.1.2')
        self.assertEqual(updated_domain.port, 8081)
        self.assertEqual(updated_domain.title, 'Updated Domain')