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', 'stories.json')
with open(instagram_data_path, "r", encoding="raw-unicode-escape") as posts:
    stories_json = json.loads(convert_encoding_meta(posts.read()))

#%% In[ ]:
ig_stories_df = pd.DataFrame(stories_json['ig_stories'])

#%% In[ ]:
ig_stories_df['index'] = "rs_instagram_content"
ig_stories_df['type'] = "stories"
ig_stories_df['network'] = "Instagram"
ig_stories_df['chemin'] = instagram_data_path

#%% In[ ]:
ig_stories_df.rename(columns={"title": "texte"}, inplace=True)

#%% In[ ]:
del ig_stories_df['media_metadata']
del ig_stories_df['cross_post_source']
del ig_stories_df['ai_stickers']
del ig_stories_df['dubbing_info']

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

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

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

#%% In[ ]:
documents_to_database(ig_stories_df)
