import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))

import pytest
from app.core.database import execute_query

def test_module_deactivation():
    """Ensure module deactivation preserves data integrity."""
    # Simulate module deactivation
    execute_query("UPDATE modules SET enabled = false WHERE name = 'sag';")

    # Check that data is preserved
    cases = execute_query("SELECT * FROM sag_sager;")
    relations = execute_query("SELECT * FROM sag_relationer;")
    tags = execute_query("SELECT * FROM sag_tags;")

    assert cases is not None, "Cases data should be preserved."
    assert relations is not None, "Relations data should be preserved."
    assert tags is not None, "Tags data should be preserved."

    # Check that soft-deleted data is still present
    soft_deleted_cases = execute_query("SELECT * FROM sag_sager WHERE deleted_at IS NOT NULL;")
    soft_deleted_relations = execute_query("SELECT * FROM sag_relationer WHERE deleted_at IS NOT NULL;")
    soft_deleted_tags = execute_query("SELECT * FROM sag_tags WHERE deleted_at IS NOT NULL;")

    assert soft_deleted_cases is not None, "Soft-deleted cases should be preserved."
    assert soft_deleted_relations is not None, "Soft-deleted relations should be preserved."
    assert soft_deleted_tags is not None, "Soft-deleted tags should be preserved."