<?php

namespace App\Http\Controllers\Admins;

use App\Http\Controllers\Controller;
use App\Models\AgendaGuestBooks;
use App\Models\Agendas;
use App\Models\Appointments;
use App\Models\DataRequests;
use App\Models\Master\Agamas;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        // Call view Dashboard
        return view('pages.admins.dashboard', $this->total());
    }

    public function coba()
    {
        $data_agama = $this->data_agama();
        // return $data_agama['json_keterangan'];
        // Call view Dashboard
        return view('pages.admins.coba', compact('data_agama'));
    }

    private function data_agama()
    {
        $data = Agamas::leftJoin(DB::raw('(
            SELECT 
                qa.answer,
                qs.name, 
                qd.question_label, 
                qd.question_value,
                JSON_UNQUOTE(JSON_EXTRACT(qd.question_label, \'$[0]\')) AS question_type,
                JSON_UNQUOTE(JSON_EXTRACT(qd.question_value, \'$[0]\')) AS name_master,
                COUNT(qa.answer) AS total_agama
            FROM question_answers AS qa
            LEFT JOIN question_surveys AS qs ON qs.id = qa.question_id
            LEFT JOIN question_details AS qd ON qs.id = qd.question_id
            WHERE qa.answer_type = \'Master\'
            GROUP BY qa.answer
            HAVING question_type = \'Master\' AND name_master = \'Agamas\'
        ) AS total'), 'total.answer', '=', 'mst_agamas.keterangan')
            ->select('mst_agamas.keterangan AS nama')
            ->selectRaw('IFNULL(total.total_agama, 0) AS total_agama')
            ->get();

        $json_total = $data->pluck('total_agama');
        $json_keterangan = $data->pluck('nama');

        // $json_keterangan =  '["' . implode('"], [', $json_keterangan) . '"]';

        return [
            'total' => $data->sum('total_agama'),
            'json_total' => $json_total,
            'json_keterangan' => $json_keterangan,
            'data' => $data,
        ];
    }

    // Total Request
    public function total()
    {
        // // Data Request
        // $total = DataRequests::count();
        // $total = DataRequests::where('status', 'Rejected')->count();
        // $total = DataRequests::whereYear('created_at', '2023')->count();
        // $total = DataRequests::whereMonth('created_at', '12')->count();

        // // Agenda
        // $total = Agendas::count();
        // $total = Agendas::whereYear('date', '2024')->count();
        // $total = Agendas::whereYear('date', '2024')->whereMonth('date', '01')->count();

        // // Guest Books
        // $total = AgendaGuestBooks::count();
        // $total = AgendaGuestBooks::where('agenda_id', 2)->count();
        // $total = Agendas::whereYear('date', '2024')->whereMonth('date', '01')->count();

        $totalUser = User::count();

        return compact('totalUser');
    }
}
