"""
Complete workflow test for workspace SID conflict detection with automatic refresh.

This demonstrates the full LSP workflow:
1. User opens a workspace folder
2. Language server analyzes all .rules files
3. User opens a file in the editor
4. Language server automatically refreshes diagnostics showing SID conflicts
5. User adds/removes workspace folders
6. Language server automatically updates diagnostics

Copyright(C) 2026 Stamus Networks SAS
"""

# pylint: disable=W0212  # Allow testing protected methods

import sys
import os

# Add src to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))


def main():
    """Demonstrate complete workspace SID conflict detection workflow."""
    from unittest.mock import Mock
    from suricatals.langserver import LangServer, path_from_uri
    from suricatals.signature_parser import SuricataFile
    from suricatals.signature_validator import SignaturesTester

    print("\n" + "=" * 80)
    print("COMPLETE WORKFLOW: Workspace SID Conflict Detection with Auto-Refresh")
    print("=" * 80)

    # Setup
    workspace_dir = os.path.join(
        os.path.dirname(__file__), "tests", "workspace_conflict_test"
    )
    file1_path = os.path.join(workspace_dir, "emerging-threats.rules")
    file2_path = os.path.join(workspace_dir, "local-custom.rules")

    # Phase 1: Initialize language server
    print("\n" + "-" * 80)
    print("PHASE 1: Initialize Language Server")
    print("-" * 80)

    lang_server = LangServer(None)
    lang_server.rules_tester = SignaturesTester()
    lang_server.server = Mock()
    lang_server.server.workspace = Mock()

    print("✓ Language server initialized")

    # Phase 2: User opens workspace folder
    print("\n" + "-" * 80)
    print("PHASE 2: User Opens Workspace Folder")
    print("-" * 80)

    lang_server.source_dirs.append(workspace_dir)
    print(f"✓ Workspace folder added: {workspace_dir}")

    # Simulate workspace analysis
    print("  Analyzing workspace files...")
    rules_tester = SignaturesTester()

    for filepath in [file1_path, file2_path]:
        s_file = SuricataFile(filepath, rules_tester)
        s_file.load_from_disk()
        s_file.parse_file()

        mpm_data = {"buffer": {}, "sids": {}}
        for sig in s_file.sigset.signatures:
            if sig.sid != 0:
                mpm_data["sids"][sig.sid] = {}

        lang_server.workspace_mpm.add_file(filepath, mpm_data)

    stats = lang_server.workspace_mpm.get_statistics()
    print(
        f"✓ Workspace analyzed: {stats['file_count']} files, {stats['total_sids']} SIDs"
    )

    # Phase 3: User opens file in editor
    print("\n" + "-" * 80)
    print("PHASE 3: User Opens File in Editor")
    print("-" * 80)

    file2_uri = f"file://{file2_path}"
    mock_text_doc = Mock()
    mock_text_doc.uri = file2_uri
    with open(file2_path, "r", encoding="utf-8") as f:
        mock_text_doc.source = f.read()
    mock_text_doc.lines = mock_text_doc.source.split("\n")

    lang_server.server.workspace.text_documents = {file2_uri: mock_text_doc}
    lang_server.server.workspace.get_text_document = lambda uri: (
        mock_text_doc if uri == file2_uri else None
    )

    print(f"✓ File opened: {os.path.basename(file2_path)}")

    # Phase 4: Check diagnostics (initial)
    print("\n" + "-" * 80)
    print("PHASE 4: Initial Diagnostic Check")
    print("-" * 80)

    diag_results, _ = lang_server.get_diagnostics(file2_uri)

    if diag_results:
        conflict_count = sum(1 for d in diag_results if "conflict" in d.message.lower())
        print(f"✓ Initial diagnostics: {len(diag_results)} total")
        print(f"  - SID conflicts: {conflict_count}")

        if conflict_count > 0:
            print("  Detected conflicts:")
            for diag in diag_results:
                if "conflict" in diag.message.lower():
                    print(f"    • Line {diag.range.start.line + 1}: {diag.message}")

    # Phase 5: Simulate workspace update (auto-refresh)
    print("\n" + "-" * 80)
    print("PHASE 5: Automatic Diagnostic Refresh on Workspace Update")
    print("-" * 80)

    published_diagnostics = []

    def mock_publish(params):
        published_diagnostics.append(params)
        print(
            f"  → Diagnostics republished for {os.path.basename(path_from_uri(params.uri))}"
        )
        conflict_count = sum(
            1 for d in params.diagnostics if "conflict" in d.message.lower()
        )
        if conflict_count > 0:
            print(f"    ✓ {conflict_count} SID conflict(s) included")

    lang_server.server.text_document_publish_diagnostics = mock_publish

    print("Triggering automatic refresh...")
    lang_server._refresh_open_file_diagnostics()

    if len(published_diagnostics) > 0:
        print(
            f"✓ {len(published_diagnostics)} file(s) had diagnostics automatically refreshed"
        )
    else:
        print("✗ No diagnostics were refreshed")

    # Phase 6: Summary
    print("\n" + "=" * 80)
    print("WORKFLOW SUMMARY")
    print("=" * 80)

    total_conflicts = sum(
        sum(1 for d in pub.diagnostics if "conflict" in d.message.lower())
        for pub in published_diagnostics
    )

    print(
        f"""
Workspace folders analyzed:    1
Total SIDs tracked:            {stats['total_sids']}
Files opened in editor:        1
Diagnostics auto-refreshed:    {len(published_diagnostics)}
SID conflicts detected:        {total_conflicts}

Expected conflicts:            2 (SID 1000001 and 2025002)
    """
    )

    if total_conflicts >= 2:
        print("✅ WORKFLOW TEST PASSED!")
        print("\nKey Features Demonstrated:")
        print("  ✓ Workspace analysis with parallel processing")
        print("  ✓ SID conflict detection across multiple files")
        print("  ✓ Automatic diagnostic refresh for open files")
        print("  ✓ Real-time conflict updates when workspace changes")
        print("=" * 80 + "\n")
        assert True, "Workflow test passed with expected SID conflicts detected"
    else:
        print("❌ WORKFLOW TEST FAILED: Expected at least 2 SID conflicts")
        print("=" * 80 + "\n")
        assert False, "Workflow test failed: Expected at least 2 SID conflicts detected"
