<?php

namespace App\Http\Controllers\Api\PublicApi;

use App\Http\Controllers\Controller;
use App\Http\Resources\Category as CategoryResource;
use App\Models\Category;
use App\Models\ConsultantProfile;
use App\Models\Listing;
use App\Models\TeamMember;
use Illuminate\Http\Request;

class AboutPageController extends Controller
{
    public function index(Request $request)
    {
        $featured_categories = Category::query()
            ->featured()
            ->active()
            ->get();

        $team = TeamMember::query()
            ->active()
            ->get();

        $categories_featured_listings = [];

        foreach ($featured_categories as $categories) {
            $categories_featured_listings[$categories->slug] = Listing::query()
                ->where('category_id', $categories->id)
                ->published()
                ->featured()
                ->with(['coverImage','category'])
                ->limit(3)
                ->latest()
                ->get()
                ->map(fn($listing) => [
                    'id' => $listing->id,
                    'slug' => $listing->slug,
                    'title' => $listing->title,
                    'price' => $listing->price,
                    'currency' => $listing->currency,
                    'image' => $listing->coverImage?->file_url ?? $listing->coverImage?->file_path,
                    'location' => trim(($listing->city ?? '') . ', ' . ($listing->country ?? ''), ', '),
                ]);
        }


        $team_members_listings = [];

        // Ekip üyesi ile danışman SLUG üzerinden eşlenir (team_members.id ile
        // consultant_profiles.id farklı tablolardır; id ile eşlemek yanlış ilan gösterir).
        $consultantIdsBySlug = ConsultantProfile::whereIn('slug', $team->pluck('slug'))
            ->pluck('id', 'slug');

        foreach ($team as $member) {
            $consultantId = $consultantIdsBySlug[$member->slug] ?? null;

            $team_members_listings[$member->slug] = $consultantId === null
                ? collect()
                : Listing::query()
                ->where('consultant_profile_id', $consultantId)
                ->published()
                ->with(['coverImage'])
                ->limit(3)
                ->latest()
                ->get()
                ->map(fn($listing) => [
                    'id' => $listing->id,
                    'slug' => $listing->slug,
                    'title' => $listing->title,
                    'price' => $listing->price,
                    'currency' => $listing->currency,
                    'image' => $listing->coverImage?->file_url ?? $listing->coverImage?->file_path,
                    'location' => trim(($listing->city ?? '') . ', ' . ($listing->country ?? ''), ', '),
                ]);
        }


        return response()->json([
            'data' => [
                // Resource, translatable name/description'ı aktif locale'e çözer
                // (ham model döndürülürse spatie tüm çevirileri {en,tr} olarak verir).
                'featured_categories' => CategoryResource::collection($featured_categories),
                'categories_featured_listings' => $categories_featured_listings,
                'team_members' => $team,
                'team_members_listings' => $team_members_listings,
            ]
        ]);
    }
}
