<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ReturnResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'company_id' => $this->company_id,
            'branch_id' => $this->branch_id,
            'original_order_id' => $this->original_order_id,
            'return_order_id' => $this->return_order_id,
            'user_id' => $this->user_id,
            'customer_id' => $this->customer_id,
            'return_number' => $this->return_number,
            'reason' => $this->reason,
            'reason_notes' => $this->reason_notes,
            'status' => $this->status,
            'refund_method' => $this->refund_method,
            'total_refund_amount' => $this->total_refund_amount,
            'restocked' => $this->restocked,
            'device_id' => $this->device_id,
            'synced_at' => $this->synced_at,
            'created_at' => $this->created_at,
            'items' => $this->items->map(fn ($item) => [
                'id' => $item->id,
                'order_item_id' => $item->order_item_id,
                'product_id' => $item->product_id,
                'variant_id' => $item->variant_id,
                'quantity' => $item->quantity,
                'unit_price' => $item->unit_price,
                'refund_amount' => $item->refund_amount,
                'condition' => $item->condition,
            ]),
            'original_order' => $this->whenLoaded('originalOrder', fn () => $this->originalOrder?->only(['id', 'order_number', 'status'])),
            'customer' => $this->whenLoaded('customer', fn () => $this->customer?->only(['id', 'name', 'phone'])),
        ];
    }
}
