"""
插入 system_functions 預設資料
"""
import psycopg2
import json
from datetime import datetime

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("Inserting system_functions default data...")

    # 預設資料
    data = [
        (10, 'system_managements', 0, '系統管理後台', 1, 100, '', '', [], '', True, True, 1),
        (11, 'system_settings', 10, '系統資料設定', 2, 10010, '', 'system_settings', ['View', 'Create', 'Read', 'Update', 'Delete'], '', True, True, 1),
        (12, 'system_codes', 10, '系統代碼設定', 2, 10020, '', 'system_codes', ['View', 'Create', 'Read', 'Update', 'Delete'], '', True, True, 1),
        (13, 'system_notifications', 10, '系統通知設定', 2, 10030, '', 'system_notifications', ['View', 'Create', 'Read', 'Update', 'Delete'], '', True, True, 1),
        (14, 'system_logs', 10, '系統紀錄查詢', 2, 10040, '', 'system_logs', ['View', 'Create', 'Read'], '', True, True, 1),
        (15, 'system_functions', 0, '系統功能設定', 2, 10050, '', 'system_functions', ['View', 'Create', 'Read', 'Update', 'Delete'], '', True, True, 1),
        (16, 'init_tenants', 0, '租戶初始資料建檔', 2, 10, '', 'init_tenants', ['View', 'Create', 'Read', 'Update', 'Delete'], '', True, True, 1),
        (17, 'tenant', 0, '公司資料維護', 2, 20, '', 'tenant', ['Read', 'Update'], '', False, True, 1),
        (18, 'tenant_departments', 0, '部門資料維護', 2, 30, '', 'tenant_departments', ['Create', 'Read', 'Update', 'Delete'], '', False, True, 1),
        (19, 'tenant_user_roles', 0, '角色設定作業', 2, 40, '', 'tenant_user_roles', ['Create', 'Read', 'Update', 'Delete'], '', False, True, 1),
        (20, 'tenant_role_rights', 0, '角色權限設定作業', 2, 50, '', 'tenant_role_rights', ['Create', 'Read', 'Update', 'Delete'], '', False, True, 1),
        (21, 'tenant_emp_resumes', 0, '人員履歷維護', 2, 60, '', 'tenant_emp_resumes', ['Create', 'Read', 'Update', 'Delete'], '', False, True, 1),
        (22, 'tenant_emp_settings', 0, '人員任用設定作業', 2, 70, '', 'tenant_emp_settings', ['Create', 'Read', 'Update', 'Delete'], '', False, True, 1),
    ]

    for row in data:
        (id, code, upper_function_id, name, function_type, order, function_icon, 
         module_code, module_functions, description, is_mana, is_active, edit_by) = row
        
        # 轉換 module_functions 為 JSON
        module_functions_json = json.dumps(module_functions)
        
        cur.execute("""
            INSERT INTO system_functions 
            (id, code, upper_function_id, name, function_type, "order", function_icon, 
             module_code, module_functions, description, is_mana, is_active, edit_by, created_at, updated_at)
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), NOW())
            ON CONFLICT (id) DO NOTHING;
        """, (id, code, upper_function_id, name, function_type, order, function_icon,
              module_code if module_code else None, module_functions_json, description, 
              is_mana, is_active, edit_by))

    conn.commit()
    
    # 驗證
    cur.execute("SELECT COUNT(*) FROM system_functions;")
    count = cur.fetchone()[0]
    print(f"[OK] Inserted {count} records into system_functions")

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