<?php

namespace App\Http\Controllers\Website;

use App\Http\Controllers\Controller;
use App\Models\DownloadItem;
use App\Models\News;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;

final class NewsController extends Controller
{
    public function index()
    {
        $news = News::active()
            ->orderBy('created_at', 'desc')
            ->get();

        $appName = config('app.name', 'Perfect World');

        // Get background paths
        $heroBgPath = DB::table('settings')->where('key', 'hero_background')->value('value');
        $newsBgPath = DB::table('settings')->where('key', 'news_background')->value('value');

        // Generate WebP versions for hero background if available
        $heroBgWebP = null;
        if ($heroBgPath) {
            $pathInfo = pathinfo($heroBgPath);
            $webpPath = $pathInfo['dirname'].'/'.$pathInfo['filename'].'.webp';
            if (file_exists(public_path($webpPath))) {
                $heroBgWebP = asset($webpPath);
            }
        }

        // Get smoke animation enabled setting
        $smokeEnabled = DB::table('settings')->where('key', 'smoke_enabled')->value('value') === '1';

        // Get news section smoke animation enabled setting
        $newsSmokeEnabled = DB::table('settings')->where('key', 'news_smoke_enabled')->value('value') === '1';

        // Get news section sparks animation enabled setting
        $newsSparksEnabled = DB::table('settings')->where('key', 'news_sparks_enabled')->value('value') === '1';

        // Get server start time for countdown
        $serverStartTime = DB::table('settings')->where('key', 'server_start_time')->value('value');

        // Get hero use logo setting
        $heroUseLogo = DB::table('settings')->where('key', 'hero_use_logo')->value('value') === '1';

        // Get app logo path
        $logoPath = DB::table('settings')->where('key', 'app_logo')->value('value');
        $heroLogoUrl = $logoPath ? asset($logoPath) : null;

        // Get hero logo size (default: 300px)
        $heroLogoSize = (int) (DB::table('settings')->where('key', 'hero_logo_size')->value('value') ?? 300);

        // Get active download items for dialog
        $downloadItems = DownloadItem::active()->ordered()->get();

        return Inertia::render('website/news/index', [
            'news' => $news,
            'appName' => $appName,
            'serverInfo' => [
                'appName' => $appName,
                'heroBackgroundUrl' => $heroBgPath ? asset($heroBgPath) : null,
                'heroBackgroundWebP' => $heroBgWebP,
                'newsBackgroundUrl' => $newsBgPath ? asset($newsBgPath) : null,
                'heroHeight' => (int) (DB::table('settings')->where('key', 'hero_height')->value('value') ?? 40),
                'smokeEnabled' => $smokeEnabled,
                'newsSmokeEnabled' => $newsSmokeEnabled,
                'newsSparksEnabled' => $newsSparksEnabled,
                'appNameColor' => DB::table('settings')->where('key', 'app_name_color')->value('value') ?? '#fbbf24',
                'serverStartTime' => $serverStartTime,
                'heroUseLogo' => $heroUseLogo,
                'heroLogoUrl' => $heroLogoUrl,
                'heroLogoSize' => $heroLogoSize,
            ],
            'downloadItems' => $downloadItems,
        ]);
    }

    public function show(News $news)
    {
        abort_if(! $news->is_active, 404);

        $appName = config('app.name', 'Perfect World');

        // Get background paths
        $newsBgPath = DB::table('settings')->where('key', 'news_background')->value('value');

        // Get news section smoke animation enabled setting
        $newsSmokeEnabled = DB::table('settings')->where('key', 'news_smoke_enabled')->value('value') === '1';

        // Get news section sparks animation enabled setting
        $newsSparksEnabled = DB::table('settings')->where('key', 'news_sparks_enabled')->value('value') === '1';

        // Get all news for sidebar
        $allNews = News::active()
            ->orderBy('created_at', 'desc')
            ->get(['id', 'slug', 'title', 'category', 'image', 'created_at']);

        return Inertia::render('website/news/show', [
            'news' => $news,
            'allNews' => $allNews,
            'appName' => $appName,
            'serverInfo' => [
                'appName' => $appName,
                'newsBackgroundUrl' => $newsBgPath ? asset($newsBgPath) : null,
                'newsSmokeEnabled' => $newsSmokeEnabled,
                'newsSparksEnabled' => $newsSparksEnabled,
            ],
        ]);
    }

    public function apiShow(News $news)
    {
        abort_if(! $news->is_active, 404);

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