"""Back-office view tests."""

from django.contrib.auth.models import User
from django.test import TestCase, override_settings
from django.urls import reverse

from allianceauth.tests.auth_utils import AuthUtils

from shop.models import Shop

# AA's base template references static files via a manifest storage that only exists
# after collectstatic. Use plain (non-manifest) static storage so {% static %} resolves
# in tests without a built manifest.
no_manifest_static = override_settings(STORAGES={
    "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
    "staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"},
})


def _member(username, corp_id=2001):
    user = AuthUtils.create_user(username)
    AuthUtils.add_main_character_2(user, username, 1_000_000 + user.pk, corp_id=corp_id, disconnect_signals=True)
    AuthUtils.add_permission_to_user_by_name("shop.basic_access", user)
    return User.objects.get(pk=user.pk)


@no_manifest_static
class BackofficeIndexTests(TestCase):
    def setUp(self):
        self.user = _member("alice")
        self.mine = Shop.objects.create(slug="mine", name="Mine", created_by=self.user)
        other = _member("bob")
        Shop.objects.create(slug="theirs", name="Theirs", created_by=other)
        self.client.force_login(self.user)

    def test_index_lists_only_my_personal_shops(self):
        resp = self.client.get(reverse("backoffice:index"), SERVER_NAME="localhost")
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "Mine")
        self.assertNotContains(resp, "Theirs")

    def test_requires_basic_access(self):
        noperm = AuthUtils.create_user("noperm")
        AuthUtils.add_main_character_2(noperm, "noperm", 1_000_000 + noperm.pk, corp_id=2001, disconnect_signals=True)
        self.client.force_login(noperm)
        resp = self.client.get(reverse("backoffice:index"), SERVER_NAME="localhost")
        self.assertIn(resp.status_code, (302, 403))


@no_manifest_static
class BackofficeCreateTests(TestCase):
    def setUp(self):
        self.user = _member("carol")
        self.client.force_login(self.user)

    def test_create_personal_shop(self):
        resp = self.client.post(
            reverse("backoffice:create"),
            {"name": "New", "slug": "new-shop", "shop_type": "personal", "is_published": "on"},
            SERVER_NAME="localhost",
        )
        self.assertRedirects(resp, reverse("backoffice:index"))
        shop = Shop.objects.get(slug="new-shop")
        self.assertEqual(shop.created_by, self.user)
        self.assertTrue(shop.is_published)


@no_manifest_static
class BackofficeEditDeleteTests(TestCase):
    def setUp(self):
        self.owner = _member("dave")
        self.intruder = _member("eve")
        self.shop = Shop.objects.create(slug="x", name="X", created_by=self.owner)

    def test_owner_can_edit(self):
        self.client.force_login(self.owner)
        resp = self.client.post(
            reverse("backoffice:edit", args=[self.shop.pk]),
            {"name": "X2", "slug": "x", "is_published": ""},
            SERVER_NAME="localhost",
        )
        self.assertRedirects(resp, reverse("backoffice:index"))
        self.shop.refresh_from_db()
        self.assertEqual(self.shop.name, "X2")

    def test_intruder_cannot_edit(self):
        self.client.force_login(self.intruder)
        resp = self.client.post(
            reverse("backoffice:edit", args=[self.shop.pk]),
            {"name": "hax", "slug": "x", "is_published": ""},
            SERVER_NAME="localhost",
        )
        self.assertEqual(resp.status_code, 403)

    def test_owner_can_delete(self):
        self.client.force_login(self.owner)
        resp = self.client.post(reverse("backoffice:delete", args=[self.shop.pk]), SERVER_NAME="localhost")
        self.assertRedirects(resp, reverse("backoffice:index"))
        self.assertFalse(Shop.objects.filter(pk=self.shop.pk).exists())
