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

from apps.products.models import Article, ArticleSection, MainProduct, SubProduct


class AdminAccessTest(TestCase):
    def setUp(self):
        self.superuser = User.objects.create_superuser(
            username="admin",
            email="admin@example.com",
            password="securepassword123",
        )
        self.client.login(username="admin", password="securepassword123")
        self.main_product = MainProduct.objects.create(
            name="Test Product",
            short_description="Short",
            description="Description",
        )
        self.sub_product = SubProduct.objects.create(
            main_product=self.main_product,
            name="Sub Product",
            short_description="Short sub",
            description="Sub description",
        )
        self.article = Article.objects.create(
            sub_product=self.sub_product,
            title="Test Article",
            description="Article body",
        )
        self.section = ArticleSection.objects.create(
            article=self.article,
            title="Link",
            value="https://example.com",
        )

    def test_main_product_changelist_accessible(self):
        url = reverse("admin:products_mainproduct_changelist")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_sub_product_changelist_accessible(self):
        url = reverse("admin:products_subproduct_changelist")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_article_changelist_accessible(self):
        url = reverse("admin:products_article_changelist")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_article_section_changelist_accessible(self):
        url = reverse("admin:products_articlesection_changelist")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_main_product_change_accessible(self):
        url = reverse("admin:products_mainproduct_change", args=[self.main_product.pk])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_sub_product_change_accessible(self):
        url = reverse("admin:products_subproduct_change", args=[self.sub_product.pk])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_article_change_accessible(self):
        url = reverse("admin:products_article_change", args=[self.article.pk])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_admin_requires_authentication(self):
        self.client.logout()
        url = reverse("admin:products_mainproduct_changelist")
        response = self.client.get(url)
        self.assertNotEqual(response.status_code, 200)
