<?php

namespace App\Http\Controllers\Owner\Analytic;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;


class AnalyticSaleListYearController extends Controller
{
	private $oDomain;


	public function __construct
	(
		private LaravelDTOGenerator $oLaravelDTOGenerator
	)
	{
        $this->oDomain = "Analytic Sale List Year";
    }


	public function __invoke(Request $request)
    {
		//------------------------------------------------------------------------------
		//	VARIABLES
		//------------------------------------------------------------------------------
		$pYear	= date("Y");
		$pYears	= [($pYear - 2), ($pYear - 1), $pYear];


		//------------------------------------------------------------------------------
		//	FUNCTION
		//------------------------------------------------------------------------------
		$oSubQuery = DB::query();

		$oSubQuery->from("v_booking_sales");
		$oSubQuery->selectRaw("Id_CashMovementDetail, Id_Booking, Booking_Confirm_Year, Booking_Confirm_Month, MAX(CashMovementDetail_Amount) AS AmountUnique");
		$oSubQuery->whereIn("Booking_Confirm_Year", $pYears);
		$oSubQuery->groupBy("Id_CashMovementDetail");
		$oSubQuery->groupBy("Booking_Confirm_Year");
		$oSubQuery->groupBy("Booking_Confirm_Month");


		$oQuery = DB::query();

		$oQuery->fromSub($oSubQuery, "t");
		$oQuery->selectRaw("Booking_Confirm_Year AS Year, Booking_Confirm_Month AS Month, COUNT(DISTINCT Id_Booking) AS Total_Booking, SUM(AmountUnique) AS Total_Sale");
		$oQuery->groupBy("Booking_Confirm_Year");
		$oQuery->groupBy("Booking_Confirm_Month");
		$oQuery->orderBy("Booking_Confirm_Year", "DESC");
		$oQuery->orderBy("Booking_Confirm_Month", "ASC");

		$oData = $oQuery->get();


		//------------------------------------------------------------------------------
		//	MAP
		//------------------------------------------------------------------------------
		$oMonths	= collect(range(1, 12));
		$oData 		= $oData->groupBy("Year")
							->map(function ($oItems, $Year) use ($oMonths)
							{
								return [
									"Year" 		=> $Year,
									"Months"	=> $oMonths->map(function ($Month) use ($oItems)
									{
										$oRow = $oItems->firstWhere("Month", $Month);

										return [
											"Month"      	=> $Month,
											"Total_Booking"	=> $oRow->Total_Booking ?? 0,
											"Total_Sale" 	=> $oRow->Total_Sale ?? 0
										];
									})->values()
								];
							})
							->values();


		//------------------------------------------------------------------------------
		//	RESPONSE
		//------------------------------------------------------------------------------
		$oResponse["Response_Status"]           = 200;
		$oResponse["Response_Code"]             = 200;
		$oResponse["Response_Domain"]           = $this->oDomain;
		$oResponse["Response_Message"]          = $this->oDomain . " List";
		$oResponse["Response_Data"]             = $oData;
		$oResponse["Response_Error_Message"]    = "";
		$oResponse["Response_Error_Reason"]     = "";

		return ResponseBuilder::Response($oResponse);

    }


}