#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Extended Tests for Language Detector

Additional test cases to improve coverage for language detection functionality.
"""

import sys
import pytest
import pytest_asyncio

# Add project root to path
sys.path.insert(0, ".")

from tree_sitter_analyzer.language_detector import (
    LanguageDetector,
    detect_language_from_file,
    detector,
    is_language_supported,
)


@pytest.fixture
def language_detector():
    """Fixture to provide a LanguageDetector instance"""
    return LanguageDetector()


def test_detect_language_with_content_java(language_detector):
    """Test language detection with Java content"""
    file_path = "test.java"
    content = """
    package com.example;
    
    public class TestClass {
        @Override
        public void method() {
            System.out.println("Hello");
        }
    }
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "java"
    assert confidence == 1.0


def test_detect_language_with_content_python(language_detector):
    """Test language detection with Python content"""
    file_path = "test.py"
    content = """
    def main():
        import os
        from sys import argv
        
        if __name__ == "__main__":
            print("Hello World")
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "python"
    assert confidence == 1.0


def test_detect_language_with_content_javascript(language_detector):
    """Test language detection with JavaScript content"""
    file_path = "test.js"
    content = """
    function greet(name) {
        var message = "Hello";
        let greeting = `${message}, ${name}!`;
        const result = greeting;
        console.log(result);
        return result;
    }
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "javascript"
    assert confidence == 1.0


def test_detect_language_with_content_typescript(language_detector):
    """Test language detection with TypeScript content"""
    file_path = "test.ts"
    content = """
    interface User {
        name: string;
        age: number;
    }
    
    type UserType = User;
    
    export class UserService {
        getUser(): User {
            return { name: "John", age: 30 };
        }
    }
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "typescript"
    assert confidence == 1.0


def test_detect_language_ambiguous_h_file_cpp(language_detector):
    """Test ambiguous .h file detection as C++"""
    file_path = "test.h"
    content = """
    #include <iostream>
    
    namespace MyNamespace {
        class MyClass {
        public:
            void method() {
                std::cout << "Hello" << std::endl;
            }
        };
        
        template<typename T>
        void templateFunction(T value) {
            std::cout << value << std::endl;
        }
    }
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "cpp"
    assert confidence == 0.9


def test_detect_language_ambiguous_h_file_c(language_detector):
    """Test ambiguous .h file detection as C"""
    file_path = "test.h"
    content = """
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
        int id;
        char name[50];
    } Person;
    
    int main() {
        printf("Hello, World!\\n");
        Person* p = malloc(sizeof(Person));
        return 0;
    }
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "c"
    assert confidence == 0.7


def test_detect_language_ambiguous_h_file_objc(language_detector):
    """Test ambiguous .h file detection as Objective-C"""
    file_path = "test.h"
    content = """
    #import <Foundation/Foundation.h>
    
    @interface MyClass : NSObject
    @property (nonatomic, strong) NSString *name;
    - (void)doSomething;
    @end
    
    @implementation MyClass
    - (void)doSomething {
        NSString *message = [[NSString alloc] initWithString:@"Hello"];
    }
    @end
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "objc"
    assert confidence == 0.9


def test_detect_language_ambiguous_m_file_objc(language_detector):
    """Test ambiguous .m file detection as Objective-C"""
    file_path = "test.m"
    content = """
    #import "MyClass.h"
    
    @implementation MyClass
    - (void)doSomething {
        NSString *message = [[NSString alloc] initWithString:@"Hello"];
    }
    @end
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "objc"
    assert confidence == 0.7


def test_detect_language_ambiguous_m_file_matlab(language_detector):
    """Test ambiguous .m file detection as MATLAB"""
    file_path = "test.m"
    content = """
    function result = calculateSum(a, b)
        clc;
        clear all;
        
        result = a + b;
        disp(['Result: ', num2str(result)]);
    end;
    
    % Main script
    x = 5;
    y = 10;
    sum_result = calculateSum(x, y);
    """

    language, confidence = language_detector.detect_language(file_path, content)

    assert language == "matlab"
    assert confidence == 0.9


def test_detect_language_ambiguous_without_content(language_detector):
    """Test ambiguous extension without content"""
    file_path = "test.h"

    language, confidence = language_detector.detect_language(file_path)

    assert language == "c"  # Default for .h
    assert confidence == 0.7


def test_detect_language_unknown_extension(language_detector):
    """Test unknown file extension"""
    file_path = "test.unknown"

    language, confidence = language_detector.detect_language(file_path)

    assert language == "unknown"
    assert confidence == 0.0


@pytest.mark.parametrize("file_path,expected_language", [
    ("test.java", "java"),
    ("test.py", "python"),
    ("test.js", "javascript"),
    ("test.ts", "typescript"),
    ("test.cpp", "cpp"),
    ("test.rs", "rust"),
    ("test.go", "go"),
    ("test.rb", "ruby"),
    ("test.php", "php"),
    ("test.swift", "swift"),
    ("test.kt", "kotlin"),
    ("test.scala", "scala"),
    ("test.unknown", "unknown"),
])
def test_detect_from_extension_various_files(language_detector, file_path, expected_language):
    """Test extension-based detection for various files"""
    language = language_detector.detect_from_extension(file_path)
    assert language == expected_language


@pytest.mark.parametrize("language", [
    "java",
    "javascript",
    "typescript",
    "python",
    "c",
    "cpp",
    "rust",
    "go",
])
def test_is_supported_supported_languages(language_detector, language):
    """Test support status for supported languages"""
    assert language_detector.is_supported(language) is True


@pytest.mark.parametrize("language", [
    "ruby",
    "php",
    "swift",
    "kotlin",
    "scala",
    "unknown"
])
def test_is_supported_unsupported_languages(language_detector, language):
    """Test support status for unsupported languages"""
    assert language_detector.is_supported(language) is False


def test_get_supported_extensions(language_detector):
    """Test getting supported extensions"""
    extensions = language_detector.get_supported_extensions()

    assert isinstance(extensions, list)
    assert ".java" in extensions
    assert ".py" in extensions
    assert ".js" in extensions
    assert ".ts" in extensions
    assert ".cpp" in extensions
    # Should be sorted
    assert extensions == sorted(extensions)


def test_get_supported_languages(language_detector):
    """Test getting supported languages"""
    languages = language_detector.get_supported_languages()

    assert isinstance(languages, list)
    assert "java" in languages
    assert "python" in languages
    assert "javascript" in languages
    assert "typescript" in languages
    assert "cpp" in languages
    # Should be sorted
    assert languages == sorted(languages)


def test_add_extension_mapping(language_detector):
    """Test adding custom extension mapping"""
    # Add a custom mapping
    language_detector.add_extension_mapping(".custom", "customlang")

    # Test the new mapping
    language = language_detector.detect_from_extension("test.custom")
    assert language == "customlang"

    # Test case insensitivity
    language_detector.add_extension_mapping(".UPPER", "upperlang")
    language = language_detector.detect_from_extension("test.upper")
    assert language == "upperlang"


def test_get_language_info_supported(language_detector):
    """Test getting language information for supported language"""
    # Test for Java
    info = language_detector.get_language_info("java")

    assert isinstance(info, dict)
    assert info["name"] == "java"
    assert ".java" in info["extensions"]
    assert info["supported"] is True
    assert info["tree_sitter_available"] is True


def test_get_language_info_unsupported(language_detector):
    """Test getting language information for unsupported language"""
    # Test for unsupported language
    info = language_detector.get_language_info("ruby")

    assert info["name"] == "ruby"
    assert ".rb" in info["extensions"]
    assert info["supported"] is False
    assert info["tree_sitter_available"] is False


def test_resolve_ambiguity_non_ambiguous(language_detector):
    """Test resolve ambiguity for non-ambiguous extension"""
    result = language_detector._resolve_ambiguity(".java", "public class Test {}")
    assert result == "java"


def test_resolve_ambiguity_unknown_extension(language_detector):
    """Test resolve ambiguity for unknown extension"""
    result = language_detector._resolve_ambiguity(".unknown", "some content")
    assert result == "unknown"


def test_detect_c_family_with_no_matches(language_detector):
    """Test C family detection with no pattern matches"""
    content = "// Just a comment\nint x = 5;"
    candidates = ["c", "cpp", "objc"]

    result = language_detector._detect_c_family(content, candidates)
    assert result == "c"  # Should return first candidate


def test_detect_objc_vs_matlab_tie(language_detector):
    """Test Objective-C vs MATLAB detection with tie"""
    content = "// No specific patterns"
    candidates = ["objc", "matlab"]

    result = language_detector._detect_objc_vs_matlab(content, candidates)
    assert result == "objc"  # Should return first candidate


def test_detect_c_family_objc_not_in_candidates(language_detector):
    """Test C family detection when objc wins but not in candidates"""
    content = "#import <Foundation/Foundation.h>\n@interface Test"
    candidates = ["c", "cpp"]  # objc not in candidates

    result = language_detector._detect_c_family(content, candidates)
    assert result in ["c", "cpp"]  # Should fall back to c or cpp


# Test global convenience functions
def test_detect_language_from_file():
    """Test global detect_language_from_file function"""
    language = detect_language_from_file("test.java")
    assert language == "java"

    language = detect_language_from_file("test.py")
    assert language == "python"

    language = detect_language_from_file("test.unknown")
    assert language == "unknown"


def test_is_language_supported_global():
    """Test global is_language_supported function"""
    assert is_language_supported("java") is True
    assert is_language_supported("python") is True
    assert is_language_supported("ruby") is False
    assert is_language_supported("unknown") is False


def test_global_detector_instance():
    """Test global detector instance"""
    assert isinstance(detector, LanguageDetector)

    # Test that it works
    language = detector.detect_from_extension("test.java")
    assert language == "java"


# Test edge cases and error conditions
def test_empty_file_path(language_detector):
    """Test empty file path"""
    language, confidence = language_detector.detect_language("")
    assert language == "unknown"
    assert confidence == 0.0


def test_file_path_without_extension(language_detector):
    """Test file path without extension"""
    language, confidence = language_detector.detect_language("README")
    assert language == "unknown"
    assert confidence == 0.0


def test_file_path_with_multiple_dots(language_detector):
    """Test file path with multiple dots"""
    language, confidence = language_detector.detect_language("test.backup.java")
    assert language == "java"
    assert confidence == 1.0


@pytest.mark.parametrize("file_path,expected_language", [
    ("test.JAVA", "java"),
    ("test.PY", "python"),
    ("test.JS", "javascript"),
    ("test.Cpp", "cpp"),
])
def test_case_insensitive_extensions(language_detector, file_path, expected_language):
    """Test case insensitive extension handling"""
    language = language_detector.detect_from_extension(file_path)
    assert language == expected_language


def test_empty_content_for_ambiguous_extension(language_detector):
    """Test empty content for ambiguous extension"""
    language, confidence = language_detector.detect_language("test.h", "")
    assert language == "c"
    assert confidence == 0.7


def test_none_content_for_ambiguous_extension(language_detector):
    """Test None content for ambiguous extension"""
    language, confidence = language_detector.detect_language("test.h", None)
    assert language == "c"
    assert confidence == 0.7
