<?php

declare(strict_types=1);

namespace TechnoArtisan\FilamentCms\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\Factory;
use Illuminate\View\View;
use TechnoArtisan\FilamentCms\FilamentCms;
use TechnoArtisan\FilamentCms\Services\TrackUserPageViews;

class RobotsTxtController extends Controller
{
    public function __construct(private readonly TrackUserPageViews $trackUserPageViews)
    {
    }

    public function index(Request $request, string $locale, string $page = ''): Factory|View // '' = home/index
    {
        $pageKey   = getPageKeyFromLocalizedPage($page);
        $variables = [
            'page'             => $page,
            'currentLocale'    => $locale,
            'pageKey'          => $pageKey,
            'languageSwitcher' => getPreparedLanguageSelectorData($pageKey),
        ];

        $this->trackUserPageViews->process($request);

        return view(
            FilamentCms::getPackageName() . '::theme.' . FilamentCms::getTheme() . '.' . $pageKey,
            $variables
        );
    }
}
