import psycopg2

conn = psycopg2.connect(
    host="192.168.0.61",
    port=55432,
    database="njts_acct",
    user="njts_app",
    password="njts_app2025"
)

cur = conn.cursor()

# 恢復 ID 445（但我們已經刪除了，無法恢復）
# 讓我們重新檢查 ID 444 的情況

print("=" * 100)
print("ID 444 的完整信息")
print("=" * 100)

cur.execute("""
    SELECT 
        je.journal_entry_id,
        je.entry_date,
        je.description,
        je.revision_count,
        je.is_latest,
        je.is_deleted,
        je.original_entry_id,
        je.created_at,
        je.created_by
    FROM journal_entries je
    WHERE je.journal_entry_id = 444
""")

result = cur.fetchone()
if result:
    entry_id, entry_date, description, rev_count, is_latest, is_deleted, original_id, created_at, created_by = result
    print(f"ID: {entry_id}")
    print(f"日付: {entry_date}")
    print(f"摘要: {description}")
    print(f"版本: v{rev_count}")
    print(f"is_latest: {is_latest}")
    print(f"is_deleted: {is_deleted}")
    print(f"original_entry_id: {original_id}")
    print(f"created_at: {created_at}")
    print(f"created_by: {created_by}")

# 檢查ID 74（父級）
print("\n" + "=" * 100)
print("ID 74 的完整信息（ID 444の親）")
print("=" * 100)

cur.execute("""
    SELECT 
        je.journal_entry_id,
        je.entry_date,
        je.description,
        je.revision_count,
        je.is_latest,
        je.is_deleted,
        je.created_at
    FROM journal_entries je
    WHERE je.journal_entry_id = 74
""")

result2 = cur.fetchone()
if result2:
    entry_id, entry_date, description, rev_count, is_latest, is_deleted, created_at = result2
    print(f"ID: {entry_id}")
    print(f"日付: {entry_date}")
    print(f"摘要: {description}")
    print(f"版本: v{rev_count}")
    print(f"is_latest: {is_latest}")
    print(f"is_deleted: {is_deleted}")
    print(f"created_at: {created_at}")

# 删除 ID 444
print("\n" + "=" * 100)
print("ID 444 削除プロセス")
print("=" * 100)

# journal_lines を削除
cur.execute("SELECT COUNT(*) FROM journal_lines WHERE journal_entry_id = 444")
lines_count = cur.fetchone()[0]
print(f"削除対象行数: {lines_count}")

cur.execute("DELETE FROM journal_lines WHERE journal_entry_id = 444")
conn.commit()
print(f"✓ journal_lines 削除完了")

# journal_entries を削除
cur.execute("DELETE FROM journal_entries WHERE journal_entry_id = 444")
conn.commit()
print(f"✓ journal_entries 削除完了")

print("\n✅ ID 444 削除完了")

cur.close()
conn.close()
