<?php

namespace App\Livewire\Dashboard;

use App\Models\Clients\Client;
use App\Models\Monitoring\Log;
use App\Models\Monitoring\Notice;
use App\Models\Products\LicenseToken;
use App\Models\Products\Product;
use App\Models\Environments\Environment;
use Livewire\Component;

class Index extends Component
{
    public function render()
    {
        return view('livewire.dashboard.index', [
            'totalEnvironments' => Environment::count(),
            'activeEnvironments' => Environment::where('active', true)->count(),
            'activeEnvironmentsWithLicense' => Environment::whereHas('licenseToken')->count(),
            'totalEnvironmentsThisMonth' => Environment::whereMonth('created_at', now()->month)->count(),
            'totalClients' => Client::count(),
            'totalClientsThisMonth' => Client::whereMonth('created_at', now()->month)->count(),

            'totalProducts' => Product::count(),
            'activeProducts' => Product::where('enabled', true)->count(),

            'totalTokens' => LicenseToken::count(),
            'activeTokens' => LicenseToken::where('active', true)->count(),
            'expiringTokens' => LicenseToken::where('end_at', '<', now()->addMonth())->count(),
            'expiredTokens' => LicenseToken::where('end_at', '<', now())->count(),
            'totalTokensThisMonth' => LicenseToken::whereMonth('created_at', now()->month)->count(),

            'latestLogs' => Log::orderByDesc('created_at')->limit(5)->get(),
            'latestNotices' => Notice::orderByDesc('created_at')->limit(5)->get(),
        ])->layout('layouts.app');
    }
}

