"""Unit tests for ServiceNow data models."""

import pytest

from beast_dream_snow_loader.models.servicenow import (
    ServiceNowEndpoint,
    ServiceNowGatewayCI,
    ServiceNowLocation,
    ServiceNowNetworkDeviceCI,
)


class TestServiceNowGatewayCI:
    """Test ServiceNowGatewayCI model validation and structure."""

    def test_gateway_ci_validates_with_minimal_data(self):
        """Test that ServiceNowGatewayCI validates with minimal required fields."""
        gateway_data = {
            "u_unifi_source_id": "test-gateway-id",
            "name": "Test Gateway",
            "ip_address": "192.168.1.1",
            "hostname": "gateway.example.com",
        }
        gateway = ServiceNowGatewayCI(**gateway_data)
        assert gateway.u_unifi_source_id == "test-gateway-id"
        assert (
            gateway.sys_id is None
        )  # sys_id is optional, auto-generated by ServiceNow
        assert gateway.name == "Test Gateway"
        assert gateway.ip_address == "192.168.1.1"
        assert gateway.hostname == "gateway.example.com"

    def test_gateway_ci_has_flat_schema(self):
        """Test that ServiceNowGatewayCI has flat schema (no nesting)."""
        gateway_data = {
            "u_unifi_source_id": "test-gateway-id",
            "name": "Test Gateway",
            "ip_address": "192.168.1.1",
            "hostname": "gateway.example.com",
            "firmware_version": "1.5.0",
            "hardware_id": "UDM-Pro",
            "mac_address": "00:11:22:33:44:55",
            "serial_number": "ABC123",
        }
        gateway = ServiceNowGatewayCI(**gateway_data)
        # All fields should be at top level, not nested
        assert gateway.firmware_version == "1.5.0"
        assert gateway.hardware_id == "UDM-Pro"
        assert gateway.mac_address == "00:11:22:33:44:55"

    def test_gateway_ci_handles_optional_fields(self):
        """Test that optional fields are handled gracefully."""
        gateway_data = {
            "u_unifi_source_id": "test-gateway-id",
            "name": "Test Gateway",
            "ip_address": "192.168.1.1",
            "hostname": "gateway.example.com",
        }
        gateway = ServiceNowGatewayCI(**gateway_data)
        assert gateway.firmware_version is None or isinstance(
            gateway.firmware_version, str
        )

    def test_gateway_ci_rejects_invalid_data(self):
        """Test that invalid data raises ValidationError."""
        from pydantic import ValidationError

        with pytest.raises(ValidationError):
            ServiceNowGatewayCI(
                sys_id="test-id",
                # Missing required fields
            )


class TestServiceNowLocation:
    """Test ServiceNowLocation model validation and structure."""

    def test_location_validates_with_minimal_data(self):
        """Test that ServiceNowLocation validates with minimal required fields."""
        location_data = {
            "u_unifi_source_id": "test-location-id",
            "name": "Test Location",
            "description": "Test location description",
            "timezone": "America/New_York",
        }
        location = ServiceNowLocation(**location_data)
        assert location.u_unifi_source_id == "test-location-id"
        assert (
            location.sys_id is None
        )  # sys_id is optional, auto-generated by ServiceNow
        assert location.name == "Test Location"
        assert location.description == "Test location description"
        assert location.timezone == "America/New_York"

    def test_location_has_flat_schema(self):
        """Test that ServiceNowLocation has flat schema (no nesting)."""
        location_data = {
            "u_unifi_source_id": "test-location-id",
            "name": "Test Location",
            "description": "Test location description",
            "timezone": "America/New_York",
            "host_id": "test-host-id",
        }
        location = ServiceNowLocation(**location_data)
        # All fields should be at top level
        assert location.host_id == "test-host-id"

    def test_location_rejects_invalid_data(self):
        """Test that invalid data raises ValidationError."""
        from pydantic import ValidationError

        with pytest.raises(ValidationError):
            ServiceNowLocation(
                sys_id="test-id",
                # Missing required fields
            )


class TestServiceNowNetworkDeviceCI:
    """Test ServiceNowNetworkDeviceCI model validation and structure."""

    def test_network_device_ci_validates_with_minimal_data(self):
        """Test that ServiceNowNetworkDeviceCI validates with minimal required fields."""
        device_data = {
            "u_unifi_source_id": "test-device-id",
            "name": "Test Device",
            "mac_address": "00:11:22:33:44:55",
        }
        device = ServiceNowNetworkDeviceCI(**device_data)
        assert device.u_unifi_source_id == "test-device-id"
        assert device.sys_id is None  # sys_id is optional, auto-generated by ServiceNow
        assert device.name == "Test Device"
        assert device.mac_address == "00:11:22:33:44:55"

    def test_network_device_ci_has_flat_schema(self):
        """Test that ServiceNowNetworkDeviceCI has flat schema (no nesting)."""
        device_data = {
            "u_unifi_source_id": "test-device-id",
            "name": "Test Device",
            "mac_address": "00:11:22:33:44:55",
            "serial_number": "ABC123",
            "model": "USW-Pro-48",
            "site_id": "test-site-id",
            "host_id": "test-host-id",
        }
        device = ServiceNowNetworkDeviceCI(**device_data)
        # All fields should be at top level
        assert device.serial_number == "ABC123"
        assert device.model == "USW-Pro-48"
        assert device.site_id == "test-site-id"

    def test_network_device_ci_rejects_invalid_data(self):
        """Test that invalid data raises ValidationError."""
        from pydantic import ValidationError

        with pytest.raises(ValidationError):
            ServiceNowNetworkDeviceCI(
                sys_id="test-id",
                # Missing required fields
            )


class TestServiceNowEndpoint:
    """Test ServiceNowEndpoint model validation and structure."""

    def test_endpoint_validates_with_minimal_data(self):
        """Test that ServiceNowEndpoint validates with minimal required fields."""
        endpoint_data = {
            "u_unifi_source_id": "test-endpoint-id",
            "hostname": "test-client",
            "ip_address": "192.168.1.100",
            "mac_address": "00:11:22:33:44:55",
        }
        endpoint = ServiceNowEndpoint(**endpoint_data)
        assert endpoint.u_unifi_source_id == "test-endpoint-id"
        assert (
            endpoint.sys_id is None
        )  # sys_id is optional, auto-generated by ServiceNow
        assert endpoint.hostname == "test-client"
        assert endpoint.ip_address == "192.168.1.100"
        assert endpoint.mac_address == "00:11:22:33:44:55"

    def test_endpoint_has_flat_schema(self):
        """Test that ServiceNowEndpoint has flat schema (no nesting)."""
        endpoint_data = {
            "u_unifi_source_id": "test-endpoint-id",
            "hostname": "test-client",
            "ip_address": "192.168.1.100",
            "mac_address": "00:11:22:33:44:55",
            "device_type": "computer",
            "site_id": "test-site-id",
            "device_id": "test-device-id",
        }
        endpoint = ServiceNowEndpoint(**endpoint_data)
        # All fields should be at top level
        assert endpoint.device_type == "computer"
        assert endpoint.site_id == "test-site-id"
        assert endpoint.device_id == "test-device-id"

    def test_endpoint_rejects_invalid_data(self):
        """Test that invalid data raises ValidationError."""
        from pydantic import ValidationError

        with pytest.raises(ValidationError):
            ServiceNowEndpoint(
                sys_id="test-id",
                # Missing required fields
            )
