"""Tests for the code scanner."""

import pytest
from pathlib import Path
from pr_guardian.scanner import CodeScanner, IssueType


def test_scanner_detects_hallucinated_imports():
    """Test that scanner detects hallucinated imports."""
    code = """
import pandas as pd
from sklearn.experimental import NewModel
import numpy as np
"""
    
    scanner = CodeScanner()
    temp_file = Path("/tmp/test.py")
    temp_file.write_text(code)
    
    result = scanner.scan_file(temp_file)
    temp_file.unlink()
    
    # Should detect the hallucinated import
    hallucination_issues = [i for i in result.issues if i.type == IssueType.HALLUCINATED_IMPORT]
    assert len(hallucination_issues) > 0
    assert result.ai_confidence_score > 0.5


def test_scanner_detects_suspicious_comments():
    """Test that scanner detects AI-generated comments."""
    code = """
def process_data(data):
    # TODO: implement actual logic here
    # TODO: add error handling
    return data
"""
    
    scanner = CodeScanner()
    temp_file = Path("/tmp/test.py")
    temp_file.write_text(code)
    
    result = scanner.scan_file(temp_file)
    temp_file.unlink()
    
    comment_issues = [i for i in result.issues if i.type == IssueType.SUSPICIOUS_COMMENT]
    assert len(comment_issues) >= 2


def test_scanner_clean_code():
    """Test that scanner gives low confidence for clean code."""
    code = """
def calculate_total_price(items, tax_rate):
    '''Calculate total price including tax.'''
    subtotal = sum(item.price for item in items)
    tax = subtotal * tax_rate
    return subtotal + tax
"""
    
    scanner = CodeScanner()
    temp_file = Path("/tmp/test.py")
    temp_file.write_text(code)
    
    result = scanner.scan_file(temp_file)
    temp_file.unlink()
    
    # Clean code should have low AI confidence
    assert result.ai_confidence_score < 0.5
    assert result.error_count == 0


def test_scanner_javascript():
    """Test scanner works with JavaScript."""
    code = """
import React from 'react';
import { futureAPI } from 'react-experimental';

// TODO: implement actual component
function MyComponent() {
  console.log('debugging');
  return <div>Hello</div>;
}
"""
    
    scanner = CodeScanner()
    temp_file = Path("/tmp/test.js")
    temp_file.write_text(code)
    
    result = scanner.scan_file(temp_file)
    temp_file.unlink()
    
    # Should detect hallucinated import and suspicious patterns
    assert len(result.issues) > 0
    assert result.ai_confidence_score > 0.3
