<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Fish\Species;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SpeciesController extends Controller
{
    /**
     * Published tür listesi.
     */
    public function index(Request $request): JsonResponse
    {
        $query = Species::published()->with('translations');

        if ($request->has('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('tr_name', 'ilike', "%{$search}%")
                  ->orWhere('en_name', 'ilike', "%{$search}%")
                  ->orWhere('scientific_name', 'ilike', "%{$search}%");
            });
        }

        if ($request->has('family')) {
            $query->where('family', $request->family);
        }

        if ($request->boolean('migratory')) {
            $query->migratory();
        }

        $species = $query->orderBy('tr_name')->get();

        return response()->json([
            'data' => $species->map(fn ($sp) => [
                'id' => $sp->id,
                'slug' => $sp->slug,
                'scientific_name' => $sp->scientific_name,
                'family' => $sp->family,
                'tr_name' => $sp->tr_name,
                'en_name' => $sp->en_name,
                'photo_url' => $sp->photo_url,
                'is_migratory' => $sp->is_migratory,
                'primary_water_zone' => $sp->primary_water_zone,
                'summary_md' => $sp->summary_md,
            ]),
        ]);
    }

    /**
     * Tür detayı + profil.
     */
    public function show(int $id): JsonResponse
    {
        $species = Species::with(['featurePrefs', 'preyRelations.prey', 'translations'])
            ->findOrFail($id);

        return response()->json([
            'data' => [
                'id' => $species->id,
                'slug' => $species->slug,
                'scientific_name' => $species->scientific_name,
                'family' => $species->family,
                'tr_name' => $species->tr_name,
                'en_name' => $species->en_name,
                'photo_url' => $species->photo_url,
                'summary_md' => $species->summary_md,
                'is_migratory' => $species->is_migratory,
                'primary_water_zone' => $species->primary_water_zone,
                'profile_status' => $species->profile_status,
                'feature_prefs' => $species->featurePrefs->map(fn ($fp) => [
                    'feature_key' => $fp->feature_key,
                    'eval_type' => $fp->eval_type,
                    'params' => $fp->params,
                    'weight' => (float) $fp->weight,
                    'note' => $fp->note,
                ]),
                'prey_relations' => $species->preyRelations->map(fn ($pr) => [
                    'prey' => [
                        'id' => $pr->prey->id,
                        'tr_name' => $pr->prey->tr_name,
                        'slug' => $pr->prey->slug,
                    ],
                    'strength' => (float) $pr->strength,
                ]),
                'translations' => $species->translations,
            ],
        ]);
    }
}
