<?php

namespace App\Http\Controllers\Category;

use App\Http\Controllers\Controller;
use App\Http\Requests\Category\CategoryStoreRequest;
use App\Http\Requests\Category\CategoryUpdateController;
use App\Models\Category;
use App\Traits\ImageCompressor;
use DataTables;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\File;
use PHPUnit\TextUI\XmlConfiguration\Exception;


class CategoryController extends Controller
{


    use ImageCompressor;

    public function __construct()
    {
        $this->middleware('permission:view categories')->only('index', 'ajax');
        $this->middleware('permission:create category')->only('create');
        $this->middleware('permission:store category')->only('store');
        $this->middleware('permission:edit category')->only('edit');
        $this->middleware('permission:update category')->only('update');
        $this->middleware('permission:destroy category')->only('destroy');
    }


    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $categories =Category::paginate('12');
       return view('app.admin.pages.categories.all-category',compact([
           'categories'
       ]));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $categories =Category::all();
        return view('app.admin.pages.categories.create-category',compact([
            'categories'
        ]));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(CategoryStoreRequest $request)
    {
        $validatedData = $request->safe();

        try {

            Category::create($validatedData->except('partners'));

            return back()->with('success', __('Data successfully created!'));
        } catch (Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Category $category)
    {
        $categories =Category::all();
        return view('app.admin.pages.categories.edit-category',compact([
            'category',
            'categories'
        ]));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(CategoryUpdateController $request, Category $category)
    {
        $validatedData = $request->safe();
        try {


            $category->update($validatedData->except('partners'));


            return back()->with('success', __('Data successfully updated!'));
        } catch (Exception $e) {
            return back()->withErrors([$e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {

        try {

            $category =Category::find($id);
            $category->delete();

            return back()->with('success', __('Data successfully deleted!'));
        } catch (Exception $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    /**
     * @return JsonResponse
     * @throws Exception
     */
    public function ajax()
    {
        $categories = Category::query();
        return DataTables::of($categories)->editColumn('image', function (Category $category) {
        })->addColumn('options', function (Category $category) {
            return view('app.admin.includes.table-elements.action-buttons', [
                'modelName' => 'category',
                'editUrl' => route('admin:categories.edit', $category->getAttribute('id')),
                'deleteUrl' => route('admin:categories.destroy', $category->getAttribute('id'))
            ]);
        })->toJson();
    }
}
