import os

from services.content_evaluator import ContentEvaluator
from services.instagram.public_instagram_scraper import PublicInstagramScraper

scraper = PublicInstagramScraper()

profile, status = scraper.get_user_profile("xplenture")

# POSTS
# posts = scraper.get_posts(user_id=profile["pk"])

# print(posts)

# search_result = scraper.search_posts_for_tag(posts, "dronepals")
# print("Likes: ", search_result["content"]["like_count"])
# print("Comments: ", search_result["content"]["comment_count"])
# print(search_result)
# downloaded_post_path = scraper.download_post_or_reel(search_result["content"]["image_versions"][0]["url"])

# os.system(f"open {downloaded_post_path}")

# # REELS
reels = scraper.get_reels(user_id=profile["pk"])
print(reels)
view_count = reels[0]["play_count"]
like_count = reels[0]["like_count"]
comment_count = reels[0]["comment_count"]
print("Views: ", view_count)
print("Likes: ", like_count)
print("Comments: ", comment_count)

search_result = scraper.search_reels_for_tag(reels, "visitutah")
print(search_result)

pk = search_result["content"]["pk"]

# try fetching the reel from the pk
reel = scraper.fetch_media_by_id(pk)
print(reel)

path_to_video = scraper.download_post_or_reel(search_result["content"]["video_url"])

# # open file locally
os.system(f"open {path_to_video}")


# assert search_result["content_found"] == True

# post = search_result["content"]
# scan + grade image
# score1 = ContentEvaluator.score_content(post["thumbnail_url"])["score"] # get grade with no required tag in image, no additional requirements
# score2 = ContentEvaluator.score_content(post["thumbnail_url"], requirements=["Post an aerial view of your winter wonderland!"])["score"] # get a grade with matching requirements
# score3 = ContentEvaluator.score_content(post["thumbnail_url"], requirements=["Post a picture of yourself enjoying a taco!"])["score"] # get a grade with an unrealistic requirement (should fail)
# score4 = ContentEvaluator.score_content(post["thumbnail_url"], required_tag="notactuallythere")["score"] # get grade with tag that doesn't exist in the photo (should return -1)

# # print("Score 1: " + str(score1))
# # print("Score 2: " + str(score2))
# print("Score 3: " + str(score3))
# print("Score 4: " + str(score4))

# assert score1 >= 70
# assert score2 >= 70
# assert score3 == -2
# assert score4 == -1

# # stories (NOTE: requires a story to be posted within the past 24 hours)
# stories = scraper.get_stories(user_id=profile["pk"])
# search_result = scraper.search_stories_for_tag(stories, "visitutah")
# print(search_result)

# downloaded_story_path = scraper.download_story(search_result["id"])

# assert search_result["content_found"] == True

# story = search_result["content"]
# # scan + grade image
# eval = ContentEvaluator.score_content(story["thumbnail_url"]) # get grade with no required tag in image, no additional requirements

# print(eval)

# assert isinstance(eval["score"], int)
