<?php

namespace App\Domains\Tenant\Resources\AdminApp;

use App\Domains\Attachable\Models\Attachment;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin Tenant
 */
class WebsiteExtrasResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $websiteExtras = $this->websiteExtras->keyBy(
            fn ($extra) => $extra->websiteTypeExtra->codigo
        );

        return [
            'website_type' => $this->websiteType ? [
                'codigo' => $this->websiteType->codigo,
                'nombre' => $this->websiteType->nombre,
            ] : null,
            'definitions' => $this->websiteType?->extras
                ->mapWithKeys(fn ($definition) => [
                    $definition->codigo => [
                        'codigo' => $definition->codigo,
                        'nombre' => $definition->nombre,
                        'descripcion' => $definition->descripcion,
                        'is_required' => $definition->is_required,
                        'is_enabled' => $websiteExtras
                            ->get($definition->codigo)?->is_enabled,
                        'request_rules' => $definition->config_schema['request_rules'] ?? [],
                    ],
                ]) ?? [],
            'extras' => $websiteExtras->mapWithKeys(fn ($extra) => [
                $extra->websiteTypeExtra->codigo => $this->formatConfig(
                    $extra->resolvedConfig(),
                    fn (Attachment $attachment): string => $attachment->key
                ),
            ]),
            'resolved_extras' => $websiteExtras->mapWithKeys(fn ($extra) => [
                $extra->websiteTypeExtra->codigo => $this->formatConfig(
                    $extra->resolvedConfig(),
                    fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
                ),
            ]),
        ];
    }

    private function formatConfig(mixed $value, callable $formatAttachment): mixed
    {
        if ($value instanceof Attachment) {
            return $formatAttachment($value);
        }

        if (! is_array($value)) {
            return $value;
        }

        return array_map(
            fn (mixed $item): mixed => $this->formatConfig($item, $formatAttachment),
            $value
        );
    }
}
