import pytest


@pytest.mark.django_db
class TestRIRConfigFilterSet:
    def test_search_by_name(self, rir_config):
        from netbox_rir_manager.filtersets import RIRConfigFilterSet
        from netbox_rir_manager.models import RIRConfig

        qs = RIRConfig.objects.all()
        fs = RIRConfigFilterSet({"q": "ARIN"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_search_no_match(self, rir_config):
        from netbox_rir_manager.filtersets import RIRConfigFilterSet
        from netbox_rir_manager.models import RIRConfig

        qs = RIRConfig.objects.all()
        fs = RIRConfigFilterSet({"q": "nonexistent"}, queryset=qs)
        assert fs.qs.count() == 0

    def test_filter_by_active(self, rir, rir_config):
        from netbox_rir_manager.filtersets import RIRConfigFilterSet
        from netbox_rir_manager.models import RIRConfig

        RIRConfig.objects.create(rir=rir, name="Inactive", is_active=False)
        qs = RIRConfig.objects.all()

        fs = RIRConfigFilterSet({"is_active": True}, queryset=qs)
        assert fs.qs.count() == 1
        assert fs.qs.first().name == rir_config.name

        fs = RIRConfigFilterSet({"is_active": False}, queryset=qs)
        assert fs.qs.count() == 1
        assert fs.qs.first().name == "Inactive"


@pytest.mark.django_db
class TestRIROrganizationFilterSet:
    def test_search_by_handle(self, rir_organization):
        from netbox_rir_manager.filtersets import RIROrganizationFilterSet
        from netbox_rir_manager.models import RIROrganization

        qs = RIROrganization.objects.all()
        fs = RIROrganizationFilterSet({"q": "TESTORG"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_search_by_name(self, rir_organization):
        from netbox_rir_manager.filtersets import RIROrganizationFilterSet
        from netbox_rir_manager.models import RIROrganization

        qs = RIROrganization.objects.all()
        fs = RIROrganizationFilterSet({"q": "Test Organization"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_filter_by_rir_config(self, rir_organization, rir_config):
        from netbox_rir_manager.filtersets import RIROrganizationFilterSet
        from netbox_rir_manager.models import RIROrganization

        qs = RIROrganization.objects.all()
        fs = RIROrganizationFilterSet({"rir_config_id": [rir_config.pk]}, queryset=qs)
        assert fs.qs.count() == 1


@pytest.mark.django_db
class TestRIRContactFilterSet:
    def test_search_by_handle(self, rir_contact):
        from netbox_rir_manager.filtersets import RIRContactFilterSet
        from netbox_rir_manager.models import RIRContact

        qs = RIRContact.objects.all()
        fs = RIRContactFilterSet({"q": "JD123"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_search_by_last_name(self, rir_contact):
        from netbox_rir_manager.filtersets import RIRContactFilterSet
        from netbox_rir_manager.models import RIRContact

        qs = RIRContact.objects.all()
        fs = RIRContactFilterSet({"q": "Doe"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_filter_by_contact_type(self, rir_contact):
        from netbox_rir_manager.filtersets import RIRContactFilterSet
        from netbox_rir_manager.models import RIRContact

        qs = RIRContact.objects.all()
        fs = RIRContactFilterSet({"contact_type": "PERSON"}, queryset=qs)
        assert fs.qs.count() == 1

        fs = RIRContactFilterSet({"contact_type": "ROLE"}, queryset=qs)
        assert fs.qs.count() == 0


@pytest.mark.django_db
class TestRIRNetworkFilterSet:
    def test_search_by_handle(self, rir_network):
        from netbox_rir_manager.filtersets import RIRNetworkFilterSet
        from netbox_rir_manager.models import RIRNetwork

        qs = RIRNetwork.objects.all()
        fs = RIRNetworkFilterSet({"q": "NET-192"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_search_by_net_name(self, rir_network):
        from netbox_rir_manager.filtersets import RIRNetworkFilterSet
        from netbox_rir_manager.models import RIRNetwork

        qs = RIRNetwork.objects.all()
        fs = RIRNetworkFilterSet({"q": "EXAMPLE"}, queryset=qs)
        assert fs.qs.count() == 1


@pytest.mark.django_db
class TestRIRSyncLogFilterSet:
    def test_search_by_handle(self, rir_config):
        from netbox_rir_manager.filtersets import RIRSyncLogFilterSet
        from netbox_rir_manager.models import RIRSyncLog

        RIRSyncLog.objects.create(
            rir_config=rir_config, operation="sync", object_type="org", object_handle="SEARCHME", status="success"
        )
        qs = RIRSyncLog.objects.all()
        fs = RIRSyncLogFilterSet({"q": "SEARCHME"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_filter_by_operation(self, rir_config):
        from netbox_rir_manager.filtersets import RIRSyncLogFilterSet
        from netbox_rir_manager.models import RIRSyncLog

        RIRSyncLog.objects.create(
            rir_config=rir_config, operation="sync", object_type="org", object_handle="A", status="success"
        )
        RIRSyncLog.objects.create(
            rir_config=rir_config, operation="create", object_type="org", object_handle="B", status="success"
        )
        qs = RIRSyncLog.objects.all()
        fs = RIRSyncLogFilterSet({"operation": "sync"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_filter_by_status(self, rir_config):
        from netbox_rir_manager.filtersets import RIRSyncLogFilterSet
        from netbox_rir_manager.models import RIRSyncLog

        RIRSyncLog.objects.create(
            rir_config=rir_config, operation="sync", object_type="org", object_handle="A", status="success"
        )
        RIRSyncLog.objects.create(
            rir_config=rir_config, operation="sync", object_type="org", object_handle="B", status="error"
        )
        qs = RIRSyncLog.objects.all()
        fs = RIRSyncLogFilterSet({"status": "error"}, queryset=qs)
        assert fs.qs.count() == 1


@pytest.mark.django_db
class TestRIRTicketFilterSet:
    def test_search_by_ticket_number(self, rir_ticket):
        from netbox_rir_manager.filtersets import RIRTicketFilterSet
        from netbox_rir_manager.models import RIRTicket

        qs = RIRTicket.objects.all()
        fs = RIRTicketFilterSet({"q": "TKT-TEST"}, queryset=qs)
        assert fs.qs.count() == 1

    def test_search_no_match(self, rir_ticket):
        from netbox_rir_manager.filtersets import RIRTicketFilterSet
        from netbox_rir_manager.models import RIRTicket

        qs = RIRTicket.objects.all()
        fs = RIRTicketFilterSet({"q": "nonexistent"}, queryset=qs)
        assert fs.qs.count() == 0

    def test_filter_by_status(self, rir_ticket):
        from netbox_rir_manager.filtersets import RIRTicketFilterSet
        from netbox_rir_manager.models import RIRTicket

        qs = RIRTicket.objects.all()
        fs = RIRTicketFilterSet({"status": "pending_review"}, queryset=qs)
        assert fs.qs.count() == 1

        fs = RIRTicketFilterSet({"status": "resolved"}, queryset=qs)
        assert fs.qs.count() == 0

    def test_filter_by_ticket_type(self, rir_ticket):
        from netbox_rir_manager.filtersets import RIRTicketFilterSet
        from netbox_rir_manager.models import RIRTicket

        qs = RIRTicket.objects.all()
        fs = RIRTicketFilterSet({"ticket_type": "IPV4_SIMPLE_REASSIGN"}, queryset=qs)
        assert fs.qs.count() == 1

        fs = RIRTicketFilterSet({"ticket_type": "IPV6_REALLOCATE"}, queryset=qs)
        assert fs.qs.count() == 0

    def test_filter_by_rir_config(self, rir_ticket, rir_config):
        from netbox_rir_manager.filtersets import RIRTicketFilterSet
        from netbox_rir_manager.models import RIRTicket

        qs = RIRTicket.objects.all()
        fs = RIRTicketFilterSet({"rir_config_id": [rir_config.pk]}, queryset=qs)
        assert fs.qs.count() == 1
