<?php

namespace App\Http\Controllers;

use App\Models\BookingModel;
use App\Models\LogKecemasanModel;
use App\Models\TestSessionsModel;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function getDataForDashboardTable()
    {
        $data_booking = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m-d') . '%')->get();
        $result = [];
        foreach ($data_booking as $booking) {
            $status_session = '0';
            $data_sessions = TestSessionsModel::where('id_booking', $booking->id_booking)->first();
            if($data_sessions != null) {
                $status_session = $data_sessions->status_session;
            }

            $result[] = [
                'nama_lengkap' => $booking->nama_lengkap,
                'email' => $booking->email,
                'no_telp' => $booking->no_telp,
                'tanggal_booking' => $booking->created_at->format('Y-m-d'),
                'status_session' => $status_session,
                'status_tes' => $booking->status_tes,
            ];
        }
        return response()->json([
            'status' => true,
            'data' => $result
        ]);
    }
    public function getDataDashboard()
    {
        $data_booking_today = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m-d') . '%')->count();
        $data_booking_yesterday = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m-d', strtotime('-1 day')) . '%')->count();
        $data_booking_month = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m') . '%')->count();
        $booking_diff = $data_booking_month - $data_booking_today;
        $booking_percent = $data_booking_month > 0 ? ($booking_diff / $data_booking_month) * 100 : 0;

        $data_tes_today = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m-d') . '%')->where('status_tes', '2')->count();
        $data_tes_yesterday = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m-d', strtotime('-1 day')) . '%')->where('status_tes', '2')->count();
        $data_tes_month = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m') . '%')->where('status_tes', '2')->count();
        $tes_diff = $data_tes_month - $data_tes_today;
        $tes_percent = $data_tes_month > 0 ? ($tes_diff / $data_tes_month) * 100 : 0;

        $data_tes_berlangsung_today = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m-d') . '%')->where('status_tes', '1')->count();
        $data_tes_berlangsung_yesterday = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m-d', strtotime('-1 day')) . '%')->where('status_tes', '1')->count();
        $data_tes_berlangsung_month = BookingModel::where('created_at', 'LIKE', '%' . date('Y-m') . '%')->where('status_tes', '1')->count();
        $tes_berlangsung_diff = $data_tes_berlangsung_month - $data_tes_berlangsung_today;
        $tes_berlangsung_percent = $data_tes_berlangsung_month > 0 ? ($tes_berlangsung_diff / $data_tes_berlangsung_month) * 100 : 0;

        $data_rata_rata_kecemasan_today = LogKecemasanModel::where('created_at', 'LIKE', '%' . date('Y-m-d') . '%')->avg('skor_kecemasan');
        $data_rata_rata_kecemasan_yesterday = LogKecemasanModel::where('created_at', 'LIKE', '%' . date('Y-m-d', strtotime('-1 day')) . '%')->avg('skor_kecemasan');
        $data_rata_rata_kecemasan_month = LogKecemasanModel::where('created_at', 'LIKE', '%' . date('Y-m') . '%')->avg('skor_kecemasan');
        $rata_rata_kecemasan_diff = $data_rata_rata_kecemasan_month - $data_rata_rata_kecemasan_today;
        $rata_rata_kecemasan_percent = $data_rata_rata_kecemasan_month > 0 ? ($rata_rata_kecemasan_diff / $data_rata_rata_kecemasan_month) * 100 : 0;

        return response()->json([
            'total_booking' => $data_booking_today,
            'booking_percent' => number_format($booking_percent, 1),
            'booking_diff' => $booking_diff,
            'booking_trend' => $booking_diff >= 0 ? 'bertambah' : 'berkurang',
            'from_month_booking_count' => $data_booking_month,

            'total_tes' => $data_tes_today,
            'tes_percent' => number_format($tes_percent, 1),
            'tes_diff' => $tes_diff,
            'tes_trend' => $tes_diff >= 0 ? 'bertambah' : 'berkurang',
            'from_month_tes_count' => $data_tes_month,

            'total_tes_berlangsung' => $data_tes_berlangsung_today,
            'tes_berlangsung_percent' => number_format($tes_berlangsung_percent, 1),
            'tes_berlangsung_diff' => $tes_berlangsung_diff,
            'tes_berlangsung_trend' => $tes_berlangsung_diff >= 0 ? 'bertambah' : 'berkurang',
            'from_month_tes_berlangsung_count' => $data_tes_berlangsung_month,

            'rata_rata_kecemasan' => number_format($data_rata_rata_kecemasan_today, 1),
            'rata_rata_kecemasan_percent' => number_format($rata_rata_kecemasan_percent, 1),
            'rata_rata_kecemasan_diff' => number_format($rata_rata_kecemasan_diff, 1),
            'rata_rata_kecemasan_trend' => $rata_rata_kecemasan_diff >= 0 ? 'bertambah' : 'berkurang',
            'from_month_rata_rata_kecemasan' => number_format($data_rata_rata_kecemasan_month, 1),
        ]);
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('admin.pages.dashboard.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
