<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Artesaos\SEOTools\Facades\SEOTools;
use Artesaos\SEOTools\Facades\SEOMeta;
use App\Models\Category;
use App\Models\User;

class BlogController extends Controller
{
  public function index(Request $request){
    $title = isset(settings()->site_title) ? settings()->site_title : '';
    $pageTitle = 'Bulletin Room | '.$title;
    $description = isset(settings()->site_meta_description) ? settings()->site_meta_description : '';
    $imgURL = isset(settings()->site_logo) ? asset('/images/site/'.settings()->site_logo) : '';
    $keywords = isset(settings()->site_meta_keywords) ? settings()->site_meta_keywords : '';
    $currentUrl = url()->current();
    /** Meta SEO*/
    SEOTools::setTitle($pageTitle, false);
    SEOTools::setDescription($description);
    SEOMeta::setKeywords($keywords);
    /** Open Graph*/
    SEOTools::opengraph()->setUrl($currentUrl);
    SEOTools::opengraph()->addImage($imgURL);
    SEOTools::opengraph()->addProperty('type', 'articles');

    SEOTools::twitter()->addImage($imgURL);
    SEOTools::twitter()->setUrl($currentUrl);
    SEOTools::twitter()->setSite('@larablog');
    $data =[
        'pageTitle'=>'Bulletin Room |'.$title
    ];
    return view('front.pages.bulletin.bulletin', $data);
  }

  public function categoryPosts(Request $request, $slug = null){
    $category = Category::where('slug', $slug)->firstOrFail();
    $post = Post::where('category', $category->id)->where('visibility',1)->paginate(8);
    $title = 'Post in Category: '.$category->name;
    $description = 'Browse the latest PCDS post in the ' . $category->name .'category. Stay updated with articles, news and events.';

    SEOTools::setTitle($title, false);
    SEOTools::setDescription($description);
    SEOTools::opengraph()->setUrl(url()->current());
    $data = [
      'pageTitle'=>$category->name,
      'post'=>$post
    ];
    return view('front.pages.bulletin.category_post', $data);
  }
  public function authorPosts(Request $request, $username = null){
    $author = User::where('username', $username)->firstOrFail();
    $post = Post::where('author_id',$author->id)->where('visibility', 1)->orderBy('created_at', 'asc')->paginate(8);
    $title = $author->name.' - Blog Posts';
    $description = 'Explore the latest post by '.$author->name.'on various topics.';

    SEOTools::setTitle($title, false);
    SEOTools::setDescription($description);
    SEOTools::setCanonical(route('author_posts',['username'=>$author->username]));
    SEOTools::opengraph()->setUrl(route('author_posts',['username'=>$author->username]));
    SEOTools::opengraph()->addProperty('type', 'profile');
    SEOTools::opengraph()->setProfile([
      'first_name'=>$author->name,
      'username'=>$author->username
    ]);
    $data =[
      'pageTitle'=>$author->name,
      'author'=>$author,
      'post'=>$post
    ];
    return view('front.pages.bulletin.author_posts', $data);
  }

  public function tagPosts(Request $request, $tag = null)
{
    $tag = urldecode($tag); // 💡 This line fixes the '+' issue

    $post = Post::where('tags', 'LIKE', "%{$tag}%")
                ->where('visibility', 1)
                ->paginate(8);

    $title = "Post tagged with {$tag}";
    $description = "Explore all posts tagged with {$tag} on our blog.";

    SEOTools::setTitle($title, false);
    SEOTools::setDescription($description);
    SEOTools::setCanonical(url()->current());

    SEOTools::opengraph()->setUrl(url()->current());
    SEOTools::opengraph()->addProperty('type', 'articles');

    $data = [
        'pageTitle' => $title,
        'tag'       => $tag,
        'post'      => $post
    ];

    return view('front.pages.bulletin.tag_post', $data);
}
public function searchPosts(Request $request){
  $query = $request->input('q');
  

  if($query){
    $keywords = explode(' ', $query);
    $postsQuery = Post::query();
  
    foreach($keywords as $keyword){
      $postsQuery->orWhere('title', 'LIKE', '%'.$keyword.'%')
                 ->orWhere('tags', 'LIKE', '%'.$keyword.'%');
    }
    $posts = $postsQuery->where('visibility', 1)->orderBy('created_at', 'desc')->paginate(10);
    $title = "Search results for : {$query}";
    $description = "Browse search results for {$query} on bulletin. ";

  }else{
    $posts = collect();
    $title = 'Search';
    $description = 'Search for blog posts on our website.';
  }

  SEOTools::setTitle($title, false);
  SEOTools::setDescription($description);

  $data=[
    'pageTitle'=>$title,
    'query'=>$query,
    'post'=>$posts
  ];
  return view('front.pages.bulletin.search_posts', $data);
}
public function readPost(Request $request, $slug = null){
  $post = Post::where('slug',$slug)->firstOrFail();

  $relatedPosts = Post::where('category', $post->category)
                      ->where('id','!=',$post->id)
                      ->where('visibility',1)
                      ->take(3)
                      ->get();

  $nextPost = Post::where('id','>',$post->id)
                  ->where('visibility', 1)
                  ->orderBy('id','asc')
                  ->first();

  $prevPost = Post::where('id','<',$post->id)
                  ->where('visibility',1)
                  ->orderBy('id','desc')
                  ->first();
  $title = $post->title;
  $description = ($post->meta_description != '') ? $post->meta_description :words($post->content, 35);

  SEOTools::setTitle($title, false);
  SEOTools::setDescription($description);
  SEOTools::opengraph()->setUrl(route('read_post',['slug'=>$post->slug]));
  SEOTools::opengraph()->addProperty('type', 'article');
  SEOTools::opengraph()->addImage(asset('images/posts/'.$post->featured_image));
  SEOTools::twitter()->setImage(asset('images/posts/'.$post->featured_image));

  $data = [
    'pageTitle'=>$title,
    'post'=>$post,
    'relatedPosts'=>$relatedPosts,
    'nextPost'=>$nextPost,
    'prevPost'=>$prevPost
  ];
  return view('front.pages.bulletin.single_post',$data);
}
}
