<?php

namespace App\Modules\Dashboard\Resources;

use App\Modules\Dashboard\DTOs\ActionCenterWidgetData;
use App\Modules\Dashboard\DTOs\DashboardViewData;
use App\Modules\Dashboard\DTOs\ModuleCardData;
use App\Modules\Dashboard\DTOs\OperationalAlertItem;
use JsonSerializable;

/**
 * Transforms the dashboard DTO to a plain array (e.g. for JSON/telemetry).
 */
class DashboardViewResource implements JsonSerializable
{
    public function __construct(
        private readonly DashboardViewData $data,
    ) {}

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'plan_name' => $this->data->planName,
            'subscription_status_key' => $this->data->subscriptionStatusKey,
            'trial_days_left' => $this->data->trialDaysLeft,
            'is_trial' => $this->data->isTrial,
            'action_widgets' => array_map(
                static fn (ActionCenterWidgetData $w) => [
                    'key' => $w->key,
                    'title' => $w->title,
                    'count' => $w->count,
                    'severity' => $w->severity,
                    'action_label' => $w->actionLabel,
                    'action_url' => $w->actionUrl,
                    'icon' => $w->icon,
                ],
                $this->data->actionWidgets
            ),
            'attention_alerts' => array_map(
                static fn (OperationalAlertItem $a) => [
                    'id' => $a->id,
                    'title' => $a->title,
                    'body' => $a->body,
                    'severity' => $a->severity,
                    'priority' => $a->priority,
                    'action_url' => $a->actionUrl,
                    'action_label' => $a->actionLabel,
                ],
                $this->data->attentionAlerts
            ),
            'stat_cards' => $this->data->statCards,
            'modules' => array_map(
                static fn (ModuleCardData $m) => [
                    'slug' => $m->slug,
                    'name' => $m->name,
                    'description' => $m->description,
                    'icon' => $m->icon,
                    'enabled' => $m->enabled,
                    'href' => $m->href,
                ],
                $this->data->modules
            ),
        ];
    }

    public function jsonSerialize(): array
    {
        return $this->toArray();
    }
}
