import datetime

import pandas as pd
import json

from utils.get_ids import get_idtypedocument, get_idreseausocial
from utils.documents_to_database import documents_to_database
from utils.convert_encoding_meta import convert_encoding_meta

# In[ ]:
instagram_data_path = 'data/Instagram/content/reels.json'
with open(instagram_data_path, "r", encoding="raw-unicode-escape") as posts:
    reels_json = json.loads(convert_encoding_meta(posts.read()))

# In[ ]:
ig_reels_media = [x['media'][0] for x in reels_json['ig_reels_media']]

# In[ ]:
ig_reels_df = pd.DataFrame(ig_reels_media)

# In[ ]:
ig_reels_df['datepublication'] = ig_reels_df['creation_timestamp'].apply(
    lambda x: datetime.datetime.fromtimestamp(x).isoformat())
ig_reels_df['index'] = "rs_instagram_content"
ig_reels_df['type'] = "reels"
ig_reels_df['network'] = "Instagram"
ig_reels_df['chemin'] = instagram_data_path

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

# In[ ]:
del ig_reels_df['creation_timestamp']
del ig_reels_df['media_metadata']
del ig_reels_df['cross_post_source']
del ig_reels_df['dubbing_info']

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

# In[ ]:
ig_reels_df.drop_duplicates(subset=['texte', 'datepublication'], inplace=True)

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

# In[ ]:
documents_to_database(ig_reels_df)
