"""Integration tests for Wikidata firmographics."""
import os
import sys

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

from fmatch.saas.operators import wikidata_profiles  # type: ignore


class TestWikidataFirmographics:
    """Test Wikidata firmographics integration."""

    def test_company_to_domain_attaches_metadata(self):
        """Test that company_to_domain attaches Wikidata profile."""
        try:
            from fmatch.saas.operators import company_to_domain  # type: ignore
        except Exception as exc:
            pytest.skip(f"company_to_domain import failed: {exc}")

        try:
            result = company_to_domain('Verizon Communications')
        except Exception as exc:
            pytest.skip(f"company_to_domain unavailable: {exc}")

        assert 'value' in result
        assert 'metadata' in result['value']

        if 'wikidata_profile' in result['value']['metadata']:
            profile = result['value']['metadata']['wikidata_profile']
            assert 'wikidata_id' in profile
            assert 'domain' in profile


class TestParentCompanyDetection:
    """Test parent company detection with Wikidata."""

    def test_wikidata_parent_lookup(self):
        """Test parent lookup via Wikidata relationships."""
        parent_info = wikidata_profiles.get_parent_company_by_domain("youtube.com")

        if parent_info:
            assert 'parent_name' in parent_info
            assert 'parent_wikidata_id' in parent_info

    def test_detect_parent_uses_wikidata(self):
        """Test that detect_parent_company queries Wikidata."""
        try:
            from fmatch.saas.operators import detect_parent_company  # type: ignore
        except Exception as exc:
            pytest.skip(f"detect_parent_company import failed: {exc}")

        try:
            result = detect_parent_company(company="YouTube", domain="youtube.com")
        except Exception as exc:
            pytest.skip(f"detect_parent_company unavailable: {exc}")

        assert 'parent' in result or result.get('confidence', 0) == 0


class TestDomainFamilyOverlay:
    """Test Wikidata family links overlay into registry."""

    def test_registry_loads_wikidata_links(self):
        """Test that registry overlays Wikidata family links."""
        try:
            from fmatch.saas.services.domain_family import get_registry  # type: ignore
            registry = get_registry()
        except Exception as exc:
            pytest.skip(f"registry unavailable: {exc}")

        domains_before = len(registry.alias_to_family)
        try:
            registry.refresh(force=True)
        except Exception as exc:
            pytest.skip(f"registry refresh unavailable: {exc}")
        domains_after = len(registry.alias_to_family)

        assert domains_after >= domains_before

    def test_wikidata_links_are_loaded(self):
        """Test that Wikidata family links are accessible."""
        links = wikidata_profiles.get_all_family_links()

        assert isinstance(links, dict)


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
