<?php

namespace App\Domains\Tenant\Resources;

use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;

/**
 * @mixin Tenant
 */
class TenantResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $heroConfig = $this->hero_config;
        if (is_array($heroConfig)) {
            $heroConfig['background_image'] = $this->heroBgImage?->getTemporaryUrl(1440);
            unset($heroConfig['background_image_id']);
        }

        return [
            'id' => $this->id,
            'codigo' => $this->codigo,
            'nombre' => $this->nombre,
            'dominio' => $this->dominio,
            'primary_color' => $this->primary_color,
            'secondary_color' => $this->secondary_color,
            'danger_color' => $this->danger_color,
            'success_color' => $this->success_color,
            'header_bg_color' => $this->header_bg_color,
            'footer_bg_color' => $this->footer_bg_color,
            // 1 day
            'header_logo' => $this->headerLogo?->getTemporaryUrl(1440),
            'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440),
            'hero_config' => $heroConfig,
            'event_config' => $this->event_config,
            'main_carousel_images' => $this->whenLoaded(
                'mainCarouselImages',
                fn () => $this->mainCarouselImages
                    ->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
                    ->values()
            ),
            'social_media' => $this->whenLoaded(
                'socialMedia',
                fn () => $this->socialMedia
                    ->map(fn ($socialMedia) => [
                        'code' => $socialMedia->code,
                        'icon' => $socialMedia->icon,
                        'name' => $socialMedia->name,
                        'url' => $socialMedia->pivot->url,
                    ])
                    ->values()
            ),
            'menues' => $this->whenLoaded(
                'menues',
                fn () => $this->menuTree($this->menues)
            ),
        ];
    }

    /**
     * @param  Collection<int, Menu>  $menus
     * @return Collection<int, array<string, mixed>>
     */
    private function menuTree(Collection $menus): Collection
    {
        $menuCodes = $menus->pluck('code')->flip();
        $childrenByParent = $menus
            ->filter(fn (Menu $menu) => $menu->parent_menu_code !== null)
            ->groupBy('parent_menu_code');

        $formatMenu = function (Menu $menu) use (&$formatMenu, $childrenByParent): array {
            $formatted = [
                'id' => $menu->id,
                'code' => $menu->code,
                'label' => $menu->label,
                'parent_menu_code' => $menu->parent_menu_code,
                'content_type' => $menu->content_type,
                'route' => $menu->route,
            ];

            if ($menu->pivot?->static_content !== null) {
                $formatted['static_content'] = $menu->pivot->static_content;
            }

            $formatted['submenues'] = $childrenByParent
                ->get($menu->code, collect())
                ->map($formatMenu)
                ->values();

            return $formatted;
        };

        return $menus
            ->filter(fn (Menu $menu) => $menu->parent_menu_code === null
                || ! $menuCodes->has($menu->parent_menu_code))
            ->map($formatMenu)
            ->values();
    }
}
