"""
Stage 1 Backend Refactor - Basic Tests
Test the refactored models and serializers
"""

import pytest
from django.test import TestCase
from igny8_core.business.planning.models import Clusters
from igny8_core.business.content.models import Tasks, Content, ContentTaxonomy
from igny8_core.modules.writer.serializers import TasksSerializer, ContentSerializer, ContentTaxonomySerializer


class TestClusterModel(TestCase):
    """Test Cluster model after Stage 1 refactor"""
    
    def test_cluster_fields_removed(self):
        """Verify deprecated fields are removed"""
        cluster = Clusters()
        
        # These fields should NOT exist
        assert not hasattr(cluster, 'context_type'), "context_type should be removed"
        assert not hasattr(cluster, 'dimension_meta'), "dimension_meta should be removed"
        
        # These fields SHOULD exist
        assert hasattr(cluster, 'name'), "name field should exist"
        assert hasattr(cluster, 'keywords'), "keywords field should exist"


class TestTasksModel(TestCase):
    """Test Tasks model after Stage 1 refactor"""
    
    def test_tasks_fields_removed(self):
        """Verify deprecated fields are removed"""
        task = Tasks()
        
        # These fields should NOT exist
        assert not hasattr(task, 'cluster_role'), "cluster_role should be removed"
        assert not hasattr(task, 'idea_id'), "idea_id should be removed"
        assert not hasattr(task, 'content_record'), "content_record should be removed"
        assert not hasattr(task, 'entity_type'), "entity_type should be removed"
    
    def test_tasks_fields_added(self):
        """Verify new fields are added"""
        task = Tasks()
        
        # These fields SHOULD exist
        assert hasattr(task, 'content_type'), "content_type should be added"
        assert hasattr(task, 'content_structure'), "content_structure should be added"
        assert hasattr(task, 'taxonomy_term_id'), "taxonomy_term_id should be added"
    
    def test_tasks_status_choices(self):
        """Verify status choices are simplified"""
        # Status should only have 'queued' and 'completed'
        status_choices = [choice[0] for choice in Tasks._meta.get_field('status').choices]
        assert 'queued' in status_choices, "queued should be a valid status"
        assert 'completed' in status_choices, "completed should be a valid status"
        assert len(status_choices) == 2, "Should only have 2 status choices"


class TestContentModel(TestCase):
    """Test Content model after Stage 1 refactor"""
    
    def test_content_fields_removed(self):
        """Verify deprecated fields are removed"""
        content = Content()
        
        # These fields should NOT exist
        assert not hasattr(content, 'task'), "task FK should be removed"
        assert not hasattr(content, 'html_content'), "html_content should be removed (use content_html)"
        assert not hasattr(content, 'entity_type'), "entity_type should be removed"
        assert not hasattr(content, 'cluster_role'), "cluster_role should be removed"
        assert not hasattr(content, 'sync_status'), "sync_status should be removed"
    
    def test_content_fields_added(self):
        """Verify new fields are added"""
        content = Content()
        
        # These fields SHOULD exist
        assert hasattr(content, 'title'), "title should be added"
        assert hasattr(content, 'content_html'), "content_html should be added"
        assert hasattr(content, 'cluster_id'), "cluster_id should be added"
        assert hasattr(content, 'content_type'), "content_type should be added"
        assert hasattr(content, 'content_structure'), "content_structure should be added"
        assert hasattr(content, 'taxonomy_terms'), "taxonomy_terms M2M should exist"
    
    def test_content_status_choices(self):
        """Verify status choices are simplified"""
        # Status should only have 'draft' and 'published'
        status_choices = [choice[0] for choice in Content._meta.get_field('status').choices]
        assert 'draft' in status_choices, "draft should be a valid status"
        assert 'published' in status_choices, "published should be a valid status"
        assert len(status_choices) == 2, "Should only have 2 status choices"


class TestContentTaxonomyModel(TestCase):
    """Test ContentTaxonomy model after Stage 1 refactor"""
    
    def test_taxonomy_fields_removed(self):
        """Verify deprecated fields are removed"""
        taxonomy = ContentTaxonomy()
        
        # These fields should NOT exist
        assert not hasattr(taxonomy, 'description'), "description should be removed"
        assert not hasattr(taxonomy, 'parent'), "parent FK should be removed"
        assert not hasattr(taxonomy, 'sync_status'), "sync_status should be removed"
        assert not hasattr(taxonomy, 'count'), "count should be removed"
        assert not hasattr(taxonomy, 'metadata'), "metadata should be removed"
        assert not hasattr(taxonomy, 'clusters'), "clusters M2M should be removed"
    
    def test_taxonomy_type_includes_cluster(self):
        """Verify taxonomy_type includes 'cluster' option"""
        type_choices = [choice[0] for choice in ContentTaxonomy._meta.get_field('taxonomy_type').choices]
        assert 'category' in type_choices, "category should be a valid type"
        assert 'post_tag' in type_choices, "post_tag should be a valid type"
        assert 'cluster' in type_choices, "cluster should be a valid type"


class TestTasksSerializer(TestCase):
    """Test TasksSerializer after Stage 1 refactor"""
    
    def test_serializer_fields(self):
        """Verify serializer has correct fields"""
        serializer = TasksSerializer()
        fields = serializer.fields.keys()
        
        # Should have new fields
        assert 'content_type' in fields, "content_type should be in serializer"
        assert 'content_structure' in fields, "content_structure should be in serializer"
        assert 'taxonomy_term_id' in fields, "taxonomy_term_id should be in serializer"
        assert 'cluster_id' in fields, "cluster_id should be in serializer"
        
        # Should NOT have deprecated fields
        assert 'idea_title' not in fields, "idea_title should not be in serializer"
        assert 'cluster_role' not in fields, "cluster_role should not be in serializer"


class TestContentSerializer(TestCase):
    """Test ContentSerializer after Stage 1 refactor"""
    
    def test_serializer_fields(self):
        """Verify serializer has correct fields"""
        serializer = ContentSerializer()
        fields = serializer.fields.keys()
        
        # Should have new fields
        assert 'title' in fields, "title should be in serializer"
        assert 'content_html' in fields, "content_html should be in serializer"
        assert 'cluster_id' in fields, "cluster_id should be in serializer"
        assert 'content_type' in fields, "content_type should be in serializer"
        assert 'content_structure' in fields, "content_structure should be in serializer"
        assert 'taxonomy_terms_data' in fields, "taxonomy_terms_data should be in serializer"
        
        # Should NOT have deprecated fields
        assert 'task_id' not in fields, "task_id should not be in serializer"
        assert 'entity_type' not in fields, "entity_type should not be in serializer"
        assert 'cluster_role' not in fields, "cluster_role should not be in serializer"


class TestContentTaxonomySerializer(TestCase):
    """Test ContentTaxonomySerializer after Stage 1 refactor"""
    
    def test_serializer_fields(self):
        """Verify serializer has correct fields"""
        serializer = ContentTaxonomySerializer()
        fields = serializer.fields.keys()
        
        # Should have these fields
        assert 'id' in fields
        assert 'name' in fields
        assert 'slug' in fields
        assert 'taxonomy_type' in fields
        
        # Should NOT have deprecated fields
        assert 'description' not in fields, "description should not be in serializer"
        assert 'parent' not in fields, "parent should not be in serializer"
        assert 'sync_status' not in fields, "sync_status should not be in serializer"
        assert 'cluster_names' not in fields, "cluster_names should not be in serializer"


# Run tests with: python manage.py test igny8_core.modules.writer.tests.test_stage1_refactor
# Or with pytest: pytest backend/igny8_core/modules/writer/tests/test_stage1_refactor.py -v
