<?php

namespace App\Http\Controllers\Web;

use App\Helpers\ImageHelper;
use App\Http\Controllers\Controller;
use App\Video;
use App\VideoDescription;
use App\VideoCategory;
use App\VideoCategoryDescription;
use Illuminate\Http\Request;
use \Illuminate\Support\Str;
use Illuminate\View\View;

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

    /**
     * Show the Videos of category.
     *
     * @return \View
     */
    public function index(VideoCategory $category): View
    {
        $videos= Video::orderby('videos.id', 'desc')
            ->join('video_descriptions AS d', 'videos.id', 'd.video_id')
            ->where('language_id', currentLanguage()->id)
            ->where('video_category_id', $category->id)
            ->select(['d.title', 'videos.*'])->get()->toArray();

        foreach ($videos as $a => $value) {
            $videos[$a]['image']= ImageHelper::get($value['image']);
        }
        // dd($videos);
        $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( 'videos', 'title', 'keywords', 'meta_description'));
    }

    /**
     * Show the Videos of category.
     *
     * @return \View
     */
    public function categories(): View
    {
        $query= VideoCategory::latest()
            ->join('video_category_descriptions AS d', 'video_categories.id', 'd.video_category_id')
            ->where('language_id', currentLanguage()->id)
            ->select(['d.*', 'video_categories.*'])
            ->get();

        $categories= [];

        if ($query) {
            $categories= $query->toArray();
        }

        foreach ($categories as $key => $category) {
            $query= Video::orderby('id', 'desc')
            ->where('video_category_id', $category['video_category_id'])
            ->select(['video', 'image', 'type'])->limit(1)->first();

            $video=['image'=>''];
            if ($query) {
                $video= $query->toArray();
            }

            $categories[$key]['image']= ImageHelper::get($video['image']);
            $categories[$key]['route']= route('videos', ['category'=> $category['video_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'));
    }
}
