<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Transactions\Services\CreatesTransactionDetail;
use App\Classes\Modules\Transactions\Services\DeletesTransactionDetails;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\Services\UpdatesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Http\Resources\TransactionResource;
use App\Models\Booking;
use App\Models\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreatePurchaseOrderTransactionLogic extends AbstractControllerLogic
{
    /**
     * CreatePurchaseOrderTransactionLogic constructor.
     * @param FetchesBooking $fetchesBooking
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param CreatesTransaction $createsTransaction
     * @param CreatesTransactionDetail $createsTransactionDetail
     * @param UpdatesTransaction $updatesTransaction
     * @param DeletesTransactionDetails $deletesTransactionDetails
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     */
    public function __construct(FetchesBooking $fetchesBooking, UpdatesTransactionStatus $updatesTransactionStatus, CreatesTransaction $createsTransaction, CreatesTransactionDetail $createsTransactionDetail, UpdatesTransaction $updatesTransaction, DeletesTransactionDetails $deletesTransactionDetails, GeneratesTransactionBillNumber $generatesTransactionBillNumber)
    {
        $this->fetchesBooking = $fetchesBooking;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->createsTransaction = $createsTransaction;
        $this->createsTransactionDetail = $createsTransactionDetail;
        $this->updatesTransaction = $updatesTransaction;
        $this->deletesTransactionDetails = $deletesTransactionDetails;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
    }

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Update Purchase Order',
            'message' => 'You have successfully updated you booking\'s purchase order'
        ];
    }

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

    /** @var UpdatesTransactionStatus */
    private $updatesTransactionStatus;

    /** @var CreatesTransaction */
    private $createsTransaction;

    /** @var CreatesTransactionDetail */
    private $createsTransactionDetail;

    /** @var UpdatesTransaction */
    private $updatesTransaction;

    /** @var DeletesTransactionDetails */
    private $deletesTransactionDetails;

    /** @var GeneratesTransactionBillNumber */
    private $generatesTransactionBillNumber;


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        /** @var Booking $booking */
        $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]);

        /** @var Transaction $transaction */
        $transaction = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first();

        $billNumber = $this->generatesTransactionBillNumber->execute('PO-');

        $total = collect($request->input('products'))->sum(function($product){
            return $product['quantity'] * floatval(str_replace(',', '', $product['unit_price']));
        });

        $object = new TransactionObject($billNumber, TransactionType::PURCHASE_ORDER, $booking->company->id, 1,
            1, PaymentMethodType::CASH,
            $total, $total, $booking->fix_currency_id, $booking->fix_currency_id,
            1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, $request->input('products'));

        !$transaction ? $transaction = $this->createsTransaction->execute($booking, $object) : $transaction = $this->updatesTransaction->execute($transaction, $object);

        $this->updatesTransactionStatus->execute($transaction, (float)number_format((float)$total , 2, '.', '') === (float)number_format((float)$booking->fix_amount, 2, '.', '') ? ApprovalStatus::PENDING_VERIFICATION : ApprovalStatus::PENDING_SUBMISSION);

        $this->deletesTransactionDetails->execute($transaction);

        foreach ($object->getDetails() as $product){
            $this->createsTransactionDetail->execute($transaction, $product);
        }

        return $this->resourceResponse(new TransactionResource($transaction));

    }



}
