<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Alerta;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class EmpresaController extends Controller
{
    
    public function dashboard()
    {
        $empresaId = Auth::user()->empresa_id;
    
        $empleadosCount = User::where('empresa_id', $empresaId)
                              ->where('role', 'empleado')
                              ->count();
    
        $alertasTotal = Alerta::where('empresa_id', $empresaId)->count();
        $alertasPendientes = Alerta::where('empresa_id', $empresaId)
                                    ->where('estado', 'pendiente')->count();
        $ultimaAlerta = Alerta::where('empresa_id', $empresaId)
                              ->latest()->first();
    
        $labels = [];
        $data = [];
    
        for ($i = 6; $i >= 0; $i--) {
            $date = Carbon::today()->subDays($i);
            $labels[] = $date->format('d/m');
            $data[] = Alerta::where('empresa_id', $empresaId)
                            ->whereDate('created_at', $date)
                            ->count();
        }
    
        return view('empresa.dashboard', compact(
            'empleadosCount', 'alertasTotal', 'alertasPendientes', 'ultimaAlerta',
            'labels', 'data'
        ));
    }
    
}
