<?php

namespace App\Modules\Dashboard\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Dashboard\Services\DashboardService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class DashboardController extends Controller
{
    public function __construct(
        private readonly DashboardService $dashboard,
    ) {}

    public function index(Request $request): View|RedirectResponse
    {
        $user = $request->user();
        if ($user !== null && ! $user->can('dashboard.company')) {
            if ($user->can('self_service.portal')) {
                return redirect()->route('me.dashboard');
            }
            abort(403);
        }

        $user->loadMissing(['tenant.plan', 'tenant.activeSubscription.plan']);
        $data = $this->dashboard->buildViewData($user);

        return view('pages.dashboard.index', compact('data'));
    }
}
