<?php

namespace App\Http\Resources;

use App\Http\Resources\Concerns\FormatsFeatureValues;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class AdminListingResource extends JsonResource
{
    use FormatsFeatureValues;

    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'short_description' => $this->short_description,
            'description' => $this->description,

            'price' => $this->price,
            'currency' => $this->currency,

            'status' => $this->status,
            'deal_status' => $this->deal_status,
            'rejection_reason' => $this->rejection_reason,
            'is_featured' => $this->is_featured,
            'view_count' => $this->view_count,

            'location' => [
                'country' => $this->country,
                'city' => $this->city,
                'district' => $this->district,
                // Admin düzenleme formu bu alanları da doldurur.
                'neighborhood' => $this->neighborhood,
                'address' => $this->address,
            ],

            'area' => [
                'gross_area' => $this->gross_area,
                'net_area' => $this->net_area,
            ],

            'category' => $this->whenLoaded('category', fn () => [
                'id' => $this->category->id,
                'name' => $this->category->name,
                'slug' => $this->category->slug,
            ]),

            'consultant' => $this->whenLoaded('consultantProfile', function () {
                if (! $this->consultantProfile) {
                    return null;
                }

                return [
                    'id' => $this->consultantProfile->id,
                    'company_name' => $this->consultantProfile->company_name,
                    'slug' => $this->consultantProfile->slug,
                    'phone' => $this->consultantProfile->phone,
                    'whatsapp_phone' => $this->consultantProfile->whatsapp_phone,
                    'profile_image' => $this->consultantProfile->profile_image,
                    'city' => $this->consultantProfile->city,
                    'district' => $this->consultantProfile->district,
                    'license_number' => $this->consultantProfile->license_number,
                    'is_verified' => $this->consultantProfile->is_verified,
                    'is_active' => $this->consultantProfile->is_active,
                    'user' => $this->consultantProfile->relationLoaded('user') && $this->consultantProfile->user
                        ? [
                            'id' => $this->consultantProfile->user->id,
                            'name' => $this->consultantProfile->user->name,
                            'email' => $this->consultantProfile->user->email,
                        ]
                        : null,
                ];
            }),

            'cover_image' => $this->whenLoaded('coverImage', function () {
                if (! $this->coverImage) {
                    return null;
                }

                return [
                    'id' => $this->coverImage->id,
                    'file_url' => $this->coverImage->file_url,
                    'alt_text' => $this->coverImage->alt_text,
                ];
            }),

            'media' => $this->whenLoaded('media', fn () => $this->media->map(fn ($m) => [
                'id' => $m->id,
                'file_url' => $m->file_url,
                'alt_text' => $m->alt_text,
                'is_cover' => $m->is_cover,
                'sort_order' => $m->sort_order,
            ])->values()),

            'features' => $this->whenLoaded('featureValues', fn () => $this->featureValues->map(fn ($fv) => [
                'name' => $fv->feature?->name,
                'key' => $fv->feature?->key,
                'input_type' => $fv->feature?->input_type,
                'unit' => $fv->feature?->unit,
                'value' => $fv->value,
                'value_label' => $this->localizedValueLabel($fv),
            ])->values()),

            'media_count' => $this->whenCounted('media'),

            // İlan videoları (admin düzenleme formu yönetir).
            'videos' => $this->whenLoaded('videos', fn () => $this->videos->map(fn ($v) => [
                'id' => $v->id,
                'file_url' => $v->file_url,
                'mime_type' => $v->mime_type,
                'size' => $v->size,
                'duration_seconds' => $v->duration_seconds,
                'title' => $v->title,
                'sort_order' => $v->sort_order,
                'created_at' => $v->created_at?->toISOString(),
            ])->values()),

            'created_at' => $this->created_at?->toISOString(),
            'published_at' => $this->published_at?->toISOString(),

            'version' => $this->version
        ];
    }
}
