"""Test harvest idempotency — running twice should skip duplicates."""

import pytest
import uuid
from datetime import datetime, date
from pathlib import Path
import tempfile
import os

# Mock the integrations and state for testing
class MockBacklog:
    def __init__(self):
        self.leads = []
    
    def load(self):
        return self.leads
    
    def save(self, leads):
        self.leads = leads
    
    def add(self, lead):
        self.leads.append(lead)


def test_duplicate_detection():
    """Running harvest twice should skip duplicates on second run."""
    from capabilities.harvest import _apply_filters, _compute_temporal_category
    
    # Simulate a lead
    lead1 = {
        "name": "Test Business LLC",
        "filing_date": "2026-05-01",
        "entity_type": "Domestic LLC",
        "citizenship": "Domestic",
    }
    
    # First harvest - should pass filters
    filter_reason = _apply_filters(lead1)
    assert filter_reason is None, f"Lead should not be filtered: {filter_reason}"
    
    # Second harvest with same lead - deduplication should catch it
    # (This is tested in the actual harvest flow, not here)


def test_filter_tracking():
    """Filters should track breakdown by reason."""
    from capabilities.harvest import _apply_filters
    
    # Test shell name filter
    shell_lead = {
        "name": "ABC Holdings LLC",
        "filing_date": "2026-05-01",
        "entity_type": "Domestic LLC",
        "citizenship": "Domestic",
    }
    reason = _apply_filters(shell_lead)
    assert reason == "shell_name"
    
    # Test foreign citizenship filter
    foreign_lead = {
        "name": "Foreign Business LLC",
        "filing_date": "2026-05-01",
        "entity_type": "Domestic LLC",
        "citizenship": "Foreign",
    }
    reason = _apply_filters(foreign_lead)
    assert reason == "citizenship_foreign"
    
    # Test empty name filter
    empty_lead = {
        "name": "",
        "filing_date": "2026-05-01",
    }
    reason = _apply_filters(empty_lead)
    assert reason == "empty_name"
    
    # Test valid lead
    valid_lead = {
        "name": "Valid Business LLC",
        "filing_date": "2026-05-01",
        "entity_type": "Domestic LLC",
        "citizenship": "Domestic",
    }
    reason = _apply_filters(valid_lead)
    assert reason is None


def test_temporal_category_tracking():
    """Harvest should track leads by temporal category."""
    from capabilities.harvest import _compute_temporal_category
    
    today = date(2026, 5, 20)
    
    # Fresh lead
    fresh_date = (today - __import__('datetime').timedelta(days=5)).isoformat()
    assert _compute_temporal_category(fresh_date, today) == "fresh"
    
    # Recent lead
    recent_date = (today - __import__('datetime').timedelta(days=30)).isoformat()
    assert _compute_temporal_category(recent_date, today) == "recent"
    
    # Historical lead
    historical_date = (today - __import__('datetime').timedelta(days=100)).isoformat()
    assert _compute_temporal_category(historical_date, today) == "historical"