from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User
from chat.models import Channel


class VoiceChannelsTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='testuser', password='password')
        self.client = Client()

    def test_voice_credentials_anonymous(self):
        # Anonymous user should be redirected to login
        url = reverse('voice_credentials')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 302)

    def test_voice_credentials_authenticated(self):
        # Authenticated user should get ICE servers JSON
        self.client.login(username='testuser', password='password')
        url = reverse('voice_credentials')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertIn('iceServers', data)
        self.assertTrue(len(data['iceServers']) > 0)
        self.assertIn('urls', data['iceServers'][0])

    def test_channel_model_default_type(self):
        # Default channel type should be 'text'
        channel = Channel.objects.create(name='general', created_by=self.user)
        self.assertEqual(channel.channel_type, 'text')

    def test_voice_channel_type(self):
        # Can create channel of type 'voice'
        channel = Channel.objects.create(name='Gaming', channel_type='voice', created_by=self.user)
        self.assertEqual(channel.channel_type, 'voice')
