<?php

namespace App\Helpers;

use App\Models\FormEntertaimentPresentation;
use App\Models\FormMeetingSeminar;
use App\Models\FormOthers;
use App\Models\FormUpCountry;
use App\Models\FormVehicleRunningCost;
use Illuminate\Support\Facades\DB;
use App\Models\UserHasCabang;

class FormHelper
{
    public static function getFormsByUserIds($userIds, $month, $year) {
		$formUpCountryQuery = FormUpCountry::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Up Country' as type")
		)->whereIn('user_id', $userIds)
		->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Vehicle Running Cost' as type")
		)->whereIn('user_id', $userIds)
		->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Entertainment Presentation' as type")
		)->whereIn('user_id', $userIds)
		->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		$formMeetingSeminarQuery = FormMeetingSeminar::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Meeting Seminar' as type")
		)->whereIn('user_id', $userIds)
		->whereMonth('created_at', '=', date('m', strtotime($month)))
		->whereYear('created_at', '=', $year);

		$formOthersQuery = FormOthers::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Others' as type")
		)->whereIn('user_id', $userIds)
		->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		// Use union before fetching the data
		$forms = $formUpCountryQuery
			->union($formVehicleRunningCostQuery)
			->union($formEntertaimentPresentationQuery)
			->union($formMeetingSeminarQuery)
			->union($formOthersQuery)
			->get();

		return $forms;
	}

	public static function getAllForms($month, $year) {
		$formUpCountryQuery = FormUpCountry::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Up Country' as type")
		)->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		$formVehicleRunningCostQuery = FormVehicleRunningCost::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Vehicle Running Cost' as type")
		)->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		$formEntertaimentPresentationQuery = FormEntertaimentPresentation::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Entertainment Presentation' as type")
		)->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		$formMeetingSeminarQuery = FormMeetingSeminar::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Meeting Seminar' as type")
		)->whereMonth('created_at', '=', date('m', strtotime($month)))
		->whereYear('created_at', '=', $year);

		$formOthersQuery = FormOthers::select(
			'expense_number',
			'user_id',
			'total',
			'approved_total',
			'status',
			'account_number',
			'created_at',
			DB::raw("'Others' as type")
		)->whereMonth('tanggal', '=', date('m', strtotime($month)))
		->whereYear('tanggal', '=', $year);

		// Use union before fetching the data
		$forms = $formUpCountryQuery
			->union($formVehicleRunningCostQuery)
			->union($formEntertaimentPresentationQuery)
			->union($formMeetingSeminarQuery)
			->union($formOthersQuery)
			->get();

		return $forms;
	}

	public static function getNominalByFormType($cabang_id, $type, $type_id, $month, $year) {
		$users = UserHasCabang::where('cabang_id', $cabang_id)->get();
		$userIds = $users->pluck('user_id')->toArray();
		
		if($type == 'Up Country') {
			$form = FormUpCountry::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year);
		} else if($type == 'Vehicle Running Cost') {
			$form = FormVehicleRunningCost::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year);
		} else if($type == 'Entertainment') {
			$form = FormEntertaimentPresentation::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year);
		} else if($type == 'Meeting / Seminar') {
			$form = FormMeetingSeminar::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('created_at', '=', date('m', strtotime($month)))->whereYear('created_at', '=', $year);
		} else {
			$form = FormOthers::select('approved_total')->whereIn('user_id', $userIds)->whereMonth('tanggal', '=', date('m', strtotime($month)))->whereYear('tanggal', '=', $year)->where('kategori_id', $type_id);
		}


		$nominal = $form->sum('approved_total');
		return $nominal;
	}
}
