from django.test import TestCase
from django.urls import reverse

from apps.core.video import parse_youtube_video_id, youtube_embed_url
from apps.pages.models import (
    AboutSection,
    CustomPage,
    CustomPageSection,
    HomepageSection,
    PageVideo,
)
from apps.products.models import MainProduct, ProductVideo


class YouTubeParsingTest(TestCase):
    def test_watch_url(self):
        self.assertEqual(
            parse_youtube_video_id("https://www.youtube.com/watch?v=dQw4w9WgXcQ"),
            "dQw4w9WgXcQ",
        )

    def test_short_url(self):
        self.assertEqual(
            parse_youtube_video_id("https://youtu.be/dQw4w9WgXcQ"),
            "dQw4w9WgXcQ",
        )

    def test_embed_url(self):
        url = youtube_embed_url("https://youtu.be/dQw4w9WgXcQ")
        self.assertEqual(url, "https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ")


class HomepageVideoSectionTest(TestCase):
    def test_video_section_renders_youtube_embed(self):
        HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_VIDEO,
            title="Demo",
            video_source="youtube",
            video_url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
            is_active=True,
        )
        response = self.client.get(reverse("pages:home"))
        self.assertContains(response, "youtube-nocookie.com/embed/dQw4w9WgXcQ")
        self.assertContains(response, "video-block--md")


class AboutVideoSectionTest(TestCase):
    def test_video_section_renders_on_about_page(self):
        AboutSection.objects.create(
            section_type=AboutSection.TYPE_VIDEO,
            title="Overview",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            video_size="lg",
            is_active=True,
        )
        response = self.client.get(reverse("pages:about"))
        self.assertContains(response, "video-block--lg")
        self.assertContains(response, "Overview")


class CustomPageVideoSectionTest(TestCase):
    def test_video_section_renders_on_custom_page(self):
        page = CustomPage.objects.create(
            title="Media",
            slug="media-page",
            is_published=True,
        )
        CustomPageSection.objects.create(
            page=page,
            section_type=CustomPageSection.TYPE_VIDEO,
            title="Walkthrough",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            is_active=True,
        )
        response = self.client.get(reverse("pages:custom_page", kwargs={"slug": "media-page"}))
        self.assertContains(response, "Walkthrough")
        self.assertContains(response, "iframe")


class PageVideoTest(TestCase):
    def _create_page_videos(self, page, count):
        for index in range(count):
            PageVideo.objects.create(
                page=page,
                title=f"Video {index + 1}",
                video_source="youtube",
                video_url="https://youtu.be/dQw4w9WgXcQ",
                order=index,
                is_active=True,
            )

    def test_contact_page_video(self):
        PageVideo.objects.create(
            page=PageVideo.PAGE_CONTACT,
            title="Intro",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            is_active=True,
        )
        response = self.client.get(reverse("pages:contact"))
        self.assertContains(response, "Intro")
        self.assertContains(response, "youtube-nocookie.com/embed/dQw4w9WgXcQ")

    def test_faq_page_video(self):
        PageVideo.objects.create(
            page=PageVideo.PAGE_FAQ,
            title="Tutorial",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            is_active=True,
        )
        response = self.client.get(reverse("pages:faq"))
        self.assertContains(response, "Tutorial")

    def test_page_shows_three_video_preview_and_archive_link(self):
        self._create_page_videos(PageVideo.PAGE_FAQ, 8)

        response = self.client.get(reverse("pages:faq"))

        self.assertEqual(response.content.count(b"youtube-nocookie.com/embed"), 3)
        self.assertContains(
            response,
            reverse("pages:video_archive", kwargs={"library": PageVideo.PAGE_FAQ}),
        )
        self.assertContains(response, "More videos")

    def test_page_video_archive_is_paginated(self):
        self._create_page_videos(PageVideo.PAGE_FAQ, 8)
        archive_url = reverse("pages:video_archive", kwargs={"library": PageVideo.PAGE_FAQ})

        first_page = self.client.get(archive_url)
        second_page = self.client.get(archive_url, {"page": 2})

        self.assertEqual(first_page.status_code, 200)
        self.assertEqual(first_page.content.count(b"youtube-nocookie.com/embed"), 6)
        self.assertContains(first_page, "Page 1 of 2")
        self.assertContains(first_page, "?page=2")
        self.assertEqual(second_page.content.count(b"youtube-nocookie.com/embed"), 2)
        self.assertContains(second_page, "Page 2 of 2")

    def test_unknown_page_video_archive_returns_404(self):
        response = self.client.get(
            reverse("pages:video_archive", kwargs={"library": "unknown"})
        )
        self.assertEqual(response.status_code, 404)


class ProductVideoTest(TestCase):
    def setUp(self):
        self.main_product = MainProduct.objects.create(
            name="Tecvico",
            slug="tecvico",
            short_description="Short",
            description="Long",
            is_active=True,
        )

    def test_product_video_renders_on_detail_page(self):
        ProductVideo.objects.create(
            main_product=self.main_product,
            title="Product Demo",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            video_size="full",
            is_active=True,
        )
        response = self.client.get(
            reverse("products:main_product_detail", kwargs={"main_slug": "tecvico"})
        )
        self.assertContains(response, "Product Demo")
        self.assertContains(response, "video-block--full")

    def test_product_video_preview_links_to_paginated_archive(self):
        for index in range(7):
            ProductVideo.objects.create(
                main_product=self.main_product,
                title=f"Product video {index + 1}",
                video_source="youtube",
                video_url="https://youtu.be/dQw4w9WgXcQ",
                order=index,
                is_active=True,
            )
        archive_url = reverse(
            "products:main_product_videos",
            kwargs={"main_slug": self.main_product.slug},
        )

        detail_response = self.client.get(self.main_product.get_absolute_url())
        archive_response = self.client.get(archive_url)
        second_page = self.client.get(archive_url, {"page": 2})

        self.assertEqual(detail_response.content.count(b"youtube-nocookie.com/embed"), 3)
        self.assertContains(detail_response, archive_url)
        self.assertEqual(archive_response.content.count(b"youtube-nocookie.com/embed"), 6)
        self.assertEqual(second_page.content.count(b"youtube-nocookie.com/embed"), 1)


class VideoStyledBackgroundTest(TestCase):
    def test_styled_background_renders_panel(self):
        HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_VIDEO,
            title="Styled Demo",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            video_styled_background=True,
            is_active=True,
        )
        response = self.client.get(reverse("pages:home"))
        self.assertContains(response, "video-panel--styled")
        self.assertContains(response, "video-panel-blob")

    def test_plain_background_without_panel(self):
        HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_VIDEO,
            title="Plain Demo",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            video_styled_background=False,
            is_active=True,
        )
        response = self.client.get(reverse("pages:home"))
        self.assertNotContains(response, "video-panel--styled")
        self.assertContains(response, "Plain Demo")


class VideoDescriptionFormatTest(TestCase):
    def test_plain_description_preserves_line_breaks(self):
        HomepageSection.objects.create(
            section_type=HomepageSection.TYPE_VIDEO,
            title="Demo",
            description="First line\nSecond line",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            is_active=True,
        )
        response = self.client.get(reverse("pages:home"))
        self.assertContains(response, "First line")
        self.assertContains(response, "Second line")
        self.assertContains(response, "<br>")

    def test_markdown_description_renders(self):
        PageVideo.objects.create(
            page=PageVideo.PAGE_FAQ,
            title="Guide",
            description="**Bold** intro",
            description_format="markdown",
            video_source="youtube",
            video_url="https://youtu.be/dQw4w9WgXcQ",
            is_active=True,
        )
        response = self.client.get(reverse("pages:faq"))
        self.assertContains(response, "<strong>Bold</strong>")
