"""Test scoring capability."""

import pytest
from capabilities.scoring import score_lead, calculate_priority


@pytest.fixture
def sample_lead():
    """Sample lead for testing."""
    return {
        "id": "test-lead-1",
        "name": "Moxie Therapy PLLC",
        "city": "Hartford",
        "state": "CT",
        "email": "contact@moxietherapy.com",
        "phone": "",
        "filing_date": "2026-05-19",
        "naics": "621330 - Mental Health Practitioners",
        "entity_type": "LLC",
        "citizenship": "Domestic",
        "agent_name": "Test Agent",
    }


def test_score_pllc_fast_track(sample_lead):
    """PLLC should get fast-track scoring."""
    result = score_lead(sample_lead)
    
    assert "score" in result
    assert result["score"] >= 80  # PLLC fast-track base
    assert result["tier"] == "pllc_fast_track"


def test_score_naics_tier():
    """Test NAICS tier scoring."""
    lead = {
        "id": "test-lead-2",
        "name": "ABC Construction LLC",
        "city": "New Haven",
        "state": "CT",
        "email": "info@abcconstruction.com",
        "phone": "860-555-1234",
        "filing_date": "2026-05-18",
        "naics": "236220 - Commercial Building Construction",
        "entity_type": "LLC",
        "citizenship": "Domestic",
    }
    
    result = score_lead(lead)
    
    assert "score" in result
    assert "naics_score" in result
    assert result["naics_score"] > 0  # Tier 1 should have good score


def test_score_email_domain():
    """Test email domain scoring."""
    professional_lead = {
        "id": "test-lead-3",
        "name": "Smith Law Office LLC",
        "city": "Stamford",
        "email": "contact@smithlaw.law",
        "filing_date": "2026-05-19",
        "naics": "541110 - Offices of Lawyers",
        "entity_type": "LLC",
        "citizenship": "Domestic",
    }
    
    free_email_lead = {
        "id": "test-lead-4",
        "name": "Joe's Plumbing",
        "city": "Waterbury",
        "email": "joe.plumbing@gmail.com",
        "filing_date": "2026-05-19",
        "naics": "238220 - Plumbing",
        "entity_type": "LLC",
        "citizenship": "Domestic",
    }
    
    prof_result = score_lead(professional_lead)
    free_result = score_lead(free_email_lead)
    
    assert prof_result["score"] > free_result["score"]


def test_calculate_priority():
    """Test priority calculation with readiness signals."""
    lead = {
        "id": "test-lead-5",
        "name": "Test Business",
        "score": 85,
        "email": "test@business.com",
        "phone": "860-555-1234",
        "website_exists": True,
        "domain_phone": "860-555-1234",
        "brave_phone": "",
        "competitor_displacement": False,
        "citizenship": "Domestic",
        "filing_date": "2026-05-19",
        "source": "recent",
    }
    
    result = calculate_priority(lead)
    
    assert "priority" in result
    assert "readiness_weight" in result
    assert "readiness_signals" in result
    assert len(result["readiness_signals"]) > 0
    assert result["priority"] > 0
    assert result["readiness_weight"] <= 1.0


def test_score_recency():
    """Test that recent filings get higher scores."""
    old_lead = {
        "id": "old-lead",
        "name": "Old Business LLC",
        "city": "Hartford",
        "email": "old@business.com",
        "filing_date": "2026-04-01",
        "naics": "541110",
        "entity_type": "LLC",
        "citizenship": "Domestic",
    }
    
    new_lead = {
        "id": "new-lead",
        "name": "New Business LLC",
        "city": "Hartford",
        "email": "new@business.com",
        "filing_date": "2026-05-19",
        "naics": "541110",
        "entity_type": "LLC",
        "citizenship": "Domestic",
    }
    
    old_result = score_lead(old_lead)
    new_result = score_lead(new_lead)
    
    assert new_result["score"] > old_result["score"]