<?php

namespace App\DataTables\ProductManagement\Categories;

use App\Models\ProductManagement\Category;
use App\Models\SecurityManagement\Permission;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use Yajra\DataTables\DataTablesEditor;

class CategoryDataTableEditor extends DataTablesEditor{
    protected $model = Category::class;

    /**Get create action validation rules.
     * @return array
     */
    public function createRules(){
        $category_table_name = $this->resolveModel()->getTable();

        return  [
            "name" => ["required",'min:2',
                Rule::unique($category_table_name,"name")
                   ],
            // "guard" => ["required",'string',Rule::in(array_keys(config("auth.guards")))], //,"in:$guards"
            "parent_id" => ['nullable',Rule::exists($category_table_name,"id")],
            "active" => ['nullable'],
        ];
    }

    /**Get edit action validation rules.
     * @param Model $model
     * @return array
     */
    public function editRules(Model $model){
        $category_table_name = $this->resolveModel()->getTable();
        return  [
            "name" => ["required",'min:2',
                Rule::unique($category_table_name,"name")
                    ->ignore($model->getKey())

            ],
            "parent_id" => ['nullable',Rule::exists($category_table_name,"id")],
            "active" => ['required'],
        ];
    }

    /**
     * Get remove action validation rules.
     *
     * @param Model $model
     * @return array
     */
    public function removeRules(Model $model){
       // $category_table_name = $this->resolveModel()->getTable();
        return [
//            "ids" => ['required',"array","min:1"],
//            "ids.*" => ['required',"int","exists:$category_table_name,id,deleted_at,NULL"],
        ];
    }

    public function attributes(){
        return [
            "name" => __("Name"),
            "parent_id" => __("Parent"),
            "active" => __("Status"),
        ];
    }


    public function created(Category $category, $data){
        return $category;
    }

    public function updated(Category $category, $data){
        return $category;
    }
}
