"""
測試所有觸發器功能
"""
import psycopg2

conn = psycopg2.connect(
    host="10.1.0.20",
    port=5433,
    database="hr_portal",
    user="admin",
    password="DC1qaz2wsx"
)
conn.autocommit = False
cur = conn.cursor()

try:
    print("=" * 60)
    print("Test All Triggers")
    print("=" * 60)

    # ========================================
    # Test 1: trigger_tenant_departments_seq_no
    # ========================================
    print("\n[Test 1] trigger_tenant_departments_seq_no")
    
    cur.execute("SELECT COALESCE(MAX(seq_no), 0) FROM tenant_departments WHERE tenant_id = 1;")
    max_seq = cur.fetchone()[0]
    print(f"   Current max seq_no: {max_seq}")
    
    cur.execute("""
        INSERT INTO tenant_departments (tenant_id, code, name, depth)
        VALUES (1, 'TEST_DEPT', 'Test Department', 0)
        RETURNING seq_no;
    """)
    new_seq = cur.fetchone()[0]
    print(f"   New seq_no (auto): {new_seq}")
    print(f"   [PASS] {new_seq} == {max_seq + 1}")
    
    conn.rollback()

    # ========================================
    # Test 2: trigger_tenant_user_roles_seq_no
    # ========================================
    print("\n[Test 2] trigger_tenant_user_roles_seq_no")
    
    cur.execute("SELECT COALESCE(MAX(seq_no), 0) FROM tenant_user_roles WHERE tenant_id = 1;")
    max_seq = cur.fetchone()[0]
    print(f"   Current max seq_no: {max_seq}")
    
    cur.execute("""
        INSERT INTO tenant_user_roles (tenant_id, role_code, role_name)
        VALUES (1, 'TEST_ROLE', 'Test Role')
        RETURNING seq_no;
    """)
    new_seq = cur.fetchone()[0]
    print(f"   New seq_no (auto): {new_seq}")
    print(f"   [PASS] {new_seq} == {max_seq + 1}")
    
    conn.rollback()

    # ========================================
    # Test 3: trigger_tenant_emp_resumes_seq_no
    # ========================================
    print("\n[Test 3] trigger_tenant_emp_resumes_seq_no")
    
    cur.execute("SELECT COALESCE(MAX(seq_no), 0) FROM tenant_emp_resumes WHERE tenant_id = 1;")
    max_seq = cur.fetchone()[0]
    print(f"   Current max seq_no: {max_seq}")
    
    cur.execute("""
        INSERT INTO tenant_emp_resumes (tenant_id, legal_name, id_number)
        VALUES (1, 'Test User', 'A123456789')
        RETURNING id, seq_no;
    """)
    result = cur.fetchone()
    resume_id = result[0]
    new_seq = result[1]
    print(f"   New seq_no (auto): {new_seq}")
    print(f"   [PASS] {new_seq} == {max_seq + 1}")
    
    # ========================================
    # Test 4&5: trigger_tenant_emp_settings_seq_no + trigger_generate_emp_code
    # ========================================
    print("\n[Test 4&5] auto seq_no + auto emp_code")
    
    cur.execute("SELECT COALESCE(MAX(seq_no), 0) FROM tenant_emp_settings WHERE tenant_id = 1;")
    max_seq = cur.fetchone()[0]
    print(f"   Current max seq_no: {max_seq}")
    
    cur.execute("SELECT prefix FROM tenants WHERE id = 1;")
    prefix = cur.fetchone()[0]
    print(f"   Tenant prefix: {prefix}")
    
    cur.execute("""
        INSERT INTO tenant_emp_settings (tenant_id, tenant_resume_id, hire_at)
        VALUES (1, %s, CURRENT_DATE)
        RETURNING seq_no, tenant_emp_code;
    """, (resume_id,))
    result = cur.fetchone()
    new_seq = result[0]
    emp_code = result[1]
    expected_code = f"{prefix}{str(new_seq).zfill(4)}"
    
    print(f"   New seq_no (auto): {new_seq}")
    print(f"   New emp_code (auto): {emp_code}")
    print(f"   Expected: {expected_code}")
    print(f"   [PASS] seq_no: {new_seq} == {max_seq + 1}")
    print(f"   [PASS] emp_code: {emp_code} == {expected_code}")
    
    conn.rollback()

    print("\n" + "=" * 60)
    print("ALL TESTS PASSED!")
    print("=" * 60)

except Exception as e:
    conn.rollback()
    print(f"\nERROR: {e}")
    import traceback
    traceback.print_exc()
finally:
    cur.close()
    conn.close()
