<?php

namespace App\Http\Resources;

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

class ListingDetailResource 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,

            'version' => $this->version,

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

            'location' => [
                'country' => $this->country,
                'city' => $this->city,
                'district' => $this->district,
                'neighborhood' => $this->neighborhood,
                'address' => $this->address,
                'latitude' => $this->latitude,
                'longitude' => $this->longitude,
            ],

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

            'status' => $this->status,
            'deal_status' => $this->deal_status,
            'is_featured' => $this->is_featured,
            'view_count' => $this->view_count,
            'published_at' => $this->published_at?->toISOString(),

            'category' => $this->whenLoaded('category', function () {
                return [
                    'id' => $this->category->id,
                    'name' => $this->category->name,
                    'slug' => $this->category->slug,
                    'description' => $this->category->description,
                ];
            }),

            'consultant' => $this->whenLoaded('consultantProfile', function () {
                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,
                    'bio' => $this->consultantProfile->bio,
                    'profile_image' => $this->consultantProfile->profile_image,
                    'cover_image' => $this->consultantProfile->cover_image,
                    'city' => $this->consultantProfile->city,
                    'district' => $this->consultantProfile->district,
                    'website_url' => $this->consultantProfile->website_url,
                    'instagram_url' => $this->consultantProfile->instagram_url,
                    'linkedin_url' => $this->consultantProfile->linkedin_url,
                    'is_verified' => $this->consultantProfile->is_verified,
                    // "Uzmana e-posta" ilgili danışmana gitsin (hesap e-postası).
                    'email' => $this->consultantProfile->relationLoaded('user')
                        ? $this->consultantProfile->user?->email
                        : null,
                    // Danışmanın bağlı olduğu firma (agency); bağımsız danışmanda null.
                    'company' => $this->consultantProfile->relationLoaded('company') && $this->consultantProfile->company
                        ? [
                            'id' => $this->consultantProfile->company->id,
                            'name' => $this->consultantProfile->company->name,
                            'slug' => $this->consultantProfile->company->slug,
                            'logo' => $this->consultantProfile->company->logo,
                            'is_verified' => $this->consultantProfile->company->is_verified,
                            // "Listed by" firma bloğu için ek bilgiler.
                            'description' => $this->consultantProfile->company->description,
                            'address' => $this->consultantProfile->company->address,
                            'city' => $this->consultantProfile->company->city,
                            'district' => $this->consultantProfile->company->district,
                            'country' => $this->consultantProfile->company->country,
                        ]
                        : null,
                ];
            }),

            'media' => $this->whenLoaded('media', function () {
                return $this->media->map(function ($media) {
                    return [
                        'id' => $media->id,
                        'file_path' => $media->file_path,
                        'file_url' => $media->file_url,
                        'alt_text' => $media->alt_text,
                        'mime_type' => $media->mime_type,
                        'sort_order' => $media->sort_order,
                        'is_cover' => $media->is_cover,
                    ];
                })->values();
            }),

            // İlan videoları (R2'de; sıralı). Yüklü değilse anahtar hiç dönmez.
            'videos' => $this->whenLoaded('videos', function () {
                return $this->videos->map(function ($video) {
                    return [
                        'id' => $video->id,
                        'file_url' => $video->file_url,
                        'mime_type' => $video->mime_type,
                        'size' => $video->size,
                        'duration_seconds' => $video->duration_seconds,
                        'title' => $video->title,
                        'sort_order' => $video->sort_order,
                        'created_at' => $video->created_at?->toISOString(),
                    ];
                })->values();
            }),

            'features' => $this->whenLoaded('featureValues', function () {
                return $this->featureValues->map(function ($featureValue) {
                    return [
                        'id' => $featureValue->id,

                        'feature_id' => $featureValue->feature?->id,
                        'name' => $featureValue->feature?->name,
                        'key' => $featureValue->feature?->key,
                        'input_type' => $featureValue->feature?->input_type,
                        'unit' => $featureValue->feature?->unit,

                        'value' => $featureValue->value,
                        'value_label' => $this->localizedValueLabel($featureValue),

                        'value_text' => $featureValue->value_text,
                        'value_number' => $featureValue->value_number,
                        'value_boolean' => $featureValue->value_boolean,
                        'value_json' => $featureValue->value_json,
                    ];
                })->values();
            }),

            'meta' => $this->meta,
        ];
    }
}