<?php

declare(strict_types=1);

namespace Maestro\Core\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Maestro\Core\Models\MaestroAppSetting;

final class PublicHomeController extends Controller
{
    public function __invoke(): RedirectResponse|View
    {
        $settings = MaestroAppSetting::instance()->public_page;
        $public = is_array($settings) ? $settings : [];

        $enabled = (bool) ($public['enabled'] ?? false);
        $rootMode = isset($public['root_mode']) && is_string($public['root_mode'])
            ? $public['root_mode']
            : 'login';

        if (! in_array($rootMode, ['dashboard', 'login', 'public'], true)) {
            $rootMode = 'login';
        }

        return match ($rootMode) {
            'dashboard' => redirect('/dashboard'),
            'public' => $enabled
                ? view('maestro-core::public-home')
                : redirect('/auth/login'),
            default => redirect('/auth/login'),
        };
    }
}
