<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class GameController extends Controller
{
    /**
     * Zwraca listę gier z wszystkimi potrzebnymi informacjami.
     *
     * @return array
     */
    public static function getGames()
    {
        return [
            [
                'id' => 2254990, // ID aplikacji Steam
                'title' => 'Permafrost',
                'platforms' => ['PC'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/2254990/header.jpg',
                'buy_link' => '',
                'learn_more_link' => 'https://store.steampowered.com/app/2254990/Permafrost/',
                'show_in' => ['our_games', 'blog']  
            ],
            [
                'id' => 1409840, // ID aplikacji Steam
                'title' => 'Aztecs The Last Sun',
                'platforms' => ['PC'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/1409840/header.jpg',
                'buy_link' => '',
                'learn_more_link' => 'https://store.steampowered.com/app/1409840/Aztecs_The_Last_Sun/',
                'show_in' => ['our_games', 'blog']  
            ],
            [
                'id' => 1588090, // ID aplikacji Steam
                'title' => 'Rally Mechanic Simulator',
                'platforms' => ['PC'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/1588090/header.jpg',
                'buy_link' => '',
                'learn_more_link' => 'https://store.steampowered.com/app/1588090/Rally_Mechanic_Simulator/',
                'show_in' => ['our_games', 'blog']  
            ],
            [
                'id' => 3002090, // ID aplikacji Steam
                'title' => 'Rally Mechanic Simulator Prologue',
                'platforms' => ['PC'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/3002090/header.jpg',
                'buy_link' => 'https://store.steampowered.com/app/3002090/Rally_Mechanic_Simulator_Prologue/',
                'learn_more_link' => '#',
                'show_in' => ['our_games', 'blog']  
            ],
            [
                'id' => 1078760, // ID aplikacji Steam
                'title' => 'Motorcycle Mechanic Simulator 2021',
                'platforms' => ['PC', 'Xbox', 'PlayStation'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/1078760/header.jpg',
                'buy_link' => 'https://store.steampowered.com/app/1078760/Motorcycle_Mechanic_Simulator_2021/',
                'learn_more_link' => '#',
                'show_in' => ['our_games', 'blog']  
            ],
            [
                'id' => 2139810, //DLC
                'title' => 'MMS 21 - Scooter DLC ',
                'platforms' => ['PC', 'Xbox', 'PlayStation'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/2139810/header.jpg',
                'buy_link' => 'https://store.steampowered.com/app/2139810/Motorcycle_Mechanic_Simulator_2021__Scooter_DLC/',
                'learn_more_link' => '#',
                'show_in' => ['our_games']  
            ],
            [
                'id' => 2246270, // DLC
                'title' => 'MMS 21 - Electric Bike DLC',
                'platforms' => ['PC', 'Xbox', 'PlayStation'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/2246270/header.jpg',
                'buy_link' => 'https://store.steampowered.com/app/2246270/Motorcycle_Mechanic_Simulator_2021__Electric_Bike_DLC/',
                'learn_more_link' => '#',
                'show_in' => ['our_games']  
            ],
            [
                'id' => 1574700, // ID aplikacji Steam
                'title' => 'Motorcycle Mechanic Simulator 2021 Prologue',
                'platforms' => ['PC'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/1574700/header.jpg',
                'buy_link' => 'https://store.steampowered.com/app/1574700/Motorcycle_Mechanic_Simulator_2021_Prologue/',
                'learn_more_link' => '#',
                'show_in' => ['our_games', 'blog']  
            ],
            
            [
                'id' => 936490, // ID aplikacji Steam
                'title' => 'Counter Terrorist Agency',
                'platforms' => ['PC'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/936490/header.jpg',
                'buy_link' => 'https://store.steampowered.com/app/936490/Counter_Terrorist_Agency/',
                'learn_more_link' => '',
                'show_in' => ['our_games']
            ],
            [
                'id' => 716760, // ID aplikacji Steam
                'title' => 'Dream Alone',
                'platforms' => ['PC', 'Nintendo Switch'],
                'image' => 'https://cdn.cloudflare.steamstatic.com/steam/apps/716760/header.jpg',
                'buy_link' => 'https://store.steampowered.com/app/716760/Dream_Alone/',
                'learn_more_link' => '',
                'show_in' => ['our_games']
            ],
            // Dodaj więcej gier według potrzeb
        ];
    }

    /**
     * Pobiera newsy z API Steama dla danej gry.
     *
     * @param int $appId
     * @return array
     */
    public static function fetchNewsForGame($appId)
    {
        $apiUrl = "https://api.steampowered.com/ISteamNews/GetNewsForApp/v2/?appid={$appId}&count=3&maxlength=300";
        $response = Http::get($apiUrl);

        if ($response->successful()) {
            return $response->json()['appnews']['newsitems'];
        }

        return [];
    }
    public function getLatestBlogPosts()
    {
        // Pobierz wszystkie gry
        $games = $this->getGames();
        $allNewsItems = [];
    
        // Pobierz wpisy z każdej gry i dodaj je do wspólnej tablicy
        foreach ($games as $game) {
            if (in_array('blog', $game['show_in'])) {
                $newsItems = $this->fetchNewsForGame($game['id']);
                foreach ($newsItems as $news) {
                    $allNewsItems[] = [
                        'title' => $news['title'],
                        'url' => $news['url'],
                        'date' => \Carbon\Carbon::createFromTimestamp($news['date']), // Konwertowanie na Carbon
                        'image' => $game['image'],
                        'gameTitle' => $game['title']
                    ];
                }
            }
        }
    
        // Posortuj wpisy według daty publikacji (od najnowszych)
        usort($allNewsItems, function ($a, $b) {
            return $b['date']->timestamp - $a['date']->timestamp;
        });
    
        // Zwrócenie tylko 9 najnowszych wpisów
        return array_slice($allNewsItems, 0, 9);
    }
    
}