<?php

namespace Tests\Concerns;

use Exception;
use Illuminate\Support\Collection;
use App\Models\Blog;
use App\Models\Menu;
use App\Models\Plan;

trait LoadsApplicationData
{
    /**
     * Load application data for testing.
     */
    protected function loadApplicationData(): void
    {
        // For testing, set up mock configuration data
        $settings = [
            'app_name' => 'ZEmailnator',
            'app_base_url' => 'http://localhost:8000',
            'app_title' => 'ZEmailnator - Temporary Email Service',
            'app_description' => 'Create temporary email addresses instantly',
            'app_keyword' => 'temp email, disposable email, fake email',
            'app_meta' => json_encode([
                'author' => 'ZEmailnator',
                'robots' => 'index, follow',
            ]),
            'imap_settings' => json_encode([
                'host' => 'imap.gmail.com',
                'port' => 993,
                'protocol' => 'imap',
                'encryption' => 'ssl',
                'validate_cert' => true,
                'username' => 'test@gmail.com',
                'password' => 'password',
            ]),
            'configuration_settings' => json_encode([
                'custom_username_length_min' => 3,
                'custom_username_length_max' => 20,
                'random_username_length_min' => 6,
                'random_username_length_max' => 12,
                'forbidden_ids' => ['admin', 'root', 'test'],
                'gmailUsernames' => ['john.doe', 'jane.smith'],
                'outlookUsernames' => ['outlookuser', 'testuser'],
                'domains' => ['gmail.com', 'outlook.com', 'example.com'],
                'enable_create_from_url' => true,
                'disable_mailbox_slug' => false,
                'fetch_messages_limit' => 15,
                'blocked_domains' => ['spam.com', 'blocked.com'],
                'date_format' => 'd M Y h:i A',
                'add_mail_in_title' => false,
                'fetch_seconds' => 30,
            ]),
            'ads_settings' => json_encode([
                'enabled' => false,
                'provider' => 'google',
                'one' => '<!-- Ad content placeholder -->',
                'two' => '<!-- Ad content placeholder -->',
            ]),
        ];

        // Try to load data from database, but fail gracefully if tables don't exist
        try {
            $menus = cache()->remember('app_menus', now()->addHours(6), Menu::all(...));

            $blogs = cache()->remember('app_blogs', now()->addHours(6), fn() => Blog::query()->where('is_published', 1)->get());

            $plans = cache()->remember('app_plans', now()->addHours(6), Plan::all(...));
        } catch (Exception) {
            // Set empty collections if database tables don't exist
            $menus = collect();
            $blogs = collect();
            $plans = collect();
        }

        // Ensure we always have collections, even if cache is empty
        if (! ($menus instanceof Collection)) {
            $menus = collect();
        }
        if (! ($blogs instanceof Collection)) {
            $blogs = collect();
        }
        if (! ($plans instanceof Collection)) {
            $plans = collect();
        }

        config(['app.settings' => $settings]);
        config(['app.menus' => $menus]);
        config(['app.blogs' => $blogs]);
        config(['app.plans' => $plans]);
    }
}
