"""Test CT SoS field mapping — verify every field is mapped correctly."""

import pytest
from datetime import datetime
from integrations.ctsos import map_socrata_to_lead


class TestSocrataMapping:
    """Test field mapping from Socrata to lead schema."""
    
    def test_complete_field_mapping(self):
        """All required fields should be present in mapped lead."""
        synthetic_record = {
            "business_name": "TEST BUSINESS LLC",
            "business_type": "Domestic LLC",
            "citizenship": "Domestic",
            "naics_code": "541110",
            "date_registration": "2026-05-01T00:00:00.000",
            "agent_name": "John Agent",
            "agent_address": "123 Agent St",
            "billingcity": "Hartford",
            "billingstate": "CT",
            "billingpostalcode": "06101",
            "billingstreet": "123 Main St",
            "business_email_address": "test@example.com",
            "business_phone_number": "860-555-1234",
        }
        
        harvested_at = datetime(2026, 5, 20, 10, 0, 0)
        lead = map_socrata_to_lead(synthetic_record, harvested_at)
        
        # Check all required identity fields
        assert "name" in lead
        assert "entity_type" in lead
        assert "citizenship" in lead
        assert "naics" in lead
        assert "filing_date" in lead
        assert "agent_name" in lead
        assert "agent_address" in lead
        
        # Check location fields
        assert "city" in lead
        assert "state" in lead
        assert "zip" in lead
        assert "street_address" in lead
        
        # Check contact fields
        assert "email" in lead
        assert "phone" in lead
        
        # Check metadata fields
        assert "harvested_at" in lead
        assert "harvest_source" in lead
        assert lead["harvest_source"] == "ct_sos"
    
    def test_name_cleanup(self):
        """ALL CAPS names should be converted to proper case."""
        record = {
            "business_name": "JEFFREY SCHIETZELT PLLC",
            "date_registration": "2026-05-01",
        }
        lead = map_socrata_to_lead(record, datetime.utcnow())
        assert lead["name"] == "Jeffrey Schietzelt PLLC"
    
    def test_missing_optional_fields(self):
        """Missing optional fields should be null/empty, not omitted."""
        record = {
            "business_name": "Test LLC",
            "date_registration": "2026-05-01",
            # No email, phone, naics, etc.
        }
        lead = map_socrata_to_lead(record, datetime.utcnow())
        
        # Should have the fields, just empty
        assert "email" in lead
        assert "phone" in lead
        assert "naics" in lead
        assert lead["email"] == ""
        assert lead["phone"] == ""
        assert lead["naics"] == ""
    
    def test_zip_preserved_as_string(self):
        """ZIP codes should be strings (preserve leading zeros)."""
        record = {
            "business_name": "Test LLC",
            "billingpostalcode": "06101",
            "date_registration": "2026-05-01",
        }
        lead = map_socrata_to_lead(record, datetime.utcnow())
        assert lead["zip"] == "06101"
        assert isinstance(lead["zip"], str)
    
    def test_fallback_field_mapping(self):
        """Should use fallback field names when primary is missing."""
        record = {
            "name": "Fallback Business",  # Not "business_name"
            "entity_type": "Domestic LLC",  # Not "business_type"
            "city": "New Haven",  # Not "billingcity"
            "date_registration": "2026-05-01",
        }
        lead = map_socrata_to_lead(record, datetime.utcnow())
        assert lead["name"] == "Fallback Business"
        assert lead["entity_type"] == "Domestic LLC"
        assert lead["city"] == "New Haven"