import pandas as pd
import json
import os
from pathlib import Path

from utils.documents_to_database import documents_to_database
from utils.convert_encoding_meta import convert_encoding_meta

#%% In[ ]:
# Get the current file's directory
try:
    # This will work when running as a script
    script_dir = Path(__file__).parent.parent
except NameError:
    # This will work in interactive environments
    script_dir = Path().absolute()

project_root = script_dir
instagram_data_path = os.path.join(project_root, 'import_data', 'data', 'Instagram', 'content', 'posts_1.json')


with open(instagram_data_path, "r", encoding="raw-unicode-escape") as posts:
    posts_json = json.loads(convert_encoding_meta(posts.read()))

#%% In[ ]:
posts_medias = []
for post in posts_json:
    medias = post['media']
    # S'il y a un seul média
    if len(medias) == 1:
        media = medias[0]
        posts_medias.append({
            "uri": [media["uri"]],
            "chemin": instagram_data_path,
            "index": "rs_instagram_content",
            "type": "content",
            "network": "Instagram",
            "texte": media["title"],
            "creation_timestamp": media["creation_timestamp"]})
    else:
        # S'il y a plusieurs médias,
        # on va itérer sur chacun des médias
        # pour récupérer les URI
        title = post['title']
        creation_timestamp = post['creation_timestamp']
        list_uris = []
        for media in medias:
            uri = media['uri']
            list_uris.append(uri)
        posts_medias.append({
            "uri": list_uris,
            "chemin": instagram_data_path,
            "index": "rs_instagram_content",
            "type": "posts",
            "network": "Instagram",
            "texte": title,
            "creation_timestamp": creation_timestamp})

#%% In[ ]:
posts_medias_df = pd.DataFrame(posts_medias)

#%% In[ ]:
posts_medias_df.fillna(value="", inplace=True)

#%% In[ ]:
posts_medias_df.drop_duplicates(subset=['texte', 'creation_timestamp'], inplace=True)

#%% In[ ]:
# Filter empty texte
posts_medias_df = posts_medias_df[~posts_medias_df['texte'].str.strip().eq('')]

#%% In[ ]:
documents_to_database(posts_medias_df)
