<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\View;

class LandingPagesController extends Controller
{
    public function index(
        Request $request,
        CatalogLandingPageGenerationController $catalogPages,
        PromptLandingPageController $promptPages,
    ): View {
        $tab = (string) $request->query('tab', 'catalog');

        if (! in_array($tab, ['catalog', 'prompt'], true)) {
            $tab = 'catalog';
        }

        $tabUrl = fn (string $name): string => route('landing-pages.index', array_merge(
            $request->except(['tab', 'page']),
            ['tab' => $name],
        ));

        $viewData = [
            'activeTab' => $tab,
            'tabUrl' => $tabUrl,
        ];

        if ($tab === 'catalog') {
            $viewData = array_merge($viewData, $catalogPages->indexData($request));
        } else {
            $viewData = array_merge($viewData, $promptPages->indexData($request));
        }

        return view('landing-pages.index', $viewData);
    }
}
