"""The owner-view switch: staff demoing what owners actually see.

It flips the RENDERING, server-side — the owner variant of every page really
renders, rather than staff controls being hidden with CSS. It is not an
authorization boundary: privileged endpoints keep checking the real is_staff.
"""
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

User = get_user_model()


class OwnerViewSwitchTests(TestCase):
    def setUp(self):
        self.staff = User.objects.create_user("staff", password="pw-staff-12345", is_staff=True)
        self.owner = User.objects.create_user("owner", password="pw-owner-12345")

    def toggle(self, next_url="/"):
        return self.client.post(reverse("backoffice:view_toggle"),
                                {"next": next_url}, secure=True)

    # ---- access ----
    def test_owners_cannot_reach_the_toggle(self):
        self.client.force_login(self.owner)
        response = self.toggle()
        self.assertEqual(response.status_code, 302)
        self.assertIn("/accounts/login/", response.url)
        self.assertFalse(self.client.session.get("owner_view"))

    def test_owners_never_see_the_switch(self):
        self.client.force_login(self.owner)
        body = self.client.get(reverse("documents:library"), secure=True).content.decode()
        self.assertNotIn("viewswitch", body)

    # ---- the flip ----
    def test_owner_view_hides_every_staff_surface(self):
        self.client.force_login(self.staff)
        self.toggle()
        self.assertTrue(self.client.session.get("owner_view"))
        body = self.client.get(reverse("documents:library"), secure=True).content.decode()
        for marker in ("page-dropzone", "edit-dialog", "upload-dialog", 'data-kind="collection"', "staff-flag", "nav-staff"):
            self.assertNotIn(marker, body, marker)
        # The staff nav is gone too.
        self.assertNotIn(reverse("backoffice:people"), body)
        self.assertNotIn(reverse("backoffice:trash"), body)

    def test_the_switch_itself_survives_owner_view(self):
        """Without this there is no way back."""
        self.client.force_login(self.staff)
        self.toggle()
        body = self.client.get(reverse("documents:library"), secure=True).content.decode()
        self.assertIn("viewswitch is-on", body)

    def test_no_template_comment_leaks_into_the_footer(self):
        """Django's {# #} comments are single-line; a wrapped one renders as
        page text — which is exactly what happened."""
        self.client.force_login(self.staff)
        body = self.client.get(reverse("documents:library"), secure=True).content.decode()
        self.assertNotIn("staff_ui: the switch", body)
        self.assertNotIn("{#", body)

    def test_toggling_again_restores_staff_view(self):
        self.client.force_login(self.staff)
        self.toggle(); self.toggle()
        self.assertFalse(self.client.session.get("owner_view"))
        body = self.client.get(reverse("documents:library"), secure=True).content.decode()
        self.assertIn("page-dropzone", body)
        self.assertIn('class="viewswitch"', body)

    def test_board_management_follows_the_switch(self):
        self.client.force_login(self.staff)
        self.toggle()
        body = self.client.get(reverse("documents:board"), secure=True).content.decode()
        for marker in ("member-admin", "board-add", "member-dialog"):
            self.assertNotIn(marker, body, marker)

    # ---- redirects ----
    def test_turning_on_from_a_staff_page_lands_on_the_library(self):
        """Owners cannot reach /staff/ pages, so the demo must not sit on one."""
        self.client.force_login(self.staff)
        response = self.toggle(next_url="/staff/owners/")
        self.assertEqual(response.url, reverse("documents:library"))

    def test_turning_off_returns_wherever_you_were(self):
        self.client.force_login(self.staff)
        self.toggle(next_url="/")
        response = self.toggle(next_url="/board/")
        self.assertEqual(response.url, "/board/")

    def test_an_external_next_is_refused(self):
        self.client.force_login(self.staff)
        for evil in ("https://evil.example", "//evil.example"):
            response = self.toggle(next_url=evil)
            self.assertEqual(response.url, reverse("documents:library"), evil)

    # ---- not an authz boundary ----
    def test_staff_endpoints_still_work_in_owner_view(self):
        """The switch changes rendering, not rights — flipping it must never
        lock the real staff member out of acting."""
        self.client.force_login(self.staff)
        self.toggle()
        response = self.client.get(reverse("backoffice:people"), secure=True)
        self.assertEqual(response.status_code, 200)
