ScopeType::class, 'priority' => 'integer', ]; /** * Parent profile (for profile-level inheritance). */ public function parent(): BelongsTo { return $this->belongsTo(self::class, 'parent_profile_id'); } /** * Child profiles. */ public function children(): HasMany { return $this->hasMany(self::class, 'parent_profile_id'); } /** * Config values in this profile. */ public function values(): HasMany { return $this->hasMany(ConfigValue::class, 'profile_id'); } /** * Get system profile. */ public static function system(): ?self { return static::where('scope_type', ScopeType::SYSTEM) ->whereNull('scope_id') ->orderByDesc('priority') ->first(); } /** * Get profiles for a scope. * * @return Collection */ public static function forScope(ScopeType $type, ?int $scopeId = null): Collection { return static::where('scope_type', $type) ->where('scope_id', $scopeId) ->orderByDesc('priority') ->get(); } /** * Get profile for workspace. */ public static function forWorkspace(int $workspaceId): ?self { return static::where('scope_type', ScopeType::WORKSPACE) ->where('scope_id', $workspaceId) ->orderByDesc('priority') ->first(); } /** * Get or create system profile. */ public static function ensureSystem(): self { return static::firstOrCreate( [ 'scope_type' => ScopeType::SYSTEM, 'scope_id' => null, ], [ 'name' => 'System Default', 'priority' => 0, ] ); } /** * Get or create workspace profile. */ public static function ensureWorkspace(int $workspaceId, ?int $parentProfileId = null): self { return static::firstOrCreate( [ 'scope_type' => ScopeType::WORKSPACE, 'scope_id' => $workspaceId, ], [ 'name' => "Workspace {$workspaceId}", 'parent_profile_id' => $parentProfileId, 'priority' => 0, ] ); } }