<?php

namespace App\Http\Controllers;

use App\Models\Order;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function report_details(Request  $request)
    {


        if (!$request->exists('time') || $request->input('time') == 'all'){
            $order_amount = Order::sum('amount');
            $paid_amount = Order::where('payment_status', 'COMPLETED')->sum('amount');
            $due_amount = Order::where('payment_status', '!=', 'COMPLETED')->sum('amount');
            $total_order = Order::count();
        }elseif($request->input('time') == 'this' || $request->input('time') == 'prev'){
            $month = date('m');
            if ($request->input('time') == 'prev') {
                $month = Carbon::now()->subMonth()->format('m');
            }
            $order_amount = Order::whereMonth('created_at', $month)->sum('amount');
            $paid_amount = Order::where('payment_status', 'COMPLETED')->whereMonth('created_at', $month)->sum('amount');
            $due_amount = Order::where('payment_status', '!=', 'COMPLETED')->whereMonth('created_at', $month)->sum('amount');
            $total_order = Order::whereMonth('created_at', $month)->count();

        }elseif ($request->input('time') == 'between' ){
            $start_date = $request->input('start_date').' 00.00.00' ?? Carbon::now()->format('Y-m-d').' 00:00:00';
            $end_date = Carbon::create($request->input('end_date'))->addDay()->format('Y-m-d').' 11:59:59'  ?? Carbon::now()->addDay()->format('Y-m-d').' 11:59:59';

           // whereBetween('created_at', [$start_date, $end_date])
            $order_amount = Order::whereBetween('created_at', [$start_date, $end_date])->sum('amount');
            $paid_amount = Order::whereBetween('created_at', [$start_date, $end_date])->where('payment_status', 'COMPLETED')->sum('amount');
            $due_amount = Order::whereBetween('created_at', [$start_date, $end_date])->where('payment_status', '!=', 'COMPLETED')->sum('amount');
            $total_order = Order::whereBetween('created_at', [$start_date, $end_date])->count();
        }



        $total_customer = User::where('usertype', 3)->count();
        $total_provider = User::where('usertype', 2)->count();

        $data = [
            'total_amount'=>$order_amount,
            'paid_amount'=>$paid_amount,
            'due_amount'=>$due_amount,
            'total_order'=>$total_order,
            'total_customer'=>$total_customer,
            'total_provider'=>$total_provider,
        ];
        return response()->json($data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
