<?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, 15);

        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,
                ],
            ],
        ]);
    }

    public function searchMemes(Request $request)
    {
        $query = $request->input('query', '');
        $limit = $request->input('limit', 30);

        if (empty($query)) {
            // Return random memes if no search query
            $memes = MemeMedia::where('type', 'video')->where('sub_type', 'overlay')->take($limit)->inRandomOrder()->get();
        } else {
            // PostgreSQL full-text search with ranking
            $memes = MemeMedia::where('type', 'video')
                ->where('sub_type', 'overlay')
                ->where(function ($q) use ($query) {
                    // Search in name and description using ILIKE for partial matches
                    $q->where('name', 'ILIKE', "%{$query}%")
                        ->orWhere('description', 'ILIKE', "%{$query}%")
                        // Search in JSON arrays using PostgreSQL JSON operators
                        ->orWhereRaw('keywords::text ILIKE ?', ["%{$query}%"])
                        ->orWhereRaw('action_keywords::text ILIKE ?', ["%{$query}%"])
                        ->orWhereRaw('emotion_keywords::text ILIKE ?', ["%{$query}%"])
                        ->orWhereRaw('misc_keywords::text ILIKE ?', ["%{$query}%"]);
                })
                ->orderByRaw('
                    CASE
                        WHEN name ILIKE ? THEN 1
                        WHEN description ILIKE ? THEN 2
                        WHEN keywords::text ILIKE ? THEN 3
                        WHEN action_keywords::text ILIKE ? THEN 4
                        WHEN emotion_keywords::text ILIKE ? THEN 5
                        WHEN misc_keywords::text ILIKE ? THEN 6
                        ELSE 7
                    END, name ASC
                ', ["%{$query}%", "%{$query}%", "%{$query}%", "%{$query}%", "%{$query}%", "%{$query}%"])
                ->take($limit)
                ->get();
        }

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

    public function searchBackgrounds(Request $request)
    {
        $query = $request->input('query', '');
        $limit = $request->input('limit', 30);

        if (empty($query)) {
            // Return random backgrounds if no search query
            $backgrounds = BackgroundMedia::where('status', 'completed')->take($limit)->inRandomOrder()->get();
        } else {
            // Search in prompt field using ILIKE for partial matches
            $backgrounds = BackgroundMedia::where('status', 'completed')
                ->where('prompt', 'ILIKE', "%{$query}%")
                ->orderByRaw('
                    CASE
                        WHEN prompt ILIKE ? THEN 1
                        ELSE 2
                    END, prompt ASC
                ', ["%{$query}%"])
                ->take($limit)
                ->get();
        }

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