<?php

namespace App\Http\Resources\OrderManagement;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class OrderResource extends JsonResource{
    /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array|Arrayable|\JsonSerializable
     */
    public function toArray($request){
        $payment_usages = $this->paymentUsages()->with('payment')->get();
        $status_changes = $this->statusChanges;
        return [
            "id" => $this->id,
            "ref_id" => $this->ref_id,
            "customer_id" => $this->customer_id,
            "address_id" => $this->address_id,
            "shipping_company_id" => $this->shipping_company_id,
            "point_of_sale_id" => $this->point_of_sale_id,
            "warehouse_id" => $this->warehouse_id,
            "period" => $this->period,
            "discount_code" => $this->discount_code,
            "discount" => $this->discount,
            "subtotal" => $this->subtotal,
            "shipping" => $this->shipping,
            "tax" => $this->tax,
            "total" => $this->total,
            "reason" => $this->reason,
            "currency" => $this->currency,
            "status_code" => $this->status_code,
            "note" => $this->note,
            "editable" => $this->editable,
            "created_at" => format_date($this->created_at,__("Y-m-d H:i:s")),
            "updated_at" => format_date($this->updated_at,__("Y-m-d H:i:s")),
            "payment_usages" => $this->when(filled($payment_usages),
                $payment_usages->map(fn ($item)=>
                                    [
                                        "payment" => $item->payment->only("id","provider_reference",'internal_reference',"paid_amount"),
                                        "method" =>  $item->payment->method->only('id','code',"name",'logo_file_name','is_momo'),
                                        "amount_used" => $item->amount_used,
                                        "deleted" => $item->deleted,
                                    ]
                                )),
            "status_changes" => $this->when(filled($status_changes),
                                $status_changes->map(fn ($item)=>
                                [
                                    "createdBy" => $item?->createdBy?->only("id","full_name"),
                                    "status" =>  $item?->status?->only(['code',"name","badge_dot","badge_label"]),
                                    "created_at" => format_date($item->created_at,__("Y-m-d H:i:s")),
                                ]
                                )),

            "next_statuses" => $this->getNextStatuses()->map->only(['code',"name","badge_dot","badge_label"]),
            "items" => $this->items
                            ->map(fn ($item)=>
                                array_merge(
                                    $item->product->only("name","full_name",'ref',"category_id") ,
                                    [
                                        "product_id" => $item->product_id,
                                        "qty" => $item->quantity,
                                        "price" => $item->price,
                                        "amount" => $item->amount,
                                        "rejected" => $item->rejected,
                                    ]
                                )
                            )
                            ->toArray() ,
            "created_by" => $this?->createdBy?->only(['id',"full_name"]),
            "point_of_sale" => $this?->pointOfSale?->only(['id',"name","code"]),
            "warehouse" => $this?->warehouse?->only(['id',"name","code"]),
            "status" => $this?->status?->only(['code',"name","badge_dot","badge_label"]),
            "customer" => $this?->customer?->only(['id',"full_name","email",'phone']),

        ];
    }
}
