<?php

namespace App\Http\Controllers;

use App\Models\Ticket\Category;
use App\Models\Articles\Article;

class CategorypageController extends Controller
{
    public function index($id){

        $categoryss = Category::where('categoryslug', $id)->first();
        if($categoryss == null)
        {
            $category = Category::find($id);
            $data['category'] = $category;

            $categorys = Category::where('id',$id)->with('articles.subcategorys')->get();
            $data['categorys'] = $categorys;
        }
        if($categoryss != null)
        {
            $category = Category::where('categoryslug', $id)->first();
            $data['category'] = $category;

            $categorys = Category::where('categoryslug',$id)->with('articles')->get();
            $data['categorys'] = $categorys;
        }

        $populararticle = Article::where('status', 'published')->orderBy('views','desc')->latest()->take(6)->get();
        $data['populararticles'] = $populararticle;

        $article = Article::where('status', 'published')->latest('created_at')->take(6)->get();
        $data['recentarticles'] = $article;

        $setting = response(['article_count' => setting('article_count')]);
        $data['setting'] = $setting;
        
        return response()->json($data);
    }
}
