<?php

namespace App\Http\Resources;

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

class CustomerResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $array = [
            'id' => $this->id,
            'name' => $this->name,
            'mobile' => $this->mobile,
            'notes' => $this->notes,
            'bank_accounts' => $this->bank_accounts,
            'is_active' => $this->is_active,
            'is_featured' => (bool) ($this->is_featured ?? false),
            'created_at' => $this->created_at?->toISOString(),
            'updated_at' => $this->updated_at?->toISOString(),
        ];

        if (isset($this->money_debt)) {
            $array['money_debt'] = $this->money_debt;
            $array['money_credit'] = $this->money_credit ?? 0;
        }
        if (isset($this->metal_debt_grams)) {
            $array['metal_debt_grams'] = $this->metal_debt_grams;
            $array['metal_credit_grams'] = $this->metal_credit_grams ?? 0;
        }
        if (isset($this->metal_balances)) {
            $array['metal_balances'] = $this->metal_balances;
        }

        return $array;
    }
}
