<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;


class NewsController extends Controller
{

    private function headers()
    {
        return [
            'X-RapidAPI-Key' => env('RAPIDAPI_KEY'),
            'X-RapidAPI-Host' => env('RAPIDAPI_HOST'),
        ];
    }

    public function index(Request $request)
    {
        $lang = $request->get('lang', 'en');
        $country = 'US';

        /* 🔹 1. Topic Headlines (Default Home Content) */
        $headlines = Http::withHeaders($this->headers())
            ->get('https://real-time-news-data.p.rapidapi.com/topic-headlines', [
                'topic' => 'WORLD',
                'limit' => 6,
                'country' => $country,
                'lang' => $lang,
            ])->json()['data'] ?? [];

        /* 🔹 2. Language List */
        $languages = Http::withHeaders($this->headers())
            ->get('https://real-time-news-data.p.rapidapi.com/language-list', [
                'country' => $country
            ])->json()['data'] ?? [];

        /* 🔹 3. Topic News By Section */
        $topicNews = Http::withHeaders($this->headers())
            ->get('https://real-time-news-data.p.rapidapi.com/topic-news-by-section', [
                'topic' => 'TECHNOLOGY',
                'section' => 'CAQiSkNCQVNNUW9JTDIwdk1EZGpNWFlTQldWdUxVZENHZ0pKVENJT0NBUWFDZ29JTDIwdk1ETnliSFFxQ2hJSUwyMHZNRE55YkhRb0FBKi4IACoqCAoiJENCQVNGUW9JTDIwdk1EZGpNWFlTQldWdUxVZENHZ0pKVENnQVABUAE',
                'limit' => 6,
                'country' => $country,
                'lang' => $lang,
            ])->json()['data'] ?? [];

        /* 🔹 4. Local Headlines */
        $localNews = Http::withHeaders($this->headers())
            ->get('https://real-time-news-data.p.rapidapi.com/local-headlines', [
                'query' => 'New York',
                'country' => $country,
                'lang' => $lang,
                'limit' => 6,
            ])->json()['data'] ?? [];
// echo "<pre>";
// print_r($languages);
// echo "</pre>";
        return view('news.index', compact(
            'headlines',
            'languages',
            'topicNews',
            'localNews',
            'lang'
        ));
    }

    public function show()
    {
        $response = Http::withHeaders([
            'X-RapidAPI-Key' => env('RAPIDAPI_KEY'),
            'X-RapidAPI-Host' => env('RAPIDAPI_HOST'),
        ])->get('https://real-time-news-data.p.rapidapi.com/topic-news-by-section', [
            'topic' => 'TECHNOLOGY',
            'section' => 'CAQiSkNCQVNNUW9JTDIwdk1EZGpNWFlTQldWdUxVZENHZ0pKVENJT0NBUWFDZ29JTDIwdk1ETnliSFFxQ2hJSUwyMHZNRE55YkhRb0FBKi4IACoqCAoiJENCQVNGUW9JTDIwdk1EZGpNWFlTQldWdUxVZENHZ0pKVENnQVABUAE',
            'limit' => '500',
            'country' => 'US',
            'lang' => 'en'
        ]);

        if ($response->failed()) {
            return response()->json([
                'error' => 'News fetch nahi ho payi'
            ], 500);
        }

        $article = $response->json()['data'];
        return view('news.show', compact('article'));

        // return view('news-page', compact('news'));
    }
    public function latestNews()
    {
        $response = Http::withHeaders([
            'X-RapidAPI-Key' => env('RAPIDAPI_KEY'),
            'X-RapidAPI-Host' => env('RAPIDAPI_HOST'),
        ])->get('https://real-time-news-data.p.rapidapi.com/topic-headlines', [
            'topic' => 'WORLD',
            'limit' => 10,
            'country' => 'US',
            'lang' => 'en',
        ]);

        if ($response->failed()) {
            return response()->json([
                'error' => 'News fetch nahi ho payi'
            ], 500);
        }

        $article = $response->json()['data'];
        return view('news.show', compact('article'));

        // return view('news-page', compact('news'));
    }
}
