<?php

namespace App\Http\Resources;

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

class ContentMigrationEntryResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $data = [
            'id' => $this->id,
            'original_url' => $this->original_url,
            'original_parent' => $this->original_parent,
            'original_sub' => $this->original_sub,
            'original_sub_sub' => $this->original_sub_sub,
            'resolved_new_url' => $this->resolved_new_url,
            'new_url' => $this->new_url,
            'franchise' => $this->franchise ? [
                'id' => $this->franchise->id,
                'name' => $this->franchise->name,
            ] : null,
            'record_type' => $this->record_type,
            'record' => $this->record_id ? [
                'id' => $this->record_id,
                'name' => $this->record?->name ?? $this->record?->title ?? null,
            ] : null,
            'template' => $this->template ? [
                'id' => $this->template->id,
                'name' => $this->template->name,
            ] : null,
            'category' => $this->category,
            'status' => $this->status,
            'redirect_url' => $this->redirect_url,
            'text_migrated' => $this->text_migrated,
            'imagery_migrated' => $this->imagery_migrated,
            'metas_ready' => $this->metas_ready,
            'target_review_date' => $this->target_review_date?->toDateString(),
            'checked' => $this->checked,
            'priority' => $this->priority,
            'owner' => $this->owner,
            'pageviews' => $this->pageviews,
            'avg_engagement' => $this->avg_engagement !== null ? (float) $this->avg_engagement : null,
            'key_events' => $this->key_events,
            'website_section' => $this->website_section,
            'comments' => $this->comments,
            'data' => $this->data,
            'page_id' => $this->page_id,
            'processed_at' => $this->processed_at?->toIso8601String(),
            'process_notes' => $this->process_notes,
        ];

        // Only include content_mapping on single-entry responses to avoid bloating list
        if ($request->has('entry_id') || $request->routeIs('*update*') || $request->routeIs('*.content-migration.show')) {
            $data['content_mapping'] = $this->content_mapping;
        }

        return $data;
    }
}
