<?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.';
    
    /**
     * Show the Articles of category.
     * @param Category $category
     * @param Request $request
     * @return View
     */
    public function index(Category $category, Request $request): View
    {
        $categoryDescription= $category->currentDescription;
        // Check Slug is the same
        if ($request->slug && $request->slug != str_slug($categoryDescription->slug)) {
            abort(404);
        }
        //
        $articles= Article::latest()
            ->where('category_id', $category->id)
            ->paginate(30);

        $title= $categoryDescription->name ;
        $keywords= $categoryDescription->keywords ;
        $meta_description= $categoryDescription->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()->with(['currentDescription'])->get();
        $categories= [];
        if($query){
            $categories= $query->toArray();
        }

        foreach ($query as $key => $category) {
            $articles= Article::latest()->with(['currentDescription'])
            ->where('category_id', $category->id)
            ->limit(3)->get();

            $categories[$key]['articles']= $articles;
            $categories[$key]['url']= $category->url;
        }
        // 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'));
    }

    /**
     * Show the Article details.
     * @param Article $article
     * @param Request $request
     * @return View
     */
    public function details(Article $article, Request $request) :View
    {
        if(!$request->slug || $request->slug != $article->slug_text) {
            abort(404);
        }

        $image= getImage($article->image);
        $description = $article->currentDescription;
        $articlePhotos = ArticlePhoto::where('article_id', $article->id)
        ->get()->pluck('image')->toArray();

        $photos= [];
        if ($article->video_type == 2 && $article->image ) {
            $photos[]= getImage($article->image);
        }
        foreach ($articlePhotos as $i => $value) {
            $photos[] = getImage($value);
        }
        $title= $description->title;
        $keywords= $description->keywords;
        $meta_description= $description->meta_description;

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