"""Role-aware My SME-Hub dashboard.

A migrated investor now holds a real InvestorProfile shell, so the dashboard
must reflect their persona (verification status + the right hub) instead of the
entrepreneur "register a business" framing.
"""

from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse

from apps.core.permissions.roles import UserRole
from apps.smehub.onboarding.models import EntrepreneurProfile, InvestorProfile

User = get_user_model()


class MyDashboardRoleAwarenessTests(TestCase):
    def _client_for(self, user):
        c = Client(SERVER_NAME="localhost")
        c.force_login(user)
        return c

    def test_entrepreneur_sees_repository_search_box(self):
        """FRREP-INT008 — the passive recommendation list was the only surface
        touching the Repository; entrepreneurs should also be able to search
        it directly from their SME-Hub dashboard."""
        user = User.objects.create_user(
            email="entrepreneur@example.com", password="x", role=UserRole.ENTREPRENEUR,
        )
        EntrepreneurProfile.objects.create(user=user)
        html = self._client_for(user).get(
            reverse("smehub_onboarding:my_dashboard")
        ).content.decode()
        self.assertIn("Search the Repository", html)
        # The search box wires up to the smehub_search endpoint via JS —
        # `{% url %}` resolves to the path (not the URL *name*), so assert
        # on the resolved path plus the Alpine component that drives it.
        self.assertIn(reverse("repo_integration:smehub_search"), html)
        self.assertIn("smehubResearchSearch", html)

    def test_investor_with_shell_sees_persona_not_entrepreneur_copy(self):
        user = User.objects.create_user(
            email="inv@example.com", password="x", role=UserRole.INVESTOR,
        )
        InvestorProfile.objects.create(user=user)
        html = self._client_for(user).get(
            reverse("smehub_onboarding:my_dashboard")
        ).content.decode()
        self.assertIn("Investor profile", html)
        self.assertIn("Investment hub", html)            # role hub link
        self.assertNotIn("hasn&#x27;t been started", html)
        self.assertNotIn("register a business", html.lower())

    def test_admin_gets_explainer_panel_and_workspaces_no_complete_cta(self):
        user = User.objects.create_user(
            email="adm@example.com", password="x", role=UserRole.SME_ADMIN,
        )
        html = self._client_for(user).get(
            reverse("smehub_onboarding:my_dashboard")
        ).content.decode()
        # Admins hold no SME-Hub profile by design — the panel says why and
        # routes them to their real work surface instead of a dead end.
        self.assertIn("No profile — by design", html)
        self.assertIn("Open verification hub", html)
        self.assertIn("Admin workspaces", html)
        self.assertIn("Verification hub", html)
        self.assertIn("Pipeline hub", html)
        self.assertNotIn("Complete profile", html)       # admins have no role profile

    def test_non_smehub_role_gets_explainer_and_public_links(self):
        user = User.objects.create_user(
            email="res@example.com", password="x", role=UserRole.RESEARCHER,
        )
        html = self._client_for(user).get(
            reverse("smehub_onboarding:my_dashboard")
        ).content.decode()
        self.assertIn("No SME-Hub profile", html)
        self.assertIn("take part in SME-Hub verification", html)
        self.assertIn("Innovation catalogue", html)
        self.assertNotIn("Admin workspaces", html)
        self.assertNotIn("Complete profile", html)
