<?php

namespace App\Service\RolePermission;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Spatie\Permission\Models\Role;

class RoleService
{
    public static function RoleStore(array $data)
    {
        $collection = collect($data);
        
        // Add company_id if user is logged in and has company_id
        if (Auth::check() && Schema::hasColumn('users', 'company_id')) {
            $user = Auth::user();
            if ($user->company_id) {
                $collection->put('company_id', $user->company_id);
            }
        }
        
        Role::create($collection->toArray());
    }

    public static function RoleUpdate(array $data, $id)
    {
        $collection = collect($data);
        Role::where('id', $id)->update($collection->toArray());
    }
}
