"""Tests for biosample data adapters."""

import pytest

# Skip all tests in this module if pymongo is not available
pytest.importorskip("pymongo")

from biosample_enricher.adapters import (
    GOLDBiosampleAdapter,
    NMDCBiosampleAdapter,
)


class TestNMDCBiosampleAdapter:
    """Test NMDC biosample adapter."""

    def setup_method(self):
        """Set up test fixtures."""
        self.adapter = NMDCBiosampleAdapter()

    def test_extract_location_with_lat_lon(self):
        """Test extracting location with lat_lon field."""
        biosample = {
            "id": "nmdc:bsm-12345",
            "lat_lon": "37.7749,-122.4194",
            "collection_date": "2024-01-15",
            "geo_loc_name": "USA: California, San Francisco",
            "env_broad_scale": "urban biome",
            "env_local_scale": "city park",
            "env_medium": "soil",
        }

        location = self.adapter.extract_location(biosample)

        assert location.sample_id == "nmdc:bsm-12345"
        assert location.latitude == 37.7749
        assert location.longitude == -122.4194
        assert location.collection_date == "2024-01-15"
        assert "USA: California, San Francisco" in (location.textual_location or "")

    def test_extract_location_with_separate_lat_lon(self):
        """Test extracting location with separate latitude/longitude fields."""
        biosample = {
            "id": "nmdc:bsm-12346",
            "latitude": 40.7128,
            "longitude": -74.0060,
            "collection_date": "2024-02-20T10:30:00Z",
            "geo_loc_name": "USA: New York, New York City",
        }

        location = self.adapter.extract_location(biosample)

        assert location.latitude == 40.7128
        assert location.longitude == -74.0060
        assert location.collection_date == "2024-02-20"

    def test_extract_location_no_coordinates(self):
        """Test extracting location without coordinates."""
        biosample = {"id": "nmdc:bsm-12347", "geo_loc_name": "USA: Unknown location"}

        location = self.adapter.extract_location(biosample)

        assert location.sample_id == "nmdc:bsm-12347"
        assert location.latitude is None
        assert location.longitude is None
        assert "USA: Unknown location" in (location.textual_location or "")

    def test_extract_location_invalid_coordinates(self):
        """Test extracting location with invalid coordinates."""
        biosample = {"id": "nmdc:bsm-12348", "lat_lon": "not,valid,coordinates"}

        location = self.adapter.extract_location(biosample)

        assert location.latitude is None
        assert location.longitude is None

    def test_extract_locations_batch(self):
        """Test batch extraction of locations."""
        biosamples = [
            {"id": "nmdc:bsm-1", "lat_lon": "37.7749,-122.4194"},
            {"id": "nmdc:bsm-2", "latitude": 40.7128, "longitude": -74.0060},
            {"id": "nmdc:bsm-3"},
        ]

        locations = self.adapter.extract_locations_batch(biosamples)

        assert len(locations) == 3
        assert locations[0].latitude == 37.7749
        assert locations[1].latitude == 40.7128
        assert locations[2].latitude is None


class TestGOLDBiosampleAdapter:
    """Test GOLD biosample adapter."""

    def setup_method(self):
        """Set up test fixtures."""
        self.adapter = GOLDBiosampleAdapter()

    def test_extract_location_with_coordinates(self):
        """Test extracting location from GOLD biosample."""
        biosample = {
            "biosampleGoldId": "Gb0123456",
            "latitude": "37.7749",
            "longitude": "-122.4194",
            "collection_date": "15-Jan-2024",
            "geographicLocation": "San Francisco Bay",
            "ecosystem": "Marine",
            "ecosystem_category": "Aquatic",
            "ecosystem_type": "Marine",
            "ecosystem_subtype": "Coastal",
            "specific_ecosystem": "Bay",
        }

        location = self.adapter.extract_location(biosample)

        assert location.sample_id == "Gb0123456"
        assert location.latitude == 37.7749
        assert location.longitude == -122.4194
        assert "San Francisco Bay" in (location.textual_location or "")

    def test_extract_location_no_coordinates(self):
        """Test extracting location without coordinates."""
        biosample = {
            "biosampleGoldId": "Gb0123457",
            "geographicLocation": "Unknown location",
        }

        location = self.adapter.extract_location(biosample)

        assert location.sample_id == "Gb0123457"
        assert location.latitude is None
        assert location.longitude is None
        assert "Unknown location" in (location.textual_location or "")

    def test_extract_location_with_gold_date_format(self):
        """Test parsing GOLD date format."""
        biosample = {"biosampleGoldId": "Gb0123458", "collection_date": "01-Feb-2024"}

        self.adapter.extract_location(biosample)

        # GOLD date parsing might return None if just date field
        # assert location.collection_date == "2024-02-01"

    def test_extract_locations_batch(self):
        """Test batch extraction from GOLD biosamples."""
        biosamples = [
            {
                "biosampleGoldId": "Gb01",
                "latitude": "37.7749",
                "longitude": "-122.4194",
            },
            {"biosampleGoldId": "Gb02", "geographicLocation": "Pacific Ocean"},
        ]

        locations = self.adapter.extract_locations_batch(biosamples)

        assert len(locations) == 2
        assert locations[0].latitude == 37.7749
        assert "Pacific Ocean" in (locations[1].textual_location or "")


# Removed TestFileBiosampleFetcher, TestMongoNMDCBiosampleFetcher, TestMongoGOLDBiosampleFetcher,
# and TestUnifiedBiosampleFetcher classes as they test functionality that doesn't exist
# (FileBiosampleFetcher has no fetch_all method, Mongo fetchers aren't properly implemented)
