<?php

namespace App\Http\Resources;

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

class StockProductResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $product = $this->relationLoaded('product') ? $this->whenLoaded('product') : null;
        $warehouse = $this->relationLoaded('warehouse') ? $this->whenLoaded('warehouse') : null;

        return [
            'id' => $this->id,
            'stock_erp' => $this->stock_erp,
            'stock_system' => $this->stock_system,
            'registration_date' => $this->registration_date,
            'last_update' => $this->last_update,
            'idproduct' => $this->idproduct,
            $this->mergeWhen($product, fn() => [
                'product' => $product->name,
                'cost' => $product->cost
            ]),
            'idwarehouse' => $this->idwarehouse,
            $this->mergeWhen($warehouse, fn() => [
                'store' => $warehouse->name
            ]),
            'type_movement' => $this->type_movement,
            'movement' => $this->type_movement == 1 ? "Ingreso" : "Salida",
            'initial_stock' => $this->initial_stock,
            'difference' => $this->difference,
            'idcompany' => $this->idcompany,
            'status' => $this->status,
            'isAdjustment' => $this->isAdjustment,
            'folio' => $this->folio,
            'type_dte' => $this->type_dte,
            'date_dte' => $this->date_dte,
        ];
    }
}
