<?php

namespace App\Http\Controllers;

use App\Helpers\FirstParty\Meme\MemeGenerator;
use App\Models\BackgroundMedia;
use App\Models\Meme;
use App\Models\MemeMedia;
use Illuminate\Http\Request;

class FrontMediaController extends Controller
{
    public function init(Request $request)
    {
        $meme = Meme::with('meme_media', 'background_media')->where('status', MemeGenerator::STATUS_COMPLETED)->inRandomOrder()->first();

        $meme_media = MemeGenerator::getSuitableMemeMedia($meme, 2);

        return response()->json([
            'success' => [
                'data' => [
                    'init' => [
                        'info' => $meme,
                        'caption' => $meme->caption,
                        'meme' => $meme_media,
                        'background' => $meme->background_media,
                    ],
                ],
            ],
        ]);
    }

    public function memes(Request $request)
    {
        $memes = MemeMedia::where('type', 'video')->where('sub_type', 'overlay')->take('30')->inRandomOrder()->get();

        return response()->json([
            'success' => [
                'data' => [
                    'memes' => $memes,
                ],
            ],
        ]);
    }

    public function background(Request $request)
    {
        $backgrounds = BackgroundMedia::where('status', 'completed')->take('30')->inRandomOrder()->get();

        return response()->json([
            'success' => [
                'data' => [
                    'backgrounds' => $backgrounds,
                ],
            ],
        ]);
    }
}
