<?php

namespace App\Http\Controllers\Admin;

use App\Article;
use App\Category;
use App\Condition;
use App\Contact;
use App\Faq;
use App\Header;
use App\Http\Controllers\Controller;
use App\Keyword;
use App\Manufacturer;
use App\Order;
use App\Payment;
use App\Product;
use App\Scene;
use App\Seo;
use App\Laravue\Models\User;
use Illuminate\Http\Request;
use App\Laravue\Acl;

class DashboardController extends Controller
{
    public function statistic()
    {
        $data = [];
        $user = auth('api')->user();

        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_PRODUCT_MANAGE)) {
            $productList = Product::where('is_deleted', false);
            $data['product'] = ['total' => $productList->count()];
        }
        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_ORDER_MANAGE)) {
            $orderList = Order::where('is_deleted', false);
            $data['order'] = ['total' => $orderList->count()];
        }
        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_CATEGORY_MANAGE)) {
            $totalCategory = Category::where('is_deleted', false);
            $data['category'] = ['total' => $totalCategory->count()];
        }
        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_CONDITION_MANAGE)) {
            $totalCondition = Condition::where('is_deleted', false);
            $data['condition'] = ['total' => $totalCondition->count()];
        }
        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_USER_MANAGE)) {
            $totalUser = User::whereHas('roles', function ($q) {
                $q->where('name', 'user');
            })->count();

            // Đếm số user chưa duyệt xác thực (pending hoặc not_submitted)
            $pendingVerificationCount = User::whereHas('roles', function ($q) {
                $q->where('name', 'user');
            })->where(function ($q) {
                $q->where('identity_status', 'pending')
                  ->orWhere('identity_status', 'not_submitted')
                  ->orWhereNull('identity_status');
            })->count();

            $data['user'] = [
                'total' => $totalUser,
                'pending_verification' => $pendingVerificationCount
            ];
        }
        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_ARTICLE_MANAGE)) {
            $totalArticle = Article::where('is_deleted', false)->where('type', 'article');
            if (!$user->hasRole(['admin', 'manager']) && !$user->can(Acl::PERMISSION_ARTICLE_MANAGE)) {
                $totalArticle->where('user_id', $user->id);
            }
            $data['article'] = ['total' => $totalArticle->count()];
        }
        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_ARTICLE_MANAGE)) {
            $totalColumn = Article::where('is_deleted', false)->where('type', 'column');
            if (!$user->hasRole(['admin', 'manager']) && !$user->can(Acl::PERMISSION_ARTICLE_MANAGE)) {
                $totalColumn->where('user_id', $user->id);
            }
            $data['column'] = ['total' => $totalColumn->count()];
        }
        if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_CATEGORY_MANAGE)) {
            $totalContact = Contact::where('is_deleted', false);
            $data['contact'] = ['total' => $totalContact->count()];
        }
        /*if ($user->hasRole(['admin', 'manager']) || $user->can(Acl::PERMISSION_DASHBOARD_KEYWORD_MANAGE)) {
            $totalKeyword = Keyword::where('is_deleted', false)->count();
            $data['keyword'] = ['total' => $totalKeyword];
        }*/

        return response()->json(['data' => $data], 200);
    }
}
