<?php

namespace App\Http\Resources;

use App\Classes\Modules\Transactions\Services\CalculatesBillGroupPaymentAmount;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class BillGroupResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $billGroupPayment = (App()->make(CalculatesBillGroupPaymentAmount::class))->execute($this->resource);
        $bill_refund_amount = $billGroupPayment['bill_refund_amount'];
        $floating_amount = $billGroupPayment['floating_amount'];
        $paid_amount = $billGroupPayment['paid_amount'];
        $outstanding_amount = $billGroupPayment['outstanding_amount'];

        return [
            'id' => $this->id,
            'reference' => $this->reference,
            'original_amount' => (float) $this->original_amount,
            'original_currency' => new CurrencyResource($this->original_currency),
            'issuer_name' => $this->issuerCompany->name,
            'issuer_id' => $this->issuerCompany->id,
            'invoice_amount' => (float) $this->amount + $bill_refund_amount - $this->service_charge,
            'amount' => (float) $this->amount,
            'service_charge' => (float) $this->service_charge,
            'currency' => new CurrencyResource($this->currency),
            'created_at' => Carbon::parse($this->created_at)->format('d-m-Y h:i:s A'),
            'updated_at' => Carbon::parse($this->updated_at)->format('d-m-Y h:i:s A'),
            'currency_rate' => (float) $this->currency_rate,
            'status' => $this->status,
            'groups' => GroupResource::collection($this->groups),
            'bill_refund_amount' => $bill_refund_amount,
            'floating_amount' => $floating_amount,
            'paid_amount' => $paid_amount,
            'outstanding_amount' => $outstanding_amount,
            'payment_history' => $this->transactions->map(function ($transaction) {
                return [
                    'id' => $transaction->id,
                    'type' => (int) $transaction->type,
                    'bill_no' => $transaction->bill_no,
                    'payment_method' => (float) $transaction->payment_method,
                    'amount' => (float) $transaction->amount,
                    'original_amount' => (float) $transaction->original_amount,
                    'currency' => new CurrencyResource($transaction->currency),
                    'original_currency' => new CurrencyResource($transaction->original_currency),
                    'service_charge' => (float) $transaction->service_charge,
                    'tax' => (float) $transaction->tax,
                    'status' => (int) $transaction->status,
                    'statusText' => ApprovalStatus::APPROVAL_STATUS_ID[(int) $transaction->status],
                    'documents' => $transaction->documents()->first() ? new DocumentResource($transaction->documents()->first()) : null,
                    'updated_at' => Carbon::parse($transaction->updated_at)->format('d-m-Y h:i:s A'),
                ];
            }),
            'bill_refunds' => $this->billRefunds->map(function ($transaction) {
                return [
                    'id' => $transaction->id,
                    'type' => (int) $transaction->type,
                    'bill_no' => $transaction->bill_no,
                    'payment_method' => (float) $transaction->payment_method,
                    'amount' => (float) $transaction->amount,
                    'original_amount' => (float) $transaction->original_amount,
                    'currency' => new CurrencyResource($transaction->currency),
                    'original_currency' => new CurrencyResource($transaction->original_currency),
                    'service_charge' => (float) $transaction->service_charge,
                    'tax' => (float) $transaction->tax,
                    'status' => (int) $transaction->status,
                    'statusText' => ApprovalStatus::APPROVAL_STATUS_ID[(int) $transaction->status],
                    'updated_at' => Carbon::parse($transaction->updated_at)->format('d-m-Y h:i:s A'),
                ];
            }),
        ];
    }
}
