import pandas as pd
import datetime

import os
from pathlib import Path

from utils.documents_to_database import documents_to_database

#%% 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
linkedin_data_path = os.path.join(project_root, 'import_data', 'data', 'LinkedIn', 'shares', 'Shares.csv')

raw_shares = pd.read_csv(linkedin_data_path)

#%% In[ ]:
raw_shares['index'] = "rs_linkedin_shares"
raw_shares['type'] = "posts"
raw_shares['network'] = "LinkedIn"
raw_shares['chemin'] = linkedin_data_path

#%% In[ ]:
raw_shares["creation_timestamp"] = raw_shares["Date"].apply(
    lambda x: int(datetime.datetime.fromisoformat(x).timestamp())
)
del raw_shares["Date"]

#%% In[ ]:
raw_shares.rename(columns={"ShareLink": "uri", "ShareCommentary": "texte"}, inplace=True)

#%% In[ ]:
raw_shares["texte"] = raw_shares["texte"].apply(lambda x: str(x))

#%% In[ ]:
del raw_shares["SharedUrl"]
del raw_shares["MediaUrl"]
del raw_shares["Visibility"]

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

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

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

#%% In[ ]:
documents_to_database(raw_shares)

