<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Article;
use App\Discussion;
use App\Category;
use App\Libraries\Helper;

class FeedController extends Controller
{

    public function new()
    {
	// creating rss feed with our most recent 20 posts
	$articles = Article::join('discussion', 'discussion.article_id', '=', 'view_article.id')
	->select('view_article.id', 'view_article.article_id', 'view_article.url', 'view_article.title', 'view_article.excerpt_html', 'view_article.impact', 'view_article.upvotes', 'view_article.comments', 'view_article.repost')
	->groupBy('view_article.id', 'view_article.article_id', 'view_article.url', 'view_article.title', 'view_article.excerpt_html', 'view_article.impact', 'view_article.upvotes', 'view_article.comments', 'view_article.repost')
	->where('view_article.created_at', '<', now()->subMinutes(30))
	->orderByRaw('MAX(discussion.posted_on) DESC')
	->take(20)
	->get();

	return Helper::makeFeed($articles, "new");
    }

    public function mastodon()
    {
	$articles = Article::join('discussion', 'discussion.article_id', '=', 'view_article.id')
	->select('view_article.id', 'view_article.article_id', 'view_article.url', 'view_article.title', 'view_article.excerpt_html', 'view_article.impact', 'view_article.upvotes', 'view_article.comments', 'view_article.repost')
	->groupBy('view_article.id', 'view_article.article_id', 'view_article.url', 'view_article.title', 'view_article.excerpt_html', 'view_article.impact', 'view_article.upvotes', 'view_article.comments', 'view_article.repost')
	->where('view_article.created_at', '<', now()->subMinutes(30))
	->orderByRaw('MAX(discussion.posted_on) DESC')
	->take(20)
	->get();
	return Helper::makeFeed($articles, "mastodon");
    }
    public function mastodon_test()
    {
	$articles = Article::join('discussion', 'discussion.article_id', '=', 'view_article.id')
	->select('view_article.id', 'view_article.article_id', 'view_article.url', 'view_article.title', 'view_article.excerpt_html', 'view_article.impact', 'view_article.upvotes', 'view_article.comments', 'view_article.repost')
	->groupBy('view_article.id', 'view_article.article_id', 'view_article.url', 'view_article.title', 'view_article.excerpt_html', 'view_article.impact', 'view_article.upvotes', 'view_article.comments', 'view_article.repost')
	->where('view_article.created_at', '<', now()->subMinutes(30))
	->orderByRaw('MAX(discussion.posted_on) DESC')
	->take(20)
	->get();
	return Helper::makeFeed($articles, "mastodon_test");
    }

    public function popular()
    {

	$articles = new Article;
	$articles = $articles->setTable('view_popular');
	$articles = $articles->take(20)->get();
	return Helper::makeFeed($articles, "popular");
    }

    public function search(Request $request) {
	    $search_unsafe = $request->input("q");

	    if ( "" == $search_unsafe ) {
		    $search_unsafe = "";
	    }

	    $search_unsafe = explode(",", $request->input("q"));

	    $articles = new Article;

	    if ( "on" == $request->input("onlypopular") ) {
	    	$articles = $articles->setTable('view_popular');
	    }

	    foreach($search_unsafe as $q) {
		    $q = Helper::escapeLike($q);
		    $q = "%".$q."%";
		    $articles = $articles->where(function ($query) use ($q) {
		    		$query->whereHas('getCategories', function ($query) use ($q){
		    			$query->where('name', 'like', $q);
	    			})
		    		->orWhere('title', 'like', $q)
				->orWhere('url', 'like', $q)
				->orWhere('excerpt_html', 'like', $q);
				});
	    }
	    $count = $articles->count();

	    if ( "on" == $request->input("onlypopular") ) {
	    	$articles = $articles->orderBy('impact', 'desc');
	    }
	    $articles = $articles->orderBy('created_at', 'desc');

	    return Helper::makeFeed($articles->take(20)->get(), $request->input("q"));
    }
}
