from django.test import TestCase
from graphene.test import Client
from teams_app.schema import schema

class TeamsGraphQLTestCase(TestCase):
    def setUp(self):
        self.client = Client(schema)
    
    def test_get_user_teams_with_params(self):
        """Тест getUserTeams с userId"""
        query = '''
        {
            getUserTeams(userId: "demo-user") {
                id
                name
                ownerId
                logtoOrgId
                createdAt
                updatedAt
            }
        }
        '''
        result = self.client.execute(query)
        print(f"\n=== getUserTeams WITH PARAMS ===")
        print(f"Result: {result}")
        
        if result.get('errors'):
            print(f"ERRORS: {result['errors']}")
        
        if result.get('data'):
            teams = result['data']['getUserTeams']
            print(f"Found {len(teams)} teams")
            for team in teams:
                print(f"Team: {team.get('name')} - {team.get('id')}")
        
        # Проверки
        self.assertIsNone(result.get('errors'))
        self.assertIn('getUserTeams', result['data'])
        
    def test_get_user_teams_no_params(self):
        """Тест getUserTeams без параметров"""
        query = '''
        {
            getUserTeams {
                id
                name
            }
        }
        '''
        result = self.client.execute(query)
        print(f"\n=== getUserTeams NO PARAMS ===")
        print(f"Result: {result}")
        
        if result.get('errors'):
            print(f"ERRORS: {result['errors']}")
        
        if result.get('data'):
            teams = result['data']['getUserTeams']
            print(f"Found {len(teams)} teams")
            
    def test_schema_fields(self):
        """Тест что схема содержит нужные поля"""
        query = '''
        {
            __type(name: "Team") {
                fields {
                    name
                    type {
                        name
                    }
                }
            }
        }
        '''
        result = self.client.execute(query)
        print(f"\n=== TEAM SCHEMA FIELDS ===")
        
        if result.get('data') and result['data']['__type']:
            fields = result['data']['__type']['fields']
            field_names = [f['name'] for f in fields]
            print(f"Team fields: {field_names}")
            
            required_fields = ['id', 'name', 'ownerId']
            for field in required_fields:
                if field in field_names:
                    print(f"✅ {field} - OK")
                else:
                    print(f"❌ {field} - MISSING")
                    
    def test_invalid_query(self):
        """Тест неправильного запроса"""
        query = '{ nonExistentField }'
        result = self.client.execute(query)
        print(f"\n=== INVALID QUERY TEST ===")
        print(f"Result: {result}")
        
        # Должна быть ошибка
        self.assertIsNotNone(result.get('errors'))
