"""
Tests for breadcrumb trail generation.

Breadcrumbs are generated by DjAdminViewMixin in djadmin/plugins/core/mixins.py
and displayed in all admin views via the admin_base.html template.
"""

import pytest

from djadmin import AdminSite, ModelAdmin
from examples.webshop.models import Product


@pytest.fixture
def configured_site():
    """AdminSite with Product registered"""
    site = AdminSite(name='test_admin')

    class ProductAdmin(ModelAdmin):
        list_display = ['name', 'sku', 'price']

    site.register(Product, ProductAdmin)
    return site


@pytest.mark.django_db
class TestBreadcrumbs:
    """Test breadcrumb generation for different views"""

    @pytest.fixture(autouse=True)
    def setup_urls(self, configured_site, settings):
        """Set up URLconf for breadcrumb tests"""
        from django.urls import include, path

        urlpatterns = [
            path('djadmin/', include(configured_site.urls)),
        ]

        settings.ROOT_URLCONF = type('URLConf', (), {'urlpatterns': urlpatterns})

    def test_project_dashboard_breadcrumbs(self, admin_client):
        """Project dashboard should have single breadcrumb (current page)"""
        response = admin_client.get('/djadmin/')
        assert response.status_code == 200

        # Check breadcrumb_list in context
        breadcrumbs = response.context['breadcrumb_list']
        assert len(breadcrumbs) == 1
        assert breadcrumbs[0]['title'] == 'Site administration'  # Uses admin_site.index_title
        assert breadcrumbs[0]['url'] == '/djadmin/'

    def test_app_dashboard_breadcrumbs(self, admin_client):
        """App dashboard should have 2 breadcrumbs: Dashboard (link) + App (current)"""
        response = admin_client.get('/djadmin/webshop/')
        assert response.status_code == 200

        # Check breadcrumb_list in context
        breadcrumbs = response.context['breadcrumb_list']
        assert len(breadcrumbs) == 2

        # First: Dashboard (with link)
        assert breadcrumbs[0]['title'] == 'Site administration'
        assert breadcrumbs[0]['url'] == '/djadmin/'

        # Second: App name (current page, no link)
        assert breadcrumbs[1]['title'] == 'Webshop Example'  # From AppConfig.verbose_name
        assert breadcrumbs[1]['url'] is None

    def test_list_view_breadcrumbs(self, admin_client):
        """List view should have 3 breadcrumbs: Dashboard + App + Model (current)"""
        response = admin_client.get('/djadmin/webshop/product/')
        assert response.status_code == 200

        # Check breadcrumb_list in context
        breadcrumbs = response.context['breadcrumb_list']
        assert len(breadcrumbs) == 3

        # First: Dashboard (with link)
        assert breadcrumbs[0]['title'] == 'Site administration'
        assert breadcrumbs[0]['url'] == '/djadmin/'

        # Second: App (with link)
        assert breadcrumbs[1]['title'] == 'Webshop Example'
        assert breadcrumbs[1]['url'] == '/djadmin/webshop/'

        # Third: Model list (current page, no link since this is the list view)
        assert breadcrumbs[2]['title'] == 'Products'
        assert breadcrumbs[2]['url'] is None  # Current page, no link

    def test_add_view_breadcrumbs(self, admin_client):
        """Add view should have 4 breadcrumbs: Dashboard + App + Model + Add (current)"""
        response = admin_client.get('/djadmin/webshop/product/__new__/')
        assert response.status_code == 200

        # Check breadcrumb_list in context
        breadcrumbs = response.context['breadcrumb_list']
        assert len(breadcrumbs) == 4

        # First: Dashboard (with link)
        assert breadcrumbs[0]['title'] == 'Site administration'
        assert breadcrumbs[0]['url'] == '/djadmin/'

        # Second: App (with link)
        assert breadcrumbs[1]['title'] == 'Webshop Example'
        assert breadcrumbs[1]['url'] == '/djadmin/webshop/'

        # Third: Model list (with link)
        assert breadcrumbs[2]['title'] == 'Products'
        assert breadcrumbs[2]['url'] == '/djadmin/webshop/product/'

        # Fourth: Add action (current page, no link)
        assert breadcrumbs[3]['title'] == 'Add'
        assert breadcrumbs[3]['url'] is None

    def test_edit_view_breadcrumbs(self, admin_client, product_factory):
        """Edit view should have 4 breadcrumbs: Dashboard + App + Model + Edit (current)"""
        product = product_factory()

        # Note: URL pattern changed from /actions/editrecordaction/ to /actions/edit/ in Phase 5A
        response = admin_client.get(f'/djadmin/webshop/product/{product.pk}/actions/edit/')
        assert response.status_code == 200

        # Check breadcrumb_list in context
        breadcrumbs = response.context['breadcrumb_list']
        assert len(breadcrumbs) == 4

        # First: Dashboard (with link)
        assert breadcrumbs[0]['title'] == 'Site administration'
        assert breadcrumbs[0]['url'] == '/djadmin/'

        # Second: App (with link)
        assert breadcrumbs[1]['title'] == 'Webshop Example'
        assert breadcrumbs[1]['url'] == '/djadmin/webshop/'

        # Third: Model list (with link)
        assert breadcrumbs[2]['title'] == 'Products'
        assert breadcrumbs[2]['url'] == '/djadmin/webshop/product/'

        # Fourth: Edit action (current page, no link)
        assert breadcrumbs[3]['title'] == 'Edit'
        assert breadcrumbs[3]['url'] is None

    def test_breadcrumb_html_rendering(self, admin_client):
        """Test breadcrumb HTML rendering in template"""
        response = admin_client.get('/djadmin/webshop/product/__new__/')
        assert response.status_code == 200

        content = response.content.decode('utf-8')

        # Check for breadcrumb navigation
        assert 'aria-label="Breadcrumb"' in content

        # Check breadcrumb structure
        assert '<a href="/djadmin/">Site administration</a>' in content
        assert '<a href="/djadmin/webshop/">Webshop Example</a>' in content
        assert '<a href="/djadmin/webshop/product/">Products</a>' in content
        assert '<span>Add</span>' in content  # Current page (no link)

    def test_breadcrumb_separator_rendering(self, admin_client):
        """Test that breadcrumb separators are rendered between items"""
        response = admin_client.get('/djadmin/webshop/product/')
        content = response.content.decode('utf-8')

        # The CSS uses :before pseudo-element for separators,
        # so we just verify the list structure exists
        assert '<ol>' in content
        assert '<li>' in content
        assert 'aria-label="Breadcrumb"' in content
