
import sqlite3
import json
import os

DB_PATH = "transcription-tool/backend/meetings.db"
MEETING_ID = 5

def debug_meeting(db_path, meeting_id):
    if not os.path.exists(db_path):
        print(f"ERROR: Database file not found at {db_path}")
        return

    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()

        # Get Meeting Info
        cursor.execute("SELECT id, title, status, duration_seconds FROM meetings WHERE id = ?", (meeting_id,))
        meeting = cursor.fetchone()

        if not meeting:
            print(f"ERROR: No meeting found with ID {meeting_id}")
            return

        print("--- MEETING INFO ---")
        print(f"ID: {meeting[0]}")
        print(f"Title: {meeting[1]}")
        print(f"Status: {meeting[2]}")
        print(f"Duration (s): {meeting[3]}")
        print("-" * 20)

        # Get Chunks
        cursor.execute("SELECT id, chunk_index, json_content FROM transcript_chunks WHERE meeting_id = ? ORDER BY chunk_index", (meeting_id,))
        chunks = cursor.fetchall()

        print(f"--- CHUNKS FOUND: {len(chunks)} ---")
        for chunk in chunks:
            chunk_id, chunk_index, json_content_str = chunk
            print(f"\n--- Chunk ID: {chunk_id}, Index: {chunk_index} ---")
            
            if not json_content_str:
                print("  -> JSON content is EMPTY.")
                continue

            try:
                json_content = json.loads(json_content_str)
                print(f"  -> Number of entries: {len(json_content)}")
                
                if json_content:
                    # Print first 2 and last 2 entries to check for the "Ja" loop
                    print("  -> First 2 entries:")
                    for entry in json_content[:2]:
                        print(f"    - {entry.get('display_time')} [{entry.get('speaker')}]: {entry.get('text')[:80]}...")
                    
                    if len(json_content) > 4:
                        print("  -> Last 2 entries:")
                        for entry in json_content[-2:]:
                            print(f"    - {entry.get('display_time')} [{entry.get('speaker')}]: {entry.get('text')[:80]}...")

            except json.JSONDecodeError:
                print("  -> ERROR: Failed to decode JSON content.")

    except sqlite3.Error as e:
        print(f"Database error: {e}")
    finally:
        if 'conn' in locals() and conn:
            conn.close()

if __name__ == "__main__":
    debug_meeting(DB_PATH, MEETING_ID)
