<?php

namespace Mamad\Category\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Mamad\Article\Repositories\ArticleRepository;
use Mamad\Category\Http\Requests\CategoryRequest;
use Mamad\Category\Models\Category;
use Mamad\Category\Repositories\CategoryRepository;
use Mamad\PostMeta\Repository\PostMetaRepository;
use Mamad\Product\Repository\ProductRepository;

class CategoryController extends Controller
{
    //front routes
    public function index($slug)
    {
        $category=CategoryRepository::find_by_slug($slug);
        if($category){
            $data=["category"=>$category];
            if($category->type=="product" ){
                $categories=$category->children;
                $data["products"]=ProductRepository::get_products_by_category($category);
                $data["brands"]=$data["products"]->pluck("brand");
                $data["categories"]=$categories;
            } elseif ($category->type=="article"){
                $data["articles"]=ArticleRepository::get_articles_by_category($category);
            }elseif ($category->type=="group_product"){
                $categories=$category->children;
                $data["products"]=$category->group_products;
                $data["brands"]=$data["group_products"]->pluck("brand");
                $data["categories"]=$categories;
            }

            return view("Front::category-".$category->type,$data);
        }
    }

    //admin routes
    public function category_add_form(Request $request){
        $this->authorize("index",Category::class);
        if(!in_array($request->post_type,Category::$post_type)){
            return abort(404);
        }

        $categories=CategoryRepository::get_by_type($request->post_type);
        return view("Category::Admin.category_add",["categories"=>$categories]);
    }

    public function category_add(CategoryRequest $request){

        $this->authorize("create",Category::class);
        $slug=make_slug_for_data($request->name,$request->slug);
        $request->request->add(["slug"=>$slug]);

        $cat=CategoryRepository::create($request);
        PostMetaRepository::update_meta_tag($cat,$request);

        if($request->ajax()){
            return $cat;
        } else {
            return back();
        }
    }

    public function category_edit_form(Request $request, $id){
        $this->authorize("edit",Category::class);
        $category=CategoryRepository::find($id);
        $categories=CategoryRepository::get_all_by_type_except_id($category->type,$id);
        return view("Category::Admin.category_edit",["category"=>$category,"categories"=>$categories]);
    }

    public function category_edit(CategoryRequest $request){
        $this->authorize("edit",Category::class);
        $slug=make_slug_for_data($request->name,$request->slug);
        $request->request->add(["slug"=>$slug]);
        $category=CategoryRepository::find($request->id);
        PostMetaRepository::update_meta_tag($category,$request);

        CategoryRepository::update($category,$request);
        return back();
    }

    public function category_delete($id)
    {
        $this->authorize("delete",Category::class);
        CategoryRepository::destroy($id);
        return back();
    }
}
