import unittest
from pyramid import testing

from ..models.product import get_media_type, VIDEO_EXTENSIONS, AUDIO_EXTENSIONS, IMAGE_EXTENSIONS, DOCUMENT_EXTENSIONS


class TestMediaTypeDetection(unittest.TestCase):
    """Test media type detection functions."""

    def test_video_extensions(self):
        """Test that video extensions are correctly identified."""
        for ext in VIDEO_EXTENSIONS:
            self.assertEqual(get_media_type(ext), "video")
            # Test case insensitivity
            self.assertEqual(get_media_type(ext.upper()), "video")

    def test_audio_extensions(self):
        """Test that audio extensions are correctly identified."""
        for ext in AUDIO_EXTENSIONS:
            self.assertEqual(get_media_type(ext), "audio")
            # Test case insensitivity
            self.assertEqual(get_media_type(ext.upper()), "audio")

    def test_image_extensions(self):
        """Test that image extensions are correctly identified."""
        for ext in IMAGE_EXTENSIONS:
            self.assertEqual(get_media_type(ext), "image")
            # Test case insensitivity
            self.assertEqual(get_media_type(ext.upper()), "image")

    def test_document_extensions(self):
        """Test that document extensions are correctly identified."""
        for ext in DOCUMENT_EXTENSIONS:
            self.assertEqual(get_media_type(ext), "document")
            # Test case insensitivity
            self.assertEqual(get_media_type(ext.upper()), "document")

    def test_unsupported_extensions(self):
        """Test that unsupported extensions return None."""
        unsupported = ["zip", "doc", "txt", "exe", "docx"]
        for ext in unsupported:
            self.assertIsNone(get_media_type(ext))

    def test_none_extension(self):
        """Test that None extension returns None."""
        self.assertIsNone(get_media_type(None))

    def test_empty_extension(self):
        """Test that empty string extension returns None."""
        self.assertIsNone(get_media_type(""))


class TestPlayerView(unittest.TestCase):
    """Test player view controller."""

    def setUp(self):
        self.config = testing.setUp()
        from ..models.meta import Base
        from sqlalchemy import create_engine

        engine = create_engine("sqlite:///:memory:")
        Base.metadata.create_all(engine)

        from sqlalchemy.orm import sessionmaker

        Session = sessionmaker(bind=engine)
        self.session = Session()

    def tearDown(self):
        self.session.close()
        testing.tearDown()

    def test_player_requires_feature_flag(self):
        """Test that player route requires feature flag to be enabled."""
        from ..views.player import player
        from pyramid.httpexceptions import HTTPNotFound

        request = testing.DummyRequest()
        request.popout_player_enabled = False
        request.product = None

        result = player(request)
        self.assertIsInstance(result, HTTPNotFound)

    def test_player_requires_product(self):
        """Test that player route requires a valid product."""
        from ..views.player import player
        from pyramid.httpexceptions import HTTPNotFound

        request = testing.DummyRequest()
        request.popout_player_enabled = True
        request.product = None

        result = player(request)
        self.assertIsInstance(result, HTTPNotFound)

    def test_media_type_constants_are_lowercase(self):
        """Test that all media type extension constants are lowercase."""
        for ext in VIDEO_EXTENSIONS:
            self.assertEqual(ext, ext.lower(), f"VIDEO_EXTENSIONS contains non-lowercase: {ext}")

        for ext in AUDIO_EXTENSIONS:
            self.assertEqual(ext, ext.lower(), f"AUDIO_EXTENSIONS contains non-lowercase: {ext}")

        for ext in IMAGE_EXTENSIONS:
            self.assertEqual(ext, ext.lower(), f"IMAGE_EXTENSIONS contains non-lowercase: {ext}")

        for ext in DOCUMENT_EXTENSIONS:
            self.assertEqual(ext, ext.lower(), f"DOCUMENT_EXTENSIONS contains non-lowercase: {ext}")


if __name__ == "__main__":
    unittest.main()
