<?php

namespace App\Helpers\FirstParty\Maintenance;

use App\Helpers\FirstParty\AI\OpenAI;
use App\Helpers\FirstParty\Meme\MemeGenerator;
use App\Models\Category;
use App\Models\MemeMedia;
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;

class MemeMediaMaintenance
{
    public static function generateMemesByCategories()
    {
        Category::where('system_memes_generated_count', '<', 1)
            ->chunk(10, function ($categories) {
                foreach ($categories as $category) {
                    dump('Processing '.$category->name);

                    $meme = MemeGenerator::generateMemeByCategory($category);

                    if (! is_null($meme)) {
                        $category->system_memes_generated_count++;
                        $category->save();
                    }
                }
            });
    }

    public static function patchMemeKeywords()
    {
        $meme_medias = MemeMedia::whereNull('action_keywords')->get();

        foreach ($meme_medias as $key => $meme_media) {

            dump('Processing '.$key + 1 .'/'.$meme_medias->count().': '.$meme_media->name);

            $meme_keywords_response = OpenAI::getMemeKeywords($meme_media->name, $meme_media->description);

            $meme_keywords_output = json_decode(OpenAI::getOpenAIOutput($meme_keywords_response));

            $meme_media->description = $meme_keywords_output->description;
            $meme_media->action_keywords = $meme_keywords_output->action_keywords;
            $meme_media->emotion_keywords = $meme_keywords_output->emotion_keywords;
            $meme_media->misc_keywords = $meme_keywords_output->misc_keywords;

            $meme_media->save();
        }
    }

    public static function addMemeKeywordsToTags()
    {
        $meme_medias = MemeMedia::all();

        foreach ($meme_medias as $key => $meme_media) {
            $meme_media->attachTags($meme_media->action_keywords, 'meme_media_action');
            $meme_media->attachTags($meme_media->emotion_keywords, 'meme_media_emotion');
            $meme_media->attachTags($meme_media->misc_keywords, 'meme_media_misc');
        }
    }

    public static function populateDurations()
    {

        $meme_medias_with_no_duration = self::getMemeMediasWithNoDuration();

        $total = $meme_medias_with_no_duration->count();

        foreach ($meme_medias_with_no_duration as $key => $meme_media) {

            $current = $key + 1;
            dump("Processing {$current}/{$total}: {$meme_media->webm_url}");
            $meme_media->duration = self::getDurationUsingFfmpeg($meme_media);
            $meme_media->save();
        }
    }

    public static function getDurationUsingFfmpeg($meme_media)
    {
        $duration_milliseconds = FFMpeg::openUrl($meme_media->webm_url)->getDurationInMiliseconds();
        $duration_seconds = ($duration_milliseconds / 1000);

        return $duration_seconds;
    }

    private static function getMemeMediasWithNoDuration()
    {
        return MemeMedia::whereNull('duration')->get();
    }
}
