<?php

namespace App\Http\Controllers\Web;

use App\Helpers\ImageHelper;
use App\Http\Controllers\Controller;
use App\Article;
use App\ArticleDescription;
use App\ArticlePhoto;
use App\Category;
use Carbon\Carbon;
use Carbon\Traits\Date;
use Illuminate\Http\Request;
use \Illuminate\Support\Str;
use Illuminate\View\View;

class ArticleController extends Controller
{
    private $viewDirectory = 'web.articles.';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // $this->middleware('auth')->except('lang');
    }

    /**
     * Show the Articles of category.
     *
     * @return \View
     */
    public function index(Category $category): View
    {
        $query= Article::latest()
            ->join('article_descriptions AS ad', 'articles.id', 'ad.article_id')
            ->where('language_id', currentLanguage()->id)
            ->where('category_id', $category->id)
            ->select(['ad.*', 'articles.*'])->get();
        $articles= [];
        if($query){
            $articles= $query->toArray();
        }

        foreach ($articles as $key => $value) {
            $articles[$key]['image']= ImageHelper::get($value['image']);
            $articles[$key]['date']= Carbon::parse($value['updated_at'])->format('F d Y');
            $articles[$key]['intro']= Str::words(strip_tags($value['description']), 10, '...');
            $articles[$key]['route']= route('article_details', ['article'=> $value['id'], 'slug'=> str_slug($value['slug'])] );
        }
        // dd($articles);
        $categoryDescription= $category->descriptions(currentLanguage()->id)->get()->toArray();
        $categoryDescription= count($categoryDescription) ? $categoryDescription[0] : [];
        // dd($categoryDescription);
        $settings= settings();
        $title= $categoryDescription['name'] ?? $settings['website_name'];
        $keywords= $categoryDescription['keywords'] ?? $settings['keywords'];
        $meta_description= $categoryDescription['meta_description'] ?? $settings['meta_description'];

        return view($this->viewDirectory.'index', compact( 'articles', 'title', 'keywords', 'meta_description'));
    }

    /**
     * Show the Articles of category.
     *
     * @return \View
     */
    public function categories(): View
    {
        $query= Category::latest()
            ->join('category_descriptions AS ad', 'categories.id', 'ad.category_id')
            ->where('language_id', currentLanguage()->id)
            ->select(['ad.*', 'categories.*'])->get();
        $categories= [];
        if($query){
            $categories= $query->toArray();
        }

        foreach ($categories as $key => $category) {
            $query= Article::latest()
            ->join('article_descriptions AS ad', 'articles.id', 'ad.article_id')
            ->where('language_id', currentLanguage()->id)
            ->where('category_id', $category['category_id'])
            ->select(['ad.*', 'articles.*'])->limit(3)->get();

            $articles= [];
            if($query){
                $articles= $query->toArray();
            }

            foreach ($articles as $a => $value) {
                $articles[$a]['image']= ImageHelper::get($value['image']);
                $articles[$a]['intro']= Str::words(strip_tags($value['description']), 10, '...');
                $articles[$a]['route']= route('article_details', ['article'=> $value['id'], 'slug'=> str_slug($value['slug'])] );
            }
            $categories[$key]['articles']= $articles;
            $categories[$key]['route']= route('articles', ['category'=> $category['category_id'], 'slug'=> str_slug($category['slug'])] );
        }
        // dd($categories);
        $settings= settings();
        $title= $settings['website_name'];
        $keywords= $settings['keywords'];
        $meta_description= $settings['meta_description'];

        return view($this->viewDirectory.'categories', compact( 'categories', 'title', 'keywords', 'meta_description'));
    }

    public function details(Article $article, Request $request) :View
    {
        $image= ImageHelper::get($article->image);
        $description = ArticleDescription::where('article_id', $article->id)
        ->where('language_id', currentLanguage()->id)
        ->first();
        $articlePhotos = ArticlePhoto::where('article_id', $article->id)
        ->get()->pluck('image')->toArray();

        $photos= [];
        if ($article->video_type == 2) {
            $photos[]= $image;
        }
        foreach ($articlePhotos as $i => $value) {
            $photos[] = ImageHelper::get($value);
        }
        // dd($description->toArray(), $image, $photos);
        // dd($article->toArray());
        $title= $description->title;
        $keywords= $description->keywords;
        $meta_description= $description->meta_description;

        return view( $this->viewDirectory.'details', compact('article', 'description', 'image', 'photos', 'title', 'keywords', 'meta_description') );
    }
}
