<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Bulkorder;
use App\Models\Order;
use Barryvdh\DomPDF\Facade\Pdf;
use Carbon\Carbon;
use Illuminate\Http\Request;

class revenueController extends Controller
{
    public function revenue_index(){
        $currentDate = Carbon::now();
        $lastYearDate = $currentDate->copy()->subMonths(12);
 ///////not compulsary
        $bulkOrderIds = Bulkorder::pluck('order_ids')
            ->flatten()
            ->map(function ($ids) {
                return collect(explode(',', trim($ids, '[]')))
                    ->map(function ($id) {
                        return trim($id);
                    })
                    ->toArray();
            })
            ->flatten()
            ->unique()
            ->toArray();

        $individualOrderRevenue = Order::selectRaw('DATE_FORMAT(created_at, "%Y-%m") as month, SUM(total) as revenue')
                                       ->where('created_at', '>=', $lastYearDate)
                                       ->where('cancellation', null)

                                       ->whereNotIn('order_id', $bulkOrderIds)
                                       ->groupBy('month')
                                       ->orderBy('month', 'asc')
                                       ->get()
                                       ->mapWithKeys(function($item) {
                                           return [$item->month => (float) $item->revenue];
                                       });

 ///////not compulsary
        $bulkOrderRevenue = Order::join('bulkorder', 'order.order_id', '=', 'bulkorder.order_ids')
                                 ->selectRaw('DATE_FORMAT(order.created_at, "%Y-%m") as month, SUM(bulkorder.total_price) as revenue')
                                 ->where('order.created_at', '>=', $lastYearDate)
                                 ->groupBy('month')
                                 ->orderBy('month', 'asc')
                                 ->get()
                                 ->mapWithKeys(function($item) {
                                     return [$item->month => (float) $item->revenue];
                                 });


                                 ///////not compulsary
        $monthlyRevenue = $individualOrderRevenue->mergeRecursive($bulkOrderRevenue)
                                                 ->map(function($revenue) {
                                                     return is_array($revenue) ? array_sum($revenue) : $revenue;
                                                 })
                                                 ->map(function($revenue, $month) {
                                                     return ['month' => $month, 'revenue' => $revenue];
                                                 })
                                                 ->values();

        $pdf = Pdf::loadView('admin.revenue.revenue_report', compact('monthlyRevenue'))
                  ->setOptions(['defaultFont' => 'sans-serif']);

        $filename = 'revenue.pdf';
        return $pdf->download($filename);
    }


}
