<?php

namespace App\Http\Controllers;

use App\Models\BackgroundMedia;
use App\Models\MemeMedia;
use App\Services\MemeMediaService;
use Artesaos\SEOTools\Facades\JsonLd;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;

class FrontHomeController extends Controller
{
    public function __construct(
        private MemeMediaService $memeMediaService
    ) {}

    public function index()
    {
        if (App::environment('production') && env('COMING_SOON_ENABLED', true)) {
            return Inertia::render('comingsoon');
        }

        // Cache the counts for 1 day to reduce server calls
        $stats = Cache::remember('home_stats', 60 * 60 * 24, function () {
            return [
                'meme_count' => MemeMedia::count(),
                'background_count' => BackgroundMedia::count(),
            ];
        });

        // Get FAQ data
        $faqData = $this->getFaqData();

        // Get popular keywords for search suggestions
        $popularKeywords = $this->memeMediaService->getPopularKeywords(10);

        // Add FAQ JSON-LD structured data
        $this->addFaqJsonLd($faqData);

        return Inertia::render('home/home', [
            'stats' => $stats,
            'faqData' => $faqData,
            'popularKeywords' => $popularKeywords,
        ]);
    }

    private function getFaqData()
    {
        return [
            [
                'q' => 'What is MEMEFA.ST?',
                'a' => 'MEMEFA.ST is a simple & fast meme video editor, 100% powered by the web. You can now quickly create and export video memes for TikTok, Youtube Shorts, Instagram Reels channel, or just share with your friends in WhatsApp, Discord, Facebook Messenger, Telegram, etc!',
            ],
            [
                'q' => 'Why did you made MEMEFA.ST?',
                'a' => "<a target=\"blank\" class=\"underline\" href=\"https://x.com/charlestehio\">Charles</a> here, creator of MEMEFA.ST. I like making fun memes to troll my friends, and I decided that there needs to be a faster solution than popular video editors like CapCut and Adobe Premiere Pro.<br><br>Thanks to today's web technology, we can now leverage the power of the web to create and export meme videos in minutes! Isn't that awesome?",
            ],
            [
                'q' => 'How can I create a meme video?',
                'a' => 'Use the video editor on top to start making your meme!<br><br>Edit your caption, background and meme. Once satisfied, press the Export button to download your video!',
            ],
            [
                'q' => 'Why is video export slow for me?',
                'a' => 'Video processing happens entirely in your browser using advanced web technology.<br><br> Export speed depends on your video content complexity and device performance. High-end devices export quickly, while older/slower devices may take longer or even crash. <br><br>If your phone is too slow, it\'s probably a potato. Try using a faster device like a desktop computer for better performance.',
            ],
            [
                'q' => 'What is a potato device?',
                'a' => 'Potato devices are old, dated computers, laptops, phones or tablets with little RAM and CPU power - typically devices from before '.(now()->year - 6).' (pre-'.(now()->year - 6).').<br><br>These devices are usually too weak for the modern web and may not work properly with our video editor, or may crash during video processing.',
            ],
            [
                'q' => 'What video format do you export?',
                'a' => 'We export high-quality 720p MP4 videos optimized for all social media platforms in 9:16 format.<br><br>This is compatible for TikTok, Youtube Shorts, Instagram Reels, and more.',
            ],
            [
                'q' => 'Is there a mobile app?',
                'a' => 'Our web app is fully responsive and works perfectly on mobile devices.<br><br>Do you want a mobile app? We\'ll see what can be done. Let us know in our Discord group.',
            ],
            [
                'q' => 'How often do you add new content?',
                'a' => 'We just started building this platform and will gradually add more meme templates and backgrounds over time, so everyone can continue using it for free with fresh content!<br><br>Want a certain content? Let us know in our Discord group.',
            ],
            [
                'q' => 'I found a bug!',
                'a' => 'Great, report it into the Discord group and we\'ll fix it as soon as possible!',
            ],
            [
                'q' => 'I have more questions!',
                'a' => 'Great! Join our Discord group and ask away!',
            ],
        ];
    }

    private function addFaqJsonLd($faqData)
    {
        JsonLd::addValue('@type', 'FAQPage');
        JsonLd::addValue('mainEntity', collect($faqData)->map(function ($faq) {
            return [
                '@type' => 'Question',
                'name' => $faq['q'],
                'acceptedAnswer' => [
                    '@type' => 'Answer',
                    'text' => strip_tags(str_replace('<br><br>', ' ', $faq['a'])),
                ],
            ];
        })->toArray());
    }
}
