<?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 SimpleDeliveryBoyOrderResource 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::datetime($order->order_datetime),
            'order_date'           => AppLibrary::date($order->order_datetime),
            'order_time'           => AppLibrary::time($order->order_datetime),
            'delivery_date'        => AppLibrary::date($this->date),
            'payment_method'       => $order->payment_method,
            'payment_status'       => $order->payment_status,
            'order_items'          => $orderItems->count(),
            'status'               => $this->status,
            'status_name'          => trans('orderStatus.' . $this->status),
            'category_id'          => $categoryId,
            'category_name'        => $this?->category->name,
            'total_currency_price' => AppLibrary::currencyAmountFormat($itemsTotal + $taxTotal + $deliveryCharge),
            'customer_name'        => $order?->user->name,
            'order_address'        => new AddressResource($order?->address),
            'source'               => $order->source,
        ];
    }
}