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
fb_data_path = os.path.join(project_root, 'import_data', 'data', 'Facebook', 'posts', 'your_uncategorized_photos.json')

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

#%% In[ ]:
facebook_photos = photos_json['other_photos_v2']

#%% In[ ]:
facebook_photos_df = pd.DataFrame(facebook_photos)

#%% In[ ]:
# Filter out posts without a description
facebook_photos_df = facebook_photos_df[~facebook_photos_df['description'].isnull()]

#%% In[ ]:
facebook_photos_df['index'] = "rs_facebook_posts"
facebook_photos_df['network'] = "Facebook"
facebook_photos_df['type'] = "posts"
facebook_photos_df['chemin'] = fb_data_path

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

#%% In[ ]:
del facebook_photos_df['media_metadata']

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

#%% In[ ]:
documents_to_database(facebook_photos_df)
