<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\Language;
use Illuminate\Http\Request;

class AjaxController extends Controller
{
    /**
     * Get latest news for sidebar
     * URL: /ajax/load/latestnews/{limit}/{skip}/{exclude}
     */
    public function latestNews($limit = 10, $skip = 0, $exclude = 0)
    {
        $default_language = session()->has('language')
            ? Language::find(session()->get('language'))
            : Language::where('is_default', 1)->first();

        $posts = Post::where('language_id', $default_language->id)
            ->where('status', true)
            ->where('is_pending', 0)
            ->where('schedule_post', 0)
            ->whereHas('category')
            ->when($exclude, function ($query) use ($exclude) {
                return $query->where('id', '!=', $exclude);
            })
            ->orderBy('id', 'desc')
            ->skip($skip)
            ->take($limit)
            ->get();

        $result = [];
        foreach ($posts as $post) {
            $categorySlug = $post->category->slug ?? 'news';
            $categoryTitle = $post->category->title ?? '';
            $result[] = [
                'headline' => $post->title,
                'fullheadline' => $post->title,
                'url' => route('frontend.postBySubcategory.details', [$categorySlug, $post->slug]),
                'categoryTitle' => $categoryTitle,
                'video_code' => $post->video ?? null,
            ];
        }

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

    /**
     * Get popular/trending news for sidebar
     * URL: /ajax/load/popularnews/{limit}/{skip}
     */
    public function popularNews($limit = 10, $skip = 0)
    {
        $default_language = session()->has('language')
            ? Language::find(session()->get('language'))
            : Language::where('is_default', 1)->first();

        $posts = Post::where('language_id', $default_language->id)
            ->where('status', true)
            ->where('is_pending', 0)
            ->where('schedule_post', 0)
            ->whereHas('category')
            ->orderBy('views_count', 'desc')
            ->skip($skip)
            ->take($limit)
            ->get();

        $result = [];
        foreach ($posts as $post) {
            $categorySlug = $post->category->slug ?? 'news';
            $result[] = [
                'title' => $post->title,
                'link' => route('frontend.postBySubcategory.details', [$categorySlug, $post->slug]),
            ];
        }

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

    /**
     * Get menubar categories
     * URL: /ajax/load/menubarcategories
     */
    public function menubarCategories()
    {
        $default_language = session()->has('language')
            ? Language::find(session()->get('language'))
            : Language::where('is_default', 1)->first();

        $categories = \App\Models\Category::where('language_id', $default_language->id)
            ->whereNull('parent_id')
            ->orderBy('id', 'asc')
            ->get();

        $result = [];
        foreach ($categories as $cat) {
            $result[] = [
                'title' => $cat->slug,
                'display_name' => $cat->title,
            ];
        }

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

    /**
     * Get category news
     * URL: /ajax/load/categorynews/{categoryId}/{limit}/{skip}/{type}/{exclude}
     */
    public function categoryNews($categoryId, $limit = 4, $skip = 0, $type = 0, $exclude = 0)
    {
        $default_language = session()->has('language')
            ? Language::find(session()->get('language'))
            : Language::where('is_default', 1)->first();

        $posts = Post::where('language_id', $default_language->id)
            ->where('category_id', $categoryId)
            ->where('status', true)
            ->where('is_pending', 0)
            ->where('schedule_post', 0)
            ->when($exclude, function ($query) use ($exclude) {
                return $query->where('id', '!=', $exclude);
            })
            ->orderBy('id', 'desc')
            ->skip($skip)
            ->take($limit)
            ->get();

        $result = [];
        foreach ($posts as $post) {
            $categorySlug = $post->category->slug ?? 'news';
            $categoryTitle = $post->category->title ?? '';
            $result[] = [
                'headline' => $post->title,
                'fullheadline' => $post->title,
                'url' => route('frontend.postBySubcategory.details', [$categorySlug, $post->slug]),
                'categoryTitle' => $categoryTitle,
                'categoryName' => $categoryTitle,
                'video_code' => $post->video ?? null,
                'thumbMedium' => asset('assets/images/posts/' . $post->image_big),
                'publishTime' => $post->created_at ? $post->created_at->format('d M Y') : '',
            ];
        }

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