<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Bookings\Standards\Rules\CanFetchBooking;
use App\Classes\Modules\Bookings\Processors\RegenerateInvoiceBookingProcessor;
use App\Classes\Modules\Bookings\Processors\RegenerateInvoiceBookingV2Processor;
use App\Http\Resources\BookingResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use Illuminate\Support\Facades\Log;

class RegenerateInvoiceBookingLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Regenerate Booking Invoice',
            'message' => 'You have successfully regenerate booking invoice'
        ];
    }

    /** @var CanFetchBooking */
    private $canFetchBooking;

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

    /** @var RegenerateInvoiceBookingProcessor */
    private $regenerateInvoiceBookingProcessor;

    /** @var RegenerateInvoiceBookingV2Processor */
    private $regenerateInvoiceBookingV2Processor;

    /**
     * RegenerateInvoiceBookingLogic constructor.
     * @param CanFetchBooking $canFetchBooking
     * @param FetchesBooking $fetchesBooking
     * @param RegenerateInvoiceBookingProcessor $regenerateInvoiceBookingProcessor
     * @param RegenerateInvoiceBookingV2Processor $regenerateInvoiceBookingV2Processor
     */
    public function __construct(
        CanFetchBooking $canFetchBooking,
        FetchesBooking $fetchesBooking,
        RegenerateInvoiceBookingProcessor $regenerateInvoiceBookingProcessor,
        RegenerateInvoiceBookingV2Processor $regenerateInvoiceBookingV2Processor
    ) {
        $this->canFetchBooking = $canFetchBooking;
        $this->fetchesBooking = $fetchesBooking;
        $this->regenerateInvoiceBookingProcessor = $regenerateInvoiceBookingProcessor;
        $this->regenerateInvoiceBookingV2Processor = $regenerateInvoiceBookingV2Processor;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request): JsonResponse
    {
        $this->canFetchBooking->passes();

        $booking = $this->fetchesBooking->execute(
            [
                'id' => $request->route('id'),
                // 'status' => ApprovalStatus::COMPLETED,
                'with_transactions' => true
            ]
        );

        $eInvoiceWithNormalInvoiceTemplate = $request->input('normal_invoice', false);
        $eInvoiceRefund = $request->input('e_invoice_refund', false);
        $eInvoiceTry = $request->input('e_invoice_try', false);

        if(!$eInvoiceRefund && !$eInvoiceTry){
            if($booking->status !== ApprovalStatus::COMPLETED){
                throw new MalformedRequestException('Booking incomplete.');
            }
        }

        // if($eInvoiceRefund){
            $this->regenerateInvoiceBookingV2Processor->execute($booking, $eInvoiceWithNormalInvoiceTemplate, $eInvoiceRefund, $eInvoiceTry);
        // }
        // else{
        //     $this->regenerateInvoiceBookingProcessor->execute($booking, $eInvoiceWithNormalInvoiceTemplate);
        // }

        return $this->resourceResponse(new BookingResource($booking));
    }
}
