#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
基本系統測試
"""

import os
import sys
sys.path.append('.')

def test_database():
    """測試資料庫連線"""
    try:
        import pymysql
        connection = pymysql.connect(
            host='mysql.theaken.com',
            port=33306,
            user='A060',
            password='WLeSCi0yhtc7',
            database='db_A060',
            charset='utf8mb4'
        )
        cursor = connection.cursor()
        
        # 檢查是否有翻譯系統的表
        cursor.execute('SHOW TABLES LIKE "dt_%"')
        tables = cursor.fetchall()
        
        print(f"Found {len(tables)} document translator tables")
        for table in tables:
            print(f"  - {table[0]}")
        
        connection.close()
        return True, len(tables)
        
    except Exception as e:
        print(f"Database test failed: {e}")
        return False, 0

def test_ldap():
    """測試LDAP連線"""
    try:
        import ldap3
        from ldap3 import Server, Connection, ALL
        
        server = Server('panjit.com.tw', port=389, get_info=ALL)
        
        # 使用服務帳號連線
        bind_dn = "CN=LdapBind,CN=Users,DC=PANJIT,DC=COM,DC=TW"
        bind_password = "panjit2481"
        
        conn = Connection(server, user=bind_dn, password=bind_password, auto_bind=True)
        
        # 搜尋測試使用者
        search_base = "OU=PANJIT,DC=panjit,DC=com,DC=tw"
        search_filter = "(userPrincipalName=ymirliu@panjit.com.tw)"
        
        result = conn.search(search_base, search_filter, attributes=['displayName', 'mail', 'department'])
        
        if result and conn.entries:
            user = conn.entries[0]
            print(f"Found test user: {user.displayName}")
            print(f"Email: {user.mail}")
            conn.unbind()
            return True
        else:
            print("Test user not found")
            conn.unbind()
            return False
            
    except Exception as e:
        print(f"LDAP test failed: {e}")
        return False

def test_file_processing():
    """測試檔案處理庫"""
    try:
        # 測試基本導入
        import docx
        import openpyxl
        from pptx import Presentation
        import PyPDF2
        
        print("All file processing libraries imported successfully")
        return True
        
    except Exception as e:
        print(f"File processing test failed: {e}")
        return False

def main():
    print("=== Document Translator System Test ===")
    
    print("\n1. Testing database connection...")
    db_ok, table_count = test_database()
    
    print("\n2. Testing LDAP authentication...")
    ldap_ok = test_ldap()
    
    print("\n3. Testing file processing libraries...")
    file_ok = test_file_processing()
    
    print("\n=== Test Results ===")
    print(f"Database Connection: {'PASS' if db_ok else 'FAIL'}")
    print(f"Database Tables: {table_count} found")
    print(f"LDAP Authentication: {'PASS' if ldap_ok else 'FAIL'}")
    print(f"File Processing: {'PASS' if file_ok else 'FAIL'}")
    
    if db_ok and ldap_ok and file_ok:
        if table_count > 0:
            print("\nStatus: READY for testing")
        else:
            print("\nStatus: Need to initialize database tables")
    else:
        print("\nStatus: System has issues")

if __name__ == '__main__':
    main()