import sys
from pathlib import Path

# 프로젝트 루트와 app 디렉토리를 path에 추가
current_dir = Path(__file__).parent
project_root = current_dir.parent
app_dir = project_root / "app"
sys.path.append(str(app_dir))

from services.timetable_service import TimetableService
import json

def test_timetable():
    print("=== Timetable Service Test ===")
    
    # 서비스 초기화
    service = TimetableService()
    
    if not service.is_loaded:
        print("Failed to load timetable. Check the file path.")
        return

    # 1. 특정 열차 번호로 검색 (예: 1017)
    print("\n[1] Search by Train Number (1017):")
    results = service.search_train(train_number=1017)
    if results:
        print(f"Found {len(results)} rows for train 1017")
        # 첫 3개 행 출력
        for r in results[:3]:
            print(f"  {r['diagram_type']} | {r['train_number']} | {r['station']} | {r['time']} | {r['direction']}")
    else:
        print("No results found for train 1017")

    # 2. 역과 시간으로 열차 찾기
    # 샘플 데이터에 노포 06:32:30 가 있으므로 이를 기준으로 테스트
    print("\n[2] Find closest train at station '노포', time '06:30', direction '상선':")
    match = service.find_train_by_time(station="노포", direction="상선", time_str="06:30", diagram_type="holiday")
    if match:
        print("Found matching train:")
        print(f"  Train: {match['train_number']}")
        print(f"  Time: {match['time']}")
        print(f"  Station: {match['station']}")
        print(f"  Diagram: {match['diagram_type']}")
    else:
        print("No matching train found.")

    # 3. 특정 역 정차 열차 목록
    print("\n[3] Search trains at '남산' station in holiday:")
    results = service.search_train(station="남산", diagram_type="holiday")
    print(f"Total matching rows: {len(results)}")
    for r in results[:5]:
        print(f"  {r['train_number']} | {r['time']} | {r['direction']}")

if __name__ == "__main__":
    test_timetable()
