<?php

namespace App\Http\Resources;

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

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

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

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

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

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

            '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,
                    'profile_image' => $this->consultantProfile->profile_image,
                    '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,
                        ]
                        : null,
                ];
            }),

            // Kart rozeti: ilanın videosu var mı (index withCount('videos') ile gelir).
            'has_video' => $this->whenCounted('videos', fn () => $this->videos_count > 0),

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

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

            '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();
            }),




            'is_featured' => $this->is_featured,
            'deal_status' => $this->deal_status,
            'published_at' => $this->published_at?->toISOString(),
        ];
    }
}
