<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;

class InventorySOHResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $farmId  = Auth::user()->farm_id;
        $totalIn  = (float) ($this->total_in ?? 0);
        $totalOut = (float) ($this->total_out ?? 0);
        $soh      = $totalIn - $totalOut;

        return [
            'item_id'         => $this->id,
            'item_code'       => $this->code,
            'item_name'       => $this->name,
            'reorder_point'   => $this->reorder_point,
            'total_in'        => $totalIn,
            'total_out'       => $totalOut,
            'current_soh'     => $soh,
            'is_low_stock'    => $soh <= $this->reorder_point,
            'is_out_of_stock' => $soh <= 0,
            'category'        => $this->category ? [
                'id' => $this->category->id,
                'name' => $this->category->name,
            ] : null,
        ];
    }
}
