<?php

namespace App\Repositories\Core\Auth;

use App\Models\Core\Auth\Permission;
use Yajra\DataTables\DataTables;

class Permissions
{
    public static function getModules()
    {
        return Permission::select('module')->distinct('module')->get()->pluck('module');
    }

    public static function getOptions()
    {
        return Permission::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
    }

    public static function getInfo($id)
    {
        return Permission::find($id);
    }

    public static function select_all()
    {
        return self::getAll()->toArray();
    }

    public static function select_by_id($id)
    {
        return Permission::find($id)->toArray();
    }

    public static function getAll()
    {
        return Permission::orderBy('name', 'asc')->get();
    }

    public static function getByName($name)
    {
        return Permission::where('name', $name)->first();
    }

    public static function get($id)
    {
        return Permission::findOrFail($id);
    }

    public static function getTable($id)
    {
        $datas = Permission::withCount(['users']);

        return Datatables::of($datas)->make(true);
    }

    public static function delete($id)
    {
        Users::destroyByUniquePermission($id);

        return Permission::destroy($id);
    }

    public static function store($data)
    {
        return (isset($data['id']) && $data['id']) ? self::update($data) : self::create($data);
    }

    public static function create($data)
    {
        $permission = Permission::create($data);

        return $permission;
    }

    public static function update($data)
    {
        return self::get($data['id'])->update($data);
    }

    public static function count()
    {
        return Permission::count();
    }
}
