from datetime import datetime
from million.analyze.find_holes import compute_sequences, find_holes
from million.view.bar_chart import plot as bar_chart
from million.analyze.count_participations import count_participations
from million.analyze.retain_counts import retain_counts
from million.parse.fb_exports import FacebookExportParser


DATA_PATH = './data/'

parser = FacebookExportParser()

export = parser.parse(DATA_PATH)

filtered = retain_counts(export.messages)

sequences = compute_sequences(filtered)

actual_counted = sum([s.end - s.start for s in sequences])

print(f"Actual counted: {actual_counted}")

holes = find_holes(filtered)

print(len(holes))

for hole in holes:
    print(f"{hole.start} - {hole.end} ({hole.end - hole.start})")


# lets export a csv file of the holes and the people responsible for them
with open('holes.csv', 'w') as f:
    f.write('début,fin,taille,responsable1,responsable2,date1,date2\n')
    for hole in holes:
        date_start = datetime.utcfromtimestamp(
            hole.start_message.timestamp_ms / 1000.0).strftime('%Y-%m-%d %H:%M:%S')
        date_end = datetime.utcfromtimestamp(
            hole.end_message.timestamp_ms / 1000.0).strftime('%Y-%m-%d %H:%M:%S')
        f.write(
            f"{hole.start},"
            f"{hole.end},"
            f"{hole.end - hole.start},"
            f"{hole.start_message.sender_name},"
            f"{hole.end_message.sender_name},"
            f"{date_start},{date_end}\n"
        )
