<?php

namespace App\Livewire;

use Illuminate\Support\Facades\Session;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use App\Models\ZEmail;
use Livewire\Component;

class AddOn extends Component
{
    public $title = '';

    public $description = '';

    public $keywords = '';

    public $route = '';

    public $faqs = [];

    public $faqSchema;

    protected $listeners = ['fetchMessages' => 'checkIfAnyMessage'];

    public function checkIfAnyMessage()
    {
        $email = ZEmail::getEmail();
        $messages = ZEmail::getMessages($email);
        if (count($messages['data']) > 0) {
            return to_route('mailbox');
        }
        return null;
    }

    public function mount(): void
    {
        $this->faqs = json_decode(file_get_contents(public_path('addOnFAQs.json')), true) ?? [];
        $route = request()->route()->getName();
        Session::put('addOn-route', $this->route);
        if ($route == 'disposable-email') {
            $this->title = 'Free Disposable Email | Temporary & Anonymous Email Service';
            $this->description = 'Use our free disposable email service to stay private and spam-free. Instantly create a secure, anonymous, and temporary email address—no registration needed.';
            $this->keywords = 'disposable email, free disposable email, temporary email, anonymous disposable email, temporary disposable email, secure disposable email, fake email, temp inbox, throwaway email';
            $this->route = $route;
            $this->faqs = $this->faqs[$route];
            $this->faqSchema = [
                '@context' => 'https://schema.org',
                '@type' => 'FAQPage',
                'mainEntity' => collect($this->faqs)->map(fn(array $faq): array => [
                    '@type' => 'Question',
                    'name' => $faq['title'],
                    'acceptedAnswer' => [
                        '@type' => 'Answer',
                        'text' => $faq['content'],
                    ],
                ])->all(),
            ];
        } elseif ($route == 'disposable-gmail') {
            $this->title = 'Disposable Gmail Email | Free Temporary Gmail Inbox Online';
            $this->description = 'Create a free disposable Gmail email address. Use a temporary Gmail inbox for sign-ups, testing, or staying spam-free. No login or registration required.';
            $this->keywords = 'disposable Gmail, temporary Gmail inbox, Gmail disposable email, free disposable Gmail email address, Gmail temp email, anonymous Gmail email';
            $this->route = $route;
            $this->faqs = $this->faqs[$route];
            $this->faqSchema = [
                '@context' => 'https://schema.org',
                '@type' => 'FAQPage',
                'mainEntity' => collect($this->faqs)->map(fn(array $faq): array => [
                    '@type' => 'Question',
                    'name' => $faq['title'],
                    'acceptedAnswer' => [
                        '@type' => 'Answer',
                        'text' => $faq['content'],
                    ],
                ])->all(),
            ];

        } elseif ($route == 'disposable-outlook') {
            $this->title = 'Disposable Outlook Email | Free Temporary Outlook Inbox';
            $this->description = 'Get a free disposable Outlook email address instantly. Use a temporary Outlook inbox to avoid spam and protect your main account—no registration required.';
            $this->keywords = 'disposable Outlook, Outlook disposable email, temporary Outlook inbox, free Outlook temp email, anonymous Outlook address, Outlook email for sign-ups';
            $this->route = $route;
            $this->faqs = $this->faqs[$route];
            $this->faqSchema = [
                '@context' => 'https://schema.org',
                '@type' => 'FAQPage',
                'mainEntity' => collect($this->faqs)->map(fn(array $faq): array => [
                    '@type' => 'Question',
                    'name' => $faq['title'],
                    'acceptedAnswer' => [
                        '@type' => 'Answer',
                        'text' => $faq['content'],
                    ],
                ])->all(),
            ];
        } elseif ($route == 'disposable-yahoo') {
            $this->title = 'Disposable Yahoo Mail | Free Temporary Yahoo Inbox';
            $this->description = 'Create a disposable Yahoo Mail address instantly. Use a free, temporary Yahoo inbox to stay private, filter spam, and protect your real Yahoo account.';
            $this->keywords = 'disposable Yahoo mail, Yahoo mail disposable email, temporary Yahoo inbox, free Yahoo temp email, Yahoo throwaway email';
            $this->route = $route;
            $this->faqs = $this->faqs[$route];
            $this->faqSchema = [
                '@context' => 'https://schema.org',
                '@type' => 'FAQPage',
                'mainEntity' => collect($this->faqs)->map(fn(array $faq): array => [
                    '@type' => 'Question',
                    'name' => $faq['title'],
                    'acceptedAnswer' => [
                        '@type' => 'Answer',
                        'text' => $faq['content'],
                    ],
                ])->all(),
            ];
        } elseif ($route == 'gmailnator') {
            $this->title = 'Gmailnator - Free Temporary Gmail Email Address Generator';
            $this->description = 'Use Gmailnator to get a disposable Gmail address instantly. Perfect for one-time use, signups, or testing. No account needed. Try Gmailnator now.';
            $this->keywords = 'gmailnator, gmailnator temp mail, gmailnator gmail, free gmailnator, gmailnator email address, gmailnator online, gmail temp mail, gmail fake email';
            $this->route = $route;
            $this->faqs = $this->faqs[$route];
            $this->faqSchema = [
                '@context' => 'https://schema.org',
                '@type' => 'FAQPage',
                'mainEntity' => collect($this->faqs)->map(fn(array $faq): array => [
                    '@type' => 'Question',
                    'name' => $faq['title'],
                    'acceptedAnswer' => [
                        '@type' => 'Answer',
                        'text' => $faq['content'],
                    ],
                ])->all(),
            ];
        } elseif ($route == 'emailnator') {
            $this->title = 'Emailnator - Free Disposable Temporary Email Service';
            $this->description = 'Use Emailnator to create a free temporary email address instantly. No sign-up needed. Secure, fast, and spam-free inboxes for signups or testing.';
            $this->keywords = 'emailnator, emailnator temp mail, temporary email, disposable email, free temp inbox, temp mail generator, receive email online';
            $this->route = $route;
            $this->faqs = $this->faqs[$route];
            $this->faqSchema = [
                '@context' => 'https://schema.org',
                '@type' => 'FAQPage',
                'mainEntity' => collect($this->faqs)->map(fn(array $faq): array => [
                    '@type' => 'Question',
                    'name' => $faq['title'],
                    'acceptedAnswer' => [
                        '@type' => 'Answer',
                        'text' => $faq['content'],
                    ],
                ])->all(),
            ];
        } elseif ($route == 'temp-gmail') {
            $this->title = 'Temp Gmail | Free Temporary Gmail Inbox for Instant Use';
            $this->description = 'Use Temp Gmail to get a temporary Gmail address fast. No registration. Secure, spam-free inboxes for signups, testing, or quick email verification.';
            $this->keywords = 'temp gmail, temporary gmail, disposable gmail, gmail temp email, gmail fake email, temporary gmail address, gmail for verification';
            $this->route = $route;
            $this->faqs = $this->faqs[$route];
            $this->faqSchema = [
                '@context' => 'https://schema.org',
                '@type' => 'FAQPage',
                'mainEntity' => collect($this->faqs)->map(fn(array $faq): array => [
                    '@type' => 'Question',
                    'name' => $faq['title'],
                    'acceptedAnswer' => [
                        '@type' => 'Answer',
                        'text' => $faq['content'],
                    ],
                ])->all(),
            ];
        }

    }

    public function render(): Factory|View
    {
        return view('livewire.add-on');
    }
}
