<?php

namespace App\Http\Resources\Tenant;

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

class TaskCollectionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'slug' => $this->slug,
            'name' => $this->name,
            'name_translations' => $this->name_translations,
            'description' => $this->description,
            'description_translations' => $this->description_translations,
            'tenant_id' => $this->tenant_id,
            'is_global' => $this->tenant_id === null,
            'event_type' => $this->whenLoaded('eventType', function () {
                return [
                    'id' => $this->eventType->id,
                    'slug' => $this->eventType->slug,
                    'name' => $this->eventType->name,
                    'icon' => $this->eventType->icon,
                ];
            }),
            'tasks_count' => $this->whenCounted('tasks'),
            'events_count' => $this->whenCounted('events'),
            'imports_count' => $this->whenCounted('events'),
            'is_default' => (bool) ($this->is_default ?? false),
            'is_mine' => $this->tenant_id !== null,
            'position' => $this->position,
            'source_collection_id' => $this->source_collection_id,
            'created_at' => $this->created_at?->toISOString(),
            'updated_at' => $this->updated_at?->toISOString(),
        ];
    }
}
