from utils.pgDb import pg_pool
import json

def insert_sca_video(
    name,
    raw_object_name,
    ai_object_name,
    duration,
    size,
    video_codec,
    audio_codec,
    overall_bit_rate,
    resolution,
    sc_analysis_time,
    sc_analysis_total_count,
    sc_analysis_max_count,
    sc_analysis_primary_type,
    sc_analysis_secondary_type,
    other_info,
):
    with pg_pool.getConn() as conn:
        with conn.cursor() as cursor:
            other_info = json.dumps(other_info, ensure_ascii=False)
            cursor.execute(
                """
                INSERT INTO sca_videos (
                    name, raw_object_name, ai_object_name, duration, size, video_codec, audio_codec, 
                    overall_bit_rate, resolution, sc_analysis_time, sc_analysis_total_count, sc_analysis_max_count, 
                    sc_analysis_primary_type, sc_analysis_secondary_type, other_info, created_at 
                )
                VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())
                RETURNING id
                """,
                (
                    name,
                    raw_object_name,
                    ai_object_name,
                    duration,
                    size,
                    video_codec,
                    audio_codec,
                    overall_bit_rate,
                    resolution,
                    sc_analysis_time,
                    sc_analysis_total_count,
                    sc_analysis_max_count,
                    sc_analysis_primary_type,
                    sc_analysis_secondary_type,
                    other_info,
                ),
            )

            new_id = cursor.fetchone()[0]
            conn.commit()
            return new_id

def insert_sca_video_details(
    v_id,
    time_stamp,
    other_info,
):
    with pg_pool.getConn() as conn:
        with conn.cursor() as cursor:
            other_info = json.dumps(other_info, ensure_ascii=False)
            cursor.execute(
                """
                INSERT INTO sca_video_details (
                    v_id, time_stamp, other_info 
                )
                VALUES (%s, %s, %s)
                """,
                (
                    v_id,
                    time_stamp,
                    other_info,
                ),
            )
            conn.commit()
