<?php

namespace App\Classes\Modules\Rules\Standards\Rules;

use App\Classes\Exceptions\CriteriaNotFulfilledException;
use App\Classes\General\Abstracts\AbstractRule;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding;
use App\Classes\Modules\Bookings\Services\FetchesBookingQuotation;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyConversionObject;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class CanPassMustEInvoiceAmountLimitRule extends AbstractRule
{

    /** @var FetchesBooking */
    private $fetchesBooking;

    /** @var FetchesBookingQuotation */
    private $fetchesBookingQuotation;

    /** @var CalculatesBookingOutstanding */
    private $calculatesBookingOutstanding;


    /**
     * CanPassMustEInvoiceAmountLimitRule constructor.
     * @param FetchesBooking $fetchesBooking
     * @param FetchesBookingQuotation $fetchesBookingQuotation
     * @param CalculatesBookingOutstanding $calculatesBookingOutstanding
     */
    public function __construct(FetchesBooking $fetchesBooking, FetchesBookingQuotation $fetchesBookingQuotation, CalculatesBookingOutstanding $calculatesBookingOutstanding)
    {
        $this->fetchesBooking = $fetchesBooking;
        $this->fetchesBookingQuotation = $fetchesBookingQuotation;
        $this->calculatesBookingOutstanding = $calculatesBookingOutstanding;
    }

    /**
     * @return bool
     */
    protected function authorized($object): bool
    {
        return true;

    }

    /**
     * @return bool
     */
    protected function validators($object): bool
    {
        return true;

    }


    /**
     * @return bool
     */
    protected function criteria($object): bool
    {
        //Check if booking amount is RM10,000 or more, than must opt-in E-Invoice
        $booking = $this->fetchesBooking->execute(['id' => $object->bookingId]);
        $company = $booking->company;

        // $totalPayments = 0;

        // if(isset($object->amount)){
        //     $totalPayments =  $booking->transactions()->payments()->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('amount');

        //     $conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', floatval(str_replace(',', '', $object->amount)))), $booking->convertible_currency_id, $booking->service_id,  $booking->fix_currency_id === 1 ? 0:1, PaymentMethodType::PAYMENT_METHODS[$object->paymentMethod]);
        //     $voucherCode = $object->voucherCode ?? null;
        //     $configurations = $this->fetchesBookingQuotation->execute($booking->company, $conversionObject, $voucherCode, null, $booking);
        // }
        // else{
        //     $outstanding = $this->calculatesBookingOutstanding->execute($booking);
        //     $conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $outstanding)), $booking->convertible_currency_id, $booking->service_id, $booking->fix_currency_id === 1 ? 0 : 1, PaymentMethodType::CASH);
        //     $configurations = $this->fetchesBookingQuotation->execute($booking->company, $conversionObject);
        // }

        // $bookingAmountInMYR = $configurations->getTotal();
        // $bookingAmountInMYR = $bookingAmountInMYR + $totalPayments;

        // if($bookingAmountInMYR >= 10000 && (!$company->e_invoice || !$company->tin) ){
        //     throw new CriteriaNotFulfilledException("RM10,000 and above must opt-in for E-Invoice.");
        // }

        //1 MYR, 2 CNY, 3 USD
        if((($booking->fix_amount >= 9000 && $booking->fix_currency_id === 1) ||
            ($booking->fix_amount >= 12600 && $booking->fix_currency_id === 2) ||
            ($booking->fix_amount >= 1920 && $booking->fix_currency_id === 3))
            && (!$company->e_invoice || !$company->tin) ){
            throw new CriteriaNotFulfilledException("Booking amount above limit, must opt-in for E-Invoice.");
        }

        return true;
    }
}