<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\News;
use Illuminate\Support\Str;

class SearchController extends Controller
{
    public function search(Request $request)
    {
        $query = $request->get('q');

        $results = News::searchNews($query)->map(function ($item) {
            return [
                'title' => $item->news_title,
                'encode_title' => $item->encode_title,
                'page' => Str::lower($item->category_name), // build correct URL slug
                'photo' => $item->photo,                    // add image filename
                'img_alt' => $item->img_alt ?? $item->news_title, // add alt text (fallback)
            ];
        });

        return response()->json($results);
    }
}
