<?php

namespace App\DataTables\Roles;

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

class RoleDataTableEditor extends DataTablesEditor{
    protected $model = Role::class;

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

        return  [
            "name" => ["required",'min:4',
                Rule::unique($role_table_name,"name")
                    ->where(function ($query){
                        return $query->where('guard_name',activeGuard());
                    })],
            // "guard" => ["required",'string',Rule::in(array_keys(config("auth.guards")))], //,"in:$guards"
            "description" => ['nullable','min:4'],
            "permissions" => ['required','array','min:1'],
            "permissions.*" => ['required',"int","exists:$permissions_table_name,id"],
        ];
    }

    /**Get edit action validation rules.
     * @param Model $model
     * @return array
     */
    public function editRules(Model $model){
        $role_table_name = $this->resolveModel()->getTable();
        $permissions_table_name = (new Permission)->getTable();
        return  [
            "name" => ["required",'min:4',
                Rule::unique($role_table_name,"name")
                    ->ignore($model->getKey())
                    ->where(function ($query){
                        return $query->where('guard_name',activeGuard());
                    })
            ],
            "description" => ['nullable','min:4'],
            "permissions" => ['nullable','array','min:1'],
            "permissions.*" => ['nullable',"int","exists:$permissions_table_name,id"],
        ];
    }

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

    public function attributes(){
        return [
            "name" => __("Name"),
            "descrption" => __("Description"),
            "permissions" => __("Permissions"),
            "permissions.*" => __("Permission")
        ];
    }


    public function created(Role $role, $data){
        $permissions = $data['permissions'] ?? [];

        if ($permissions){
            $role->syncPermissions($permissions);
        }
        return $role;
    }

    public function updated(Role $role, $data){
        $permissions = (array) $data['permissions'] ?? [];
        if ($permissions){
            $role->syncPermissions( $permissions);
        }
        return $role;
    }
}
