<?php

use App\Models\GeneralSetting;
use App\Models\ParentCategory;
use App\Models\Category;
use App\Models\Post;
use Carbon\Carbon;
use Illuminate\Support\Str;

if (!function_exists('settings')) {
    function settings()
    {
        $settings = GeneralSetting::take(1)->first();

        if (!is_null($settings)) {
            return $settings;
        }
    }
}
// if (!function_exists('navigation')) {
//     function navigation()
//     {
//         $navigation_html = '';

//         $pcategories = ParentCategory::whereHas('children', function ($q) {
//             $q->whereHas('posts');
//         })->orderBy('name', 'asc')->get();

//         if (count($pcategories) > 0) {
//             $navigation_html .= '
//                 <li class="has-dropdown">
//                     <a href="#!">Bulletin <i class="bi bi-chevron-down toggle-dropdown"></i></a>
//                     <ul class="dropdown">
//             ';

//             foreach ($pcategories as $item) {
//                 $navigation_html .= '<li><a href="#!">' . $item->name . '</a></li>';
//             }

//             $navigation_html .= '
//                     </ul>
//                 </li>
//             ';
//         }

//         return $navigation_html;
//     }
// }

if (!function_exists('date_formatter')) {
    function date_formatter($value)
    {
        return Carbon::createFromFormat('Y-m-d H:i:s', $value)->isoFormat('LL');
    }
}
if (!function_exists('words')) {
    function words($value, $words = 30, $end = "...")
    {
        return Str::words(strip_tags($value), $words, $end);
    }
}
if (!function_exists('readDuration')) {
    function readDuration(...$text)
    {
        Str::macro('timeCounter', function ($text) {
            $totalWords = str_word_count(implode(" ", $text));
            $minutesToRead = round($totalWords / 200);
            return (int)max(1, $minutesToRead);
        });
        return Str::timeCounter($text);
    }
}
if (!function_exists('home_latest_posts')) {
    function home_latest_posts($skip = 0, $limit = 5)
    {
        return Post::skip($skip)
            ->limit($limit)
            ->where('visibility', 1)
            ->whereHas('post_category', function ($query) {
                // Exclude posts where the category has a parent named "Article"
                $query->whereDoesntHave('parent_category', function ($subQuery) {
                    $subQuery->where('name', 'Article');
                });
            })
            ->orderBy('created_at', 'desc')
            ->get();
    }
}
if (!function_exists('latest_posts')) {
    function latest_posts($skip = 0, $limit = 5)
    {
        return Post::skip($skip)
            ->limit($limit)
            ->where('visibility', 1)
            ->orderBy('created_at', 'desc')
            ->get();
    }
}

if (!function_exists('latest_article')) {
    function latest_article($skip = 0, $limit = 2)
    {
        return Post::where('visibility', 1)
            ->whereHas('post_category', function ($query) {
                $query->whereHas('parent_category', function ($subQuery) {
                    $subQuery->where('name', 'Article');
                });
            })
            ->skip($skip)
            ->limit($limit)
            ->orderBy('created_at', 'desc')
            ->get();
    }
}

if (!function_exists('sidebar_categories')) {
    function sidebar_categories($limit = 8) {
        return Category::whereNotNull('parent') // child categories only
            ->whereHas('posts', function ($query) {
                $query->where('visibility', 1);
            })
            ->with('parent_category') // load parent info
            ->withCount(['posts' => function ($query) {
                $query->where('visibility', 1);
            }])
            ->orderBy('posts_count', 'desc')
            ->limit($limit)
            ->get();
    }
}
if(!function_exists('getTags')){
    function getTags($limit = null){
        $tags = Post::where('tags', '!=', '')->pluck('tags');
        $uniqueTags = $tags->flatMap(function($tagString){
            return explode(',',$tagString);
        })->map(fn($tag)=>trim($tag))
        ->unique()
        ->sort()
        ->values();

        if($limit){
            $uniqueTags = $uniqueTags->take($limit);
        }
        return $uniqueTags->all();
    }
}
if(!function_exists('sidebar_latest_posts')){
    function sidebar_latest_posts($limit = 3, $except = null){
        $post = Post::limit($limit);
        if($except){
            $post = $post->where('id', '!=', $except);
        }
        return $post->where('visibility', 1)
                ->orderBy('created_at', 'desc')
                ->get();
    }
   

}

