<?php

namespace App\Http\Controllers;

use App\Models\Order;
use App\Models\Revenue;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RevenueController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse
     */
    public function index(): JsonResponse
    {
        $query = Order::where('assigned_to', auth()->user()->id)->where('payment_status','COMPLETED');
        $total_paid_amount  = $query->sum('amount');
        $total_paid_order = $query->count();
        $total_revenue = $total_paid_order * 40;
        $paid_revenue =  Revenue::where('provider_id', auth()->user()->id)->sum('paid_amount');
        $due_revenue = $total_revenue-$paid_revenue;
        $total_amount =Order::where('assigned_to', auth()->user()->id)->sum('amount');

        $revenue= ['paid_order_amount'=>$total_paid_amount, 'total_order_amount' => $total_amount, 'total_paid_order'=> $total_paid_order,'total_revenue' => $total_revenue, 'paid_revenue'=> $paid_revenue, 'due_revenue'=>$due_revenue];
        return response()->json($revenue);
    }

    /**
     * @return JsonResponse
     */
    public function provider_revenue(Request $request): JsonResponse
    {
        if (!$request->exists('time') || $request->input('time') == 'all'){
            $query = Order::where('assigned_to', auth()->user()->id)->where('payment_status','COMPLETED');
            $total_amount =Order::where('assigned_to', auth()->user()->id)->sum('amount');
            $paid_revenue =  Revenue::where('provider_id', auth()->user()->id)->sum('paid_amount');
        } elseif($request->input('time') == 'this' || $request->input('time') == 'prev'){
            $month = date('m');
            if ($request->input('time') == 'prev') {
                $month = Carbon::now()->subMonth()->format('m');
            }
            $query = Order::whereMonth('created_at', $month)->where('assigned_to', auth()->user()->id)->where('payment_status','COMPLETED');
            $total_amount =Order::whereMonth('created_at', $month)->where('assigned_to', auth()->user()->id)->sum('amount');
            $paid_revenue =  Revenue::whereMonth('created_at', $month)->where('provider_id', auth()->user()->id)->sum('paid_amount');
        }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';

            $query = Order::whereBetween('created_at', [$start_date, $end_date])->where('assigned_to', auth()->user()->id)->where('payment_status','COMPLETED');
            $total_amount =Order::whereBetween('created_at', [$start_date, $end_date])->where('assigned_to', auth()->user()->id)->sum('amount');
            $paid_revenue =  Revenue::whereBetween('created_at', [$start_date, $end_date])->where('provider_id', auth()->user()->id)->sum('paid_amount');
        }



        $total_paid_amount  = $query->sum('amount');
        $total_paid_order = $query->count();
        $total_revenue = $total_paid_order * 40;
        $due_revenue = $total_revenue-$paid_revenue;


        $revenue= ['paid_order_amount'=>$total_paid_amount, 'total_order_amount' => $total_amount, 'total_paid_order'=> $total_paid_order,'total_revenue' => $total_revenue, 'paid_revenue'=> $paid_revenue, 'due_revenue'=>$due_revenue];
        return response()->json($revenue);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 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  \App\Models\Revenue  $revenue
     * @return \Illuminate\Http\Response
     */
    public function show(Revenue $revenue)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Revenue  $revenue
     * @return \Illuminate\Http\Response
     */
    public function edit(Revenue $revenue)
    {
        //
    }

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

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