<?php

namespace App\Services;

use App\Models\Objects\NirObject;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class PermissionsService {
    private Model $model;
    private ?User $user;

    private array $rules = [
        NirObject::class => 'nirObject'
    ];

    public function __construct(Model $model, ?User $user = null) {
        $this->model = $model;
        $this->user = $user ?? Auth::user();
    }

    public function get(): array {
        $rule = $this->rules[get_class($this->model)] ?? null;
        $func = "{$rule}Permissions";
        if ($this->user->isPrivileged) return ['anything' => true];
        elseif (method_exists($this, $func)) return $this->$func();
        else return [];
    }


    public function nirObjectPermissions(): array {
        return ['edit' => $this->model->owner_id === $this->user->id];
    }

}