<?php

namespace App\Http\Controllers\Api\System;

use App\Http\Controllers\Controller;
use App\Services\ApplicationUrlService;
use App\Services\Dashboard\DashboardRouteService;
use Illuminate\Http\RedirectResponse;

/**
 * Redirect /dashboard by role priority (web session).
 */
class DashboardRedirectController extends Controller
{
    public function __construct(
        private DashboardRouteService $service,
        private ApplicationUrlService $urls,
    ) {
    }

    public function dashboard(): RedirectResponse
    {
        $user = auth()->user();
        if (!$user) {
            return redirect()->guest($this->urls->webLoginUrl());
        }

        $payload = $this->service->resolveForUser($user);

        return redirect()->to($payload['route']);
    }
}
