<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Mtc\MercuryDataModels\OfferFinanceFieldConfig;

class OfferFinanceFieldConfigList extends JsonResource
{
    public static $wrap = '';

    /**
     * @return array<string, mixed>
     */
    #[\Override]
    public function toArray(Request $request): array
    {
        return [
            'data' => $this->resource
                ->map(fn (OfferFinanceFieldConfig $config) => $this->mapRecord($config))
                ->values()
                ->all(),
            'types' => self::types(),
        ];
    }

    /**
     * @return array<int, array{name: string, value: string}>
     */
    public static function types(): array
    {
        return [
            ['name' => 'Text', 'value' => 'text'],
            ['name' => 'Number', 'value' => 'number'],
            ['name' => 'Currency', 'value' => 'currency'],
        ];
    }

    /**
     * @return array<string, mixed>
     */
    protected function mapRecord(OfferFinanceFieldConfig $config): array
    {
        return [
            'id' => $config->id,
            'name' => $config->label,
            'description' => $config->key,
            'metaItems' => [
                'type' => $config->type,
                'suffix' => $config->suffix,
                'position' => $config->position,
            ],
        ];
    }
}
