<?php

namespace App\Http\Resources;

use App\Enums\Ask;
use App\Models\OrderTax;
use App\Models\DeliveryArea;
use App\Libraries\AppLibrary;
use Illuminate\Http\Resources\Json\JsonResource;

class TodayTomorrowOrderDetailsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request): array
    {
        $history = $this;
        $order = $this?->order;

        $categoryId = $history->order_item_category_id;
        $orderItems = $order->orderItems->where('item_category_id', $categoryId);
        $itemIds = $orderItems->pluck('item_id');

        $itemsTotal = $orderItems->sum('total_price');
        $taxTotal = OrderTax::where('order_id', $order->id)->whereIn('item_id', $itemIds)->sum('tax_amount');
        $deliveryCharge = DeliveryArea::whereKey($order->order_area_id)->value('delivery_charge');

        return [
            'id'                             => $history->id,
            'order_id'                       => $history->order_id,
            'order_serial_no'                => $order->order_serial_no,
            'token'                          => $order->token,
            "subtotal_currency_price"        => AppLibrary::currencyAmountFormat($itemsTotal),
            "delivery_charge_currency_price" => AppLibrary::currencyAmountFormat($deliveryCharge),
            "total_currency_price"           => AppLibrary::currencyAmountFormat($itemsTotal + $taxTotal + $deliveryCharge),
            "total_tax_currency_price"       => AppLibrary::currencyAmountFormat($taxTotal),
            'order_type'                     => $order->order_type,

            'order_datetime'                 => AppLibrary::datetime($order->order_datetime),
            'payment_method'                 => $order->payment_method,
            'payment_status'                 => $order->payment_status,
            'status'                         => $history->status,
            'status_name'                    => trans('orderStatus.' . $history->status),
            'reason'                         => $order->reason,

            'user'            => new UserResource($order->user),
            'order_address'   => new OrderAddressResource($order->address),
            'delivery_boy'    => new UserResource($this->delivery_boy_id ? $this->deliveryBoy : $order->deliveryBoy),
            'delivery_boy_id' => $this->delivery_boy_id ? $this->delivery_boy_id : $order->delivery_boy_id,
            'transaction'     => new TransactionResource($order->transaction),
            'order_items'     => OrderItemResource::collection($orderItems->values()),

            'category_name'      => $history?->category->name,
            'delivery_date'      => AppLibrary::date($history->date),
            'pos_payment_method' => $order->pos_payment_method,
            'pos_payment_note'   => $order->pos_payment_note,
            'source'             => $order->source,


            'pos_received_amount'           => $order->pos_received_amount,
            'pos_received_currency_amount'  => AppLibrary::currencyAmountFormat($order->pos_received_amount),
            'cash_back_amount'              => AppLibrary::convertAmountFormat($order->pos_received_amount - $order->total),
            'cash_back_currency_amount'     => AppLibrary::currencyAmountFormat($order->pos_received_amount - $order->total),
        ];
    }
}
