<?php

namespace App\Helpers;

class PermissionHelper
{
    protected static $permissions = [
        'admin' => [
            'users' => ['view', 'create', 'edit', 'delete'],
            'categories' => ['view', 'create', 'edit', 'delete'],
            'products' => ['view', 'create', 'edit', 'delete'],
            'orders' => ['view', 'edit', 'delete'],
            'commissions' => ['view', 'edit'],
            'contacts' => ['view', 'delete'],
            'settings' => ['view', 'create', 'edit', 'delete'],
            'shipping' => ['view', 'create', 'edit', 'delete'],
            'email-templates' => ['view', 'create', 'edit', 'delete'],
            'disputes' => ['view', 'edit', 'delete'],
            'posts' => ['view', 'create', 'edit', 'delete'],
            'ai-tasks' => ['view', 'edit'],
        ],
        'sub_admin' => [
            'users' => ['view', 'edit'],
            'categories' => ['view', 'create', 'edit'],
            'products' => ['view', 'create', 'edit'],
            'orders' => ['view', 'edit'],
            'commissions' => ['view'],
            'contacts' => ['view'],
            'settings' => ['view'],
            'shipping' => ['view', 'edit'],
            'email-templates' => ['view'],
            'disputes' => ['view', 'edit'],
            'posts' => ['view', 'edit'],
        ],
        'comptabilite' => [
            'users' => ['view'],
            'orders' => ['view', 'edit'],
            'commissions' => ['view', 'edit'],
            'contacts' => ['view'],
            'shipping' => ['view'],
            'disputes' => ['view'],
        ],
        'accountant' => [
            'users' => ['view'],
            'orders' => ['view', 'edit'],
            'commissions' => ['view', 'edit'],
            'contacts' => ['view'],
            'shipping' => ['view'],
            'disputes' => ['view'],
        ],
        'marketing' => [
            'categories' => ['view', 'create', 'edit', 'delete'],
            'products' => ['view', 'create', 'edit', 'delete'],
            'contacts' => ['view', 'delete'],
            'email-templates' => ['view', 'edit'],
            'posts' => ['view', 'create', 'edit', 'delete'],
            'users' => ['view'],
        ],
        'community_manager' => [
            'posts' => ['view', 'create', 'edit', 'delete'],
            'contacts' => ['view'],
            'users' => ['view'],
            'disputes' => ['view', 'edit'],
        ],
        'verifier' => [
            'users' => ['view', 'edit'],
            'orders' => ['view'],
            'products' => ['view'],
            'disputes' => ['view', 'edit'],
        ],
    ];

    public static function can($user, $resource, $action)
    {
        if (!$user || !isset(self::$permissions[$user->role])) {
            return false;
        }

        $rolePermissions = self::$permissions[$user->role];
        
        if (!isset($rolePermissions[$resource])) {
            return false;
        }

        return in_array($action, $rolePermissions[$resource]);
    }

    public static function hasAccess($user, $resource)
    {
        if (!$user || !isset(self::$permissions[$user->role])) {
            return false;
        }

        return isset(self::$permissions[$user->role][$resource]);
    }

    public static function getPermissions($role)
    {
        return self::$permissions[$role] ?? [];
    }
}
