<?php

class ACCJ_Permission_Service {

    private $cache;

    public function __construct() {
        $this->cache = accj_acl()->get_cache_service();
    }

    public function get_role_permissions($role_id){

        $cache_key = 'role_permissions_' . $role_id;
        $cached_permissions = $this->cache->get($cache_key);

        if ($cached_permissions !== false) {
            return $cached_permissions;
        }

        global $wpdb;

        // ... (همان کوئری قبلی)
        $sql = "
        SELECT 
            p.code,
            rp.can_view,
            rp.can_create,
            rp.can_edit,
            rp.can_delete,
            rp.scope_company,
            rp.scope_project,
            rp.scope_unit
        FROM {$wpdb->prefix}accj_role_permissions rp
        JOIN {$wpdb->prefix}accj_permissions p
        ON p.id = rp.permission_id
        WHERE rp.role_id = %d
        ";

        $rows = $wpdb->get_results(
            $wpdb->prepare($sql,$role_id),
            ARRAY_A
        );

        $permissions = [];

        foreach($rows as $row){
            $permissions[$row['code']] = $row;
        }

        $this->cache->set($cache_key, $permissions);
        return $permissions;
    }
}
