<?php

namespace Modules\User\Controllers\Canteen;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use App\Models\WebsiteVisitor;
use Illuminate\View\View;
use Modules\Order\Services\OrderService;
use Modules\PaymentLog\Services\PaymentLogService;
use Modules\Product\Enums\ProductStatusEnum;
use Modules\Product\Services\ProductService;
use Modules\User\Services\CanteenModuleService;
use Modules\User\Services\UserService;

class DashboardController extends Controller
{

    public function __construct(
        private ProductService $productService,
        private UserService $userService,
        private PaymentLogService $paymentLogService,
        private OrderService $orderService,
        private CanteenModuleService $canteenModuleService,
    ){}

    /**
     * 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'));
        $allOrdersCounts = $this->orderService->get(
            [
                'canteen_id'=>auth()->user()->canteen_id
            ]
        )->count();
        $yearOrders = $this->orderService->get(
            [
                'canteen_id'=>auth()->user()->canteen_id,
                'condition' => 'YEAR(created_at) = YEAR(CURRENT_DATE)'
            ]
        )->count();
        $monthOrders = $this->orderService->get(
            [
                'canteen_id' => auth()->user()->canteen_id,
                'condition' => "DATE_FORMAT(created_at, '%Y-%m') = DATE_FORMAT(CURRENT_DATE, '%Y-%m')"
            ]
        )->count();
        $weekOrders = $this->orderService->get(
            [
                'canteen_id' => auth()->user()->canteen_id,
                'condition' => "YEARWEEK(created_at) = YEARWEEK(CURRENT_DATE)"
            ]
        )->count();
        $dayOrders = $this->orderService->get(
            [
                'canteen_id' => auth()->user()->canteen_id,
                'condition' => "DATE(created_at) = CURRENT_DATE"
            ]
        )->count();

        $products = $this->productService->get(
            [
                'canteen_id' => auth()->user()->canteen_id,
                'valid'=> ProductStatusEnum::ACCEPT->value
            ]
        )->count();
        $payment = $this->paymentLogService->getTotal(auth()->user()->canteen_id);
        $access = $this->canteenModuleService->checkModuleAccess('statistics',auth()->user()->id);
        return view('canteen.index',get_defined_vars());
    }
}
