<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AttendanceLog;
use App\Models\Device;
use App\Models\Employee;
use App\Models\Tenant;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function superAdmin(): JsonResponse
    {
        return response()->json([
            'clients' => Tenant::count(),
            'online_clients' => Tenant::where('status', 'active')->count(),
            'active_employees' => Employee::where('employment_status', 'active')->count(),
            'online_devices' => Device::where('status', 'online')->count(),
            'live_logs' => AttendanceLog::latest('punched_at')->limit(20)->get(),
        ]);
    }

    public function client(Request $request): JsonResponse
    {
        $tenantId = $request->user()->tenant_id;
        $today = now()->toDateString();

        return response()->json([
            'total_employees' => Employee::where('tenant_id', $tenantId)->count(),
            'present_today' => AttendanceLog::where('tenant_id', $tenantId)->whereDate('punched_at', $today)->distinct('employee_id')->count('employee_id'),
            'absent_today' => 0,
            'leave_today' => 0,
            'holiday_today' => 0,
            'working_on_holiday' => 0,
            'late_in_today' => 0,
            'early_out_today' => 0,
            'late_penalty_today' => 0,
            'overtime_today' => 0,
            'active_devices' => Device::where('tenant_id', $tenantId)->where('status', 'online')->count(),
            'salary_pending' => 0,
            'branch_wise_statistics' => [],
        ]);
    }
}
