<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\EmbedVideo;

class RefreshVideos extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'brentwood:refresh-videos {--clear}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Regenerate all video files for embedded videos';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $videos = EmbedVideo::whereHas('fileUpload')->latest()->get()->groupBy('file_upload_id')->map(function ($group) {
            return $group->first();
        })->flatten()->values();

        $this->info('--- Queuing Videos ---');

        $this->withProgressBar($videos, function ($video) {
            $video->regenerate($this->option('clear') ? true : false);
        });
    }
}
