<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\IamPrincipal;
use Illuminate\Support\Facades\DB;
use App\Models\ManageRestaurant;


class DashboardController extends Controller
{
    /**
     * Created By : sayali parab
     * Created at : 16 May 2024
     * Use : To show the dashboard.
     */

    // public function showDashboard(){


    //     return view('Admin.dashboard');
    // }
    public function showDashboard()
    {
        // Monthly data
        $dataMonthlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("MONTH(created_at) as month"))
            ->where('principal_type_xid', 3)
            ->whereYear('created_at', date('Y'))
            ->groupBy(DB::raw("MONTH(created_at)"))
            ->orderBy(DB::raw("MONTH(created_at)"))
            ->pluck('count', 'month')
            ->toArray();

        $dataMonthlyWithType4 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("MONTH(created_at) as month"))
            ->where('principal_type_xid', 4)
            ->whereYear('created_at', date('Y'))
            ->groupBy(DB::raw("MONTH(created_at)"))
            ->orderBy(DB::raw("MONTH(created_at)"))
            ->pluck('count', 'month')
            ->toArray();

        // Fill missing months with zeros
        $months = range(1, 12);
        $dataMonthlyWithType3 = array_replace(array_fill_keys($months, 0), $dataMonthlyWithType3);
        $dataMonthlyWithType4 = array_replace(array_fill_keys($months, 0), $dataMonthlyWithType4);

        // Quarterly data
        $dataQuarterlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("QUARTER(created_at) as quarter"))
            ->where('principal_type_xid', 3)
            ->groupBy(DB::raw("QUARTER(created_at)"))
            ->orderBy(DB::raw("QUARTER(created_at)"))
            ->pluck('count', 'quarter')
            ->toArray();

        $dataQuarterlyWithType4 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("QUARTER(created_at) as quarter"))
            ->where('principal_type_xid', 4)
            ->groupBy(DB::raw("QUARTER(created_at)"))
            ->orderBy(DB::raw("QUARTER(created_at)"))
            ->pluck('count', 'quarter')
            ->toArray();

        // Fill missing quarters with zeros
        $quarters = range(1, 4);
        $dataQuarterlyWithType3 = array_replace(array_fill_keys($quarters, 0), $dataQuarterlyWithType3);
        $dataQuarterlyWithType4 = array_replace(array_fill_keys($quarters, 0), $dataQuarterlyWithType4);

        // Yearly data
        $dataYearlyWithType3 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year"))
            ->where('principal_type_xid', 3)
            ->groupBy(DB::raw("YEAR(created_at)"))
            ->pluck('count', 'year')
            ->toArray();

        $dataYearlyWithType4 = IamPrincipal::select(DB::raw("COUNT(*) as count"), DB::raw("YEAR(created_at) as year"))
            ->where('principal_type_xid', 4)
            ->groupBy(DB::raw("YEAR(created_at)"))
            ->pluck('count', 'year')
            ->toArray();

        $customerCount = IamPrincipal::where('principal_type_xid', '=', 3)->count();
        $restaurantCount = ManageRestaurant::where('is_active', 1)->count();

        return view('Admin.dashboard', compact(
            'customerCount',
            'restaurantCount',
            'dataMonthlyWithType3',
            'dataMonthlyWithType4',
            'dataQuarterlyWithType3',
            'dataQuarterlyWithType4',
            'dataYearlyWithType3',
            'dataYearlyWithType4'
        ));
    }
}
