import os
import tempfile
import pytest
from datetime import datetime
from ayn_antivirus.core.engine import (
    ThreatType, Severity, ScanType, ThreatInfo,
    ScanResult, FileScanResult, ScanEngine
)
from ayn_antivirus.core.event_bus import EventBus, EventType

def test_threat_type_enum():
    assert ThreatType.VIRUS.value is not None
    assert ThreatType.MINER.value is not None

def test_severity_enum():
    assert Severity.CRITICAL.value is not None
    assert Severity.LOW.value is not None

def test_threat_info_creation():
    threat = ThreatInfo(
        path="/tmp/evil.sh",
        threat_name="TestMalware",
        threat_type=ThreatType.MALWARE,
        severity=Severity.HIGH,
        detector_name="test",
        details="Test detection",
        file_hash="abc123"
    )
    assert threat.path == "/tmp/evil.sh"
    assert threat.threat_type == ThreatType.MALWARE

def test_scan_result_creation():
    result = ScanResult(
        scan_id="test-123",
        start_time=datetime.now(),
        end_time=datetime.now(),
        files_scanned=100,
        files_skipped=5,
        threats=[],
        scan_path="/tmp",
        scan_type=ScanType.QUICK
    )
    assert result.files_scanned == 100
    assert len(result.threats) == 0

def test_event_bus():
    bus = EventBus()
    received = []
    bus.subscribe(EventType.THREAT_FOUND, lambda et, data: received.append(data))
    bus.publish(EventType.THREAT_FOUND, {"test": True})
    assert len(received) == 1
    assert received[0]["test"] == True

def test_scan_clean_file(tmp_path):
    clean_file = tmp_path / "clean.txt"
    clean_file.write_text("This is a perfectly normal text file with nothing suspicious.")
    from ayn_antivirus.config import Config
    config = Config()
    engine = ScanEngine(config)
    result = engine.scan_file(str(clean_file))
    assert isinstance(result, FileScanResult)
