<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use App\Models\WebsiteVisitor;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
use Modules\Category\Models\Category;
use Modules\Order\Enums\OrderStatusEnum;
use Modules\Order\Services\OrderService;
use Modules\Product\Models\Product;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Models\User;
use Modules\User\Services\UserService;

class DashboardController extends Controller
{
    private OrderService $orderService;
    private UserService $userService;

    public function __construct(OrderService $orderService,UserService $userService)
    {
        $this->orderService = $orderService;
        $this->userService = $userService;
    }

    /**
     * Show the stats of the website.٫٣
     *
     * @return Factory|View
     */
    public function stats(Request $request)
    {
        $subscribtions= [];
        for ($i=1; $i < 13; $i++) {
            $month= ($request->year ?? date('Y')).'-'.($i<10 ? '0'.$i : $i);
            $subscribtions[] = [
                'month'=> $i,
                'value'=> WebsiteVisitor::whereRaw("DATE_FORMAT(created_at, '%Y-%m') = '". $month ."'")->count()
            ];
        }
        return response()->json($subscribtions);
    }

    public function index(Request $request) :View
    {
        $date = date('Y-m-d H:i:s',strtotime('- 5 min'));
        $allUsersCounts = WebsiteVisitor::count();
        $nowUsersCounts = WebsiteVisitor::whereDate('updated_at','>=',$date)->count();
        $dayUsers = WebsiteVisitor::whereRaw('DATE(created_at) = CURRENT_DATE')->count();
        $weekUsers = WebsiteVisitor::whereRaw('YEARWEEK(created_at) = YEARWEEK(CURRENT_DATE)')->count();
        $monthUsers = WebsiteVisitor::whereRaw("DATE_FORMAT(created_at, '%Y-%m') = DATE_FORMAT(CURRENT_DATE, '%Y-%m')")->count();
        $yearUsers = WebsiteVisitor::whereRaw("YEAR(created_at) = YEAR(CURRENT_DATE)")->count();
        $request->merge(['role'=>UserRoleEnum::USER->value]);
        $members = $this->userService->get($request)->count();
        $request->merge(['roles'=>UserRoleEnum::admins()]);
        $admins = $this->userService->get($request)->count();

        $user_orders = User::query()
            ->leftJoin('orders','users.id','orders.user_id')
            ->where('orders.deleted_at',null)
            ->select('users.name as name','users.id as id', DB::raw('count(orders.id) as count'))
            ->orderByDESC('count')
            ->take(10)
            ->groupBy('id')
            ->get();
        $products = Product::query()
            ->join('order_products as op','products.id','op.product_id')
            ->join('orders','op.order_id','orders.id')
            ->where('orders.deleted_at',null)
            ->select('products.id as id', DB::raw('sum(op.count) as count'))
            ->orderByDESC('count')
            ->take(10)
            ->groupBy('id')
            ->get();

        $orders = Order::sum('total');
        $orders_count = Order::count();
        $orders_statuses = [];
        $statuses = OrderStatusEnum::values();
        foreach ($statuses as $key=>$row)
        {
            $orders_statuses[$key]['status'] = __('orders.head').' : ' .__('orders.statuses.'.$row);
            $request->merge(['type'=>$row]);
            $orders_statuses[$key]['count'] = $this->orderService->get($request)->count();

        }
        return view('admin.index',get_defined_vars());
    }
}
