<?php

namespace App\Http\Resources;


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

class TodayTomorrowSimpleOrderResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request): array
    {
        $order = $this->order;
        $categoryId = $this->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'                           => $this->id,
            'order_id'                     => $this->order_id,
            'order_serial_no'              => $order->order_serial_no,
            'order_type'                   => $order->order_type,
            'order_datetime'               => AppLibrary::date($this->date),
            'total_amount_price'           => AppLibrary::flatAmountFormat($itemsTotal + $taxTotal + $deliveryCharge),
            "delivery_charge_amount_price" => AppLibrary::flatAmountFormat($deliveryCharge),
            'payment_method'               => $order->payment_method,
            'payment_status'               => $order->payment_status,
            'transaction'                  => new TransactionResource($order->transaction),
            'status'                       => $this->status,
            'status_name'                  => trans('orderStatus.' . $this->status),
            'order_items'                  => $orderItems->count(),
            'category_id'                  => $categoryId,
            'category_name'                => $this?->category->name,
            'customer_name'                => $order?->user->name,
            'source'                       => $order->source,
        ];
    }
}