<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\General\Eloquent\Filters\PaymentMethod;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\Modules\Transactions\Services\FetchesGroup;
use App\Classes\Modules\Transactions\Services\DeletesTransaction;
use App\Classes\Modules\Transactions\Services\DeletesGroup;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class DeletePaidGroupLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Delete Paid Group Transaction',
            'message' => 'You have successfully deleted this Group Transaction'
        ];
    }

    /** @var FetchesGroup */
    private $fetchesGroup;

    /** @var DeletesGroup */
    private $deletesGroup;

    /**
     * DeleteGroupLogic constructor.
     * @param updatesTransactionStatus $updatesTransactionStatus
     * @param FetchesGroup $fetchesGroup
     * @param DeletesTransaction $deletesTransaction
     */
    public function __construct(FetchesGroup $fetchesGroup, DeletesGroup $deletesGroup)
    {
        $this->fetchesGroup = $fetchesGroup;
        $this->deletesGroup = $deletesGroup;
    }

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

        foreach ($group->groupTransactions as $groupTransaction) {
            $invoice = $groupTransaction->transaction;
            $paymentTransaction = $invoice->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->first();

            if ($paymentTransaction->payment_method == PaymentMethodType::WALLET) {
                // Store the transaction ID before deleting
                $transactionId = Transaction::where('bill_no', $paymentTransaction->payment_reference)->first()->id;

                // Delete the transaction
                Transaction::where('id', $transactionId)->first()->delete();

                // Now you have the transaction ID available in $transactionId
                Log::channel('deletePaidGroupOrder')->info("Deleted payment reference transaction ID: " . $transactionId);
            } else {
                Log::channel('deletePaidGroupOrder')->info("no transaction found. Invoice Id ->" . $invoice->id);
            }

            // Store the transaction ID before deleting
            $transactionId = $paymentTransaction->id;

            // Delete the payment transaction
            $paymentTransaction->delete();
            Log::channel('deletePaidGroupOrder')->info("Deleted payment transaction ID: " . $transactionId);
        }

        Log::channel('deletePaidGroupOrder')->info("Deleted Group -> " . $group->id);
        $this->deletesGroup->execute($group);

        return $this->response([]);
    }
}
