from .connection import get_db_cursor


def initialize_database():
    with get_db_cursor(commit=True) as cursor:
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS geom_params (
                id SERIAL PRIMARY KEY,
                count_blades_in_outer_contour int,
                width_blades_in_outer_contour numeric(5,2),
                angle_blades_in_outer_contour numeric(5,2),
                count_blades_in_middle_contour int,
                width_blades_in_middle_contour numeric(5,2),
                count_blades_in_fuel_contour int,
                width_blades_in_fuel_contour numeric(5,2),
                file_name varchar(105)
            );
        """)


def save_params_to_db(params, file_name):
    with get_db_cursor(commit=True) as cur:
        cur.execute("SELECT 1 FROM geom_params WHERE file_name = %s", (file_name,))
        if not cur.fetchone():
            cur.execute("""
                INSERT INTO geom_params (count_blades_in_outer_contour, width_blades_in_outer_contour,
                angle_blades_in_outer_contour, count_blades_in_middle_contour,
                width_blades_in_middle_contour, count_blades_in_fuel_contour, width_blades_in_fuel_contour, file_name)
                VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
                """, (
                params.get('N1'),
                params.get('L1'),
                params.get('a1'),
                params.get('N2'),
                params.get('L2'),
                params.get('N3'),
                params.get('L3'),
                file_name
            ))
