<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
    public function stats(): JsonResponse
    {
        try {
            $userCount = DB::table('admin_users')->count();
            $roleCount = DB::table('admin_roles')->count();
            $menuCount = DB::table('admin_menus')->count();
            $attachmentCount = DB::table('attachments')->count() ?? 0;

            return response()->json([
                'code' => 0,
                'message' => 'success',
                'data' => [
                    'user_count' => $userCount,
                    'role_count' => $roleCount,
                    'menu_count' => $menuCount,
                    'attachment_count' => $attachmentCount,
                ],
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 1,
                'message' => '获取统计数据失败',
                'data' => [
                    'user_count' => 0,
                    'role_count' => 0,
                    'menu_count' => 0,
                    'attachment_count' => 0,
                ],
            ]);
        }
    }
}
