<?php

namespace App\Http\Controllers\Web;

use App\Helpers\ImageHelper;
use App\Http\Controllers\Controller;
use App\WebsitePhoto;
use App\WebsiteCategory;
use App\WebsiteCategoryDescription;
use Illuminate\Http\Request;
use \Illuminate\Support\Str;
use Illuminate\View\View;

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

    /**
     * Show the Websites of category.
     *
     * @return \View
     */
    public function index(WebsiteCategory $category): View
    {
        $websites= WebsitePhoto::orderby('website_photos.id', 'desc')
            ->join('website_photo_descriptions AS d', 'website_photos.id', 'd.website_photo_id')
            ->where('language_id', currentLanguage()->id)
            ->where('website_category_id', $category->id)
            ->select(['d.title', 'website_photos.image', 'website_photos.url'])->get()->toArray();

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

    /**
     * Show the Websites of category.
     *
     * @return \View
     */
    public function categories(): View
    {
        $query= WebsiteCategory::latest()
            ->join('website_category_descriptions AS d', 'website_categories.id', 'd.website_category_id')
            ->where('language_id', currentLanguage()->id)
            ->select(['d.*', 'website_categories.*'])
            ->get();

        $categories= [];

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

        foreach ($categories as $key => $category) {
            $query= WebsitePhoto::orderby('id', 'desc')
            ->where('website_category_id', $category['website_category_id'])
            ->select(['website_photos.image'])->limit(1)->first();
            $website= ['image'=> ''];
            if($query){
                $website= $query->toArray();
            }

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