import sqlite3
import os
import pandas as pd
from tqdm import tqdm

# Nombre del archivo de la base de datos
db_name = "xala_dev.db"

# Verificar si la base de datos ya existe
if os.path.exists(db_name):
    os.remove(db_name)
    print(f"Se elimina la base de datos pre existente.")


# Conectar a SQLite (si no existe, se crea automáticamente)
conn = sqlite3.connect(db_name)
cursor = conn.cursor()

# ######################################################
#         [1/5] CREAR LA TABLA "home"
# ######################################################
cursor.execute('''
CREATE TABLE home (
  pk TEXT PRIMARY KEY,
  label TEXT,
  icon TEXT,
  description TEXT,
  svg_data TEXT
);
''')

xlsx_path = lambda x: f'Files/xlsx_n_xlsx/{x}'
html_path = lambda x: f'Files/html_template/{x}'
svg_path = lambda x: f'Files/svg/{x}'

f_home = xlsx_path("a_home.xlsx")

df_home = pd.read_excel(f_home)
df_home = pd.DataFrame(df_home)
lst_home = []

for row in tqdm(range(df_home.shape[0])):
  pk = df_home['pk'].iloc[row]
  label = df_home['software'].iloc[row]
  icon = df_home['icon'].iloc[row]
  description = df_home['short_desc'].iloc[row]
  with open(f"{svg_path(df_home['svg_file'].iloc[row])}", "r", encoding="utf-8") as f:
     svg_data = f.read()

  lst_home.append(tuple([pk, label, icon, description, svg_data]))

cursor.executemany('INSERT INTO home (pk, label, icon, description, svg_data) VALUES (?, ?, ?, ?, ?)', lst_home)
del df_home
print("✔️ home")

# ######################################################
#         [2/5] CREAR LA TABLA "all_posts"
# ######################################################
cursor.execute('''
CREATE TABLE all_posts (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  pk TEXT,
  date TEXT,
  status TEXT,
  titulo_tema TEXT,
  sintesis TEXT,
  html_alert TEXT,
  lst_glosary TEXT,
  lst_post_related TEXT
);
''')

f_all_posts = xlsx_path("b_all_posts.xlsx")
df_all_posts = pd.read_excel(f_all_posts)
df_all_posts = pd.DataFrame(df_all_posts)
lst_all_posts = []

for row in tqdm(range(df_all_posts.shape[0])):
    id = int(df_all_posts['index'].iloc[row])
    pk = df_all_posts['pk'].iloc[row]
    date = str(df_all_posts['date'].iloc[row])
    status = df_all_posts['status'].iloc[row]
    titulo_tema = df_all_posts['titulo_tema'].iloc[row]
    sintesis = df_all_posts['sintesis'].iloc[row]
    # file_html_template = f'{str(df_all_posts["index"].iloc[row])}.html'
    # with open(f"{html_path(file_html_template)}", mode='r', encoding='utf-8') as html_temp:
    #    html_template = html_temp.read()      

    html_alert = f'{str(df_all_posts["html_alert"].iloc[row])}'
    lst_glosary = str(df_all_posts['lst_glosary'].iloc[row])  # Convertir la lista a una representación de cadena
    lst_post_related = df_all_posts['lst_post_related'].iloc[row] # Convertir la lista a una representación de cadena

    lst_all_posts.append(tuple([id, pk, date, status, titulo_tema, sintesis, html_alert, lst_glosary, lst_post_related]))

cursor.executemany('''
INSERT INTO all_posts (id, pk, date, status, titulo_tema, sintesis, html_alert, lst_glosary, lst_post_related) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', lst_all_posts)

del df_all_posts
print("✔️ All_Posts")

# ######################################################
#         [3/5] CREAR LA TABLA DE GLOSARIO.
# ######################################################

cursor.execute('''
CREATE TABLE glosary (
  pk TEXT,
  word TEXT,
  desc_html TEXT
);
''')

f_glosary = xlsx_path("d_glorary.xlsx")
df_glosary = pd.read_excel(f_glosary)
df_glosary = pd.DataFrame(df_glosary)
lst_glosary = []

for row in tqdm(range(df_glosary.shape[0])):
  pk = df_glosary['id_key_word'].iloc[row]
  word = df_glosary['word'].iloc[row]
  html_template = str(df_glosary['desc_html'].iloc[row])

  lst_glosary.append(tuple([pk, word, html_template]))

cursor.executemany('INSERT INTO glosary (pk, word, desc_html) VALUES (?, ?, ?)', lst_glosary)
del df_glosary
print("✔️ glosary")


# ######################################################
#         [4/5] CREAR LA TABLA DE about_me
# ######################################################
cursor.execute(
'''
CREATE TABLE about_me (
  about_me_html TEXT,
  lst_glosary TEXT
);
'''
)

f_about_me = xlsx_path("c_about_me.xlsx")
df_about_me = pd.read_excel(f_about_me)
df_about_me = pd.DataFrame(df_about_me)
lst_about_me = []

for index, row in tqdm(df_about_me.iterrows(), total=df_about_me.shape[0]):
  about_me_html = str(row['about_me_html'])
  lst_glosary = row['lst_glosary']
  lst_about_me.append(tuple([about_me_html,lst_glosary]))
  
cursor.executemany("INSERT INTO about_me (about_me_html, lst_glosary) VALUES (?, ?);", lst_about_me)

del df_about_me
print("✔️ about_me")

# ######################################################
#         [5/5] CREAR TABLA DE EXTENSIONES
# ######################################################
cursor.execute(
  '''
  CREATE TABLE exts (
    pk TEXT,
    ext TEXT,
    name TEXT
  )
  '''
)

f_exts = xlsx_path("e_ext.xlsx")
df_exts = pd.read_excel(f_exts)
df_exts = pd.DataFrame(df_exts)
lst_exts = []

for row in tqdm(range(df_exts.shape[0])):
  pk = df_exts['pk'].iloc[row]
  ext = df_exts['ext'].iloc[row]
  name = str(df_exts['name'].iloc[row])

  lst_exts.append(tuple([pk, ext, name]))

cursor.executemany("INSERT INTO exts (pk, ext, name) VALUES (?, ?, ?);", lst_exts)

del df_exts
print("✔️ ext")

# ######################################################
# Confirmar cambios y cerrar conexión
# ######################################################
conn.commit()
conn.close()

print(f"DB '{db_name}' creada exitosamente.")
