streams() ->with('bucket:id,uuid,name') ->orderBy('type') ->orderBy('name') ->get(); } public function create(Scenario $scenario, array $data): Stream { $this->resolveBucketId($data); return $scenario->streams()->create($data); } public function update(Stream $stream, array $data): Stream { $this->resolveBucketId($data); $stream->update($data); return $stream->fresh('bucket'); } public function delete(Stream $stream): bool { return $stream->delete(); } public function toggleActive(Stream $stream): Stream { $stream->update([ 'is_active' => ! $stream->is_active, ]); return $stream->fresh('bucket'); } /** * Check if a bucket belongs to the scenario */ public function bucketBelongsToScenario(Scenario $scenario, ?string $bucketUuid): bool { if (! $bucketUuid) { return true; } return $scenario->buckets() ->where('uuid', $bucketUuid) ->exists(); } private function resolveBucketId(array &$data): void { if (! empty($data['bucket_id'])) { $data['bucket_id'] = Bucket::where('uuid', $data['bucket_id'])->value('id'); } } /** * Get streams grouped by type */ public function getGroupedByType(Scenario $scenario): array { return [ 'income' => $scenario->streams() ->with('bucket:id,uuid,name') ->byType(Stream::TYPE_INCOME) ->orderBy('name') ->get(), 'expense' => $scenario->streams() ->with('bucket:id,uuid,name') ->byType(Stream::TYPE_EXPENSE) ->orderBy('name') ->get(), ]; } /** * Get streams for a specific bucket */ public function getForBucket(int $bucketId): Collection { return Stream::where('bucket_id', $bucketId) ->where('is_active', true) ->get(); } }