<?php

namespace App\Domains\Dashboard\Controllers;

use App\Providers\AuthCheckProvider;
use App\Providers\InertiaProvider;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DashboardController extends CommonController {
    public function __invoke(Request $request) {
        if ($this->checkAuth()) {
            return $this->renderForLoggedInUser($request);
        }

        return redirect()->intended('/login');
    }

    private function renderForLoggedInUser(Request $request) {
        $authCheckProvider = new AuthCheckProvider;
        $inertiaProvider = new InertiaProvider('Dashboard/Dashboard', [
            'myInvoices' => $this->invoices->getMyInvoicesWidget(),
            'myParticipations' => $this->eventParticipants->getMyParticipations(),

        ]);
        return $inertiaProvider->render();
    }

    private function renderForGuest(Request $request) {
    }

    public function getMyInvoices() : JsonResponse {
        $invoices = $this->invoices->getMyInvoicesWidget();
        return response()->json(['myInvoices' => $invoices]);
    }

    public function getOpenCostUnits() : JsonResponse {
        $costUnits = $this->costUnits->listForSummary(5);
        return response()->json(['openCostUnits' => $costUnits]);
    }

    public function getMyParticipations() : JsonResponse {
        return response()->json(['myParticipations' => $this->eventParticipants->getMyParticipations()]);
    }

    public function getUpcomingEvents() : JsonResponse {
        return response()->json(['upcomingEvents' => $this->events->getUpcoming()]);
    }
}
