from birds.models import BirdSightingEntry, BirdSightingLogData


class TestBirdSightingEntry:
    def test_create_entry(self, db, bird):
        entry = BirdSightingEntry(bird_id=bird.id, quantity=3)
        assert entry.bird_id == bird.id
        assert entry.quantity == 3
        assert entry.sighting_notes is None

    def test_entry_default_quantity(self, db, bird):
        entry = BirdSightingEntry(bird_id=bird.id)
        assert entry.quantity == 1

    def test_entry_str(self, db, bird):
        entry = BirdSightingEntry(
            bird_id=bird.id, quantity=2, sighting_notes="in the tree"
        )
        expected = f"{bird.common_name} x2 (in the tree)"
        assert str(entry) == expected

    def test_entry_str_no_notes(self, db, bird):
        entry = BirdSightingEntry(bird_id=bird.id, quantity=1)
        expected = f"{bird.common_name} x1"
        assert str(entry) == expected

    def test_entry_bird_property(self, db, bird):
        entry = BirdSightingEntry(bird_id=bird.id)
        assert entry.bird == bird

    def test_entry_bird_property_none(self, db):
        entry = BirdSightingEntry(bird_id=None)
        assert entry.bird is None

    def test_entry_asdict(self, db, bird):
        entry = BirdSightingEntry(
            bird_id=bird.id, quantity=4, sighting_notes="flying south"
        )
        d = entry.asdict
        assert d["bird_id"] == bird.id
        assert d["quantity"] == 4
        assert d["sighting_notes"] == "flying south"


class TestBirdSightingLogData:
    def test_empty_logdata(self):
        logdata = BirdSightingLogData()
        assert logdata.birds is None
        assert logdata.duration_minutes is None
        assert logdata.observation_type is None
        assert logdata.party_size is None
        assert logdata.complete_checklist is None

    def test_with_birds(self, db, bird):
        entry = BirdSightingEntry(bird_id=bird.id, quantity=2).asdict
        logdata = BirdSightingLogData(
            birds=[entry],
            duration_minutes=15,
            observation_type="Traveling",
            party_size=3,
            complete_checklist=True,
        )
        assert len(logdata.birds) == 1
        assert logdata.duration_minutes == 15
        assert logdata.observation_type == "Traveling"
        assert logdata.party_size == 3
        assert logdata.complete_checklist is True

    def test_bird_list_property(self, db, bird):
        entry = BirdSightingEntry(bird_id=bird.id, quantity=2).asdict
        logdata = BirdSightingLogData(birds=[entry])
        assert bird.common_name in logdata.bird_list

    def test_bird_list_empty(self):
        logdata = BirdSightingLogData()
        assert logdata.bird_list == ""

    def test_as_html_with_all_fields(self, db, bird):
        entry = BirdSightingEntry(bird_id=bird.id, quantity=2).asdict
        logdata = BirdSightingLogData(
            birds=[entry],
            observation_type="Stationary",
            distance="2 km",
            area="Woodland",
            party_size=4,
            complete_checklist=True,
            weather="Sunny",
        )
        html = logdata.as_html()
        assert "Stationary" in html
        assert "2 km" in html
        assert "Woodland" in html
        assert "Party size: 4" in html
        assert "Complete checklist: True" in html
        assert "Sunny" in html
        assert bird.common_name in html
