uuid)) { $t->uuid = (string) Str::uuid(); } }); } public function area(): BelongsTo { return $this->belongsTo(ServiceArea::class, 'area_id'); } public function driver(): BelongsTo { return $this->belongsTo(User::class, 'driver_id'); } public function scanner(): BelongsTo { return $this->belongsTo(User::class, 'scanner_id'); } public function truck(): BelongsTo { return $this->belongsTo(Truck::class, 'truck_id'); } public function members(): HasMany { return $this->hasMany(TeamMember::class, 'team_id'); } public function helpers(): HasMany { return $this->members()->where('role_in_team', TeamMember::ROLE_HELPER); } public function trips(): HasMany { return $this->hasMany(Trip::class, 'team_id'); } public function currentTrip(): HasOne { return $this->hasOne(Trip::class, 'team_id') ->whereIn('status', [Trip::STATUS_IN_PROGRESS, Trip::STATUS_AT_DUMPSITE]); } public function collectionLogs(): HasMany { return $this->hasMany(CollectionLog::class, 'team_id'); } public function getIsFullAttribute(): bool { // A team is considered "Full" if it has a Driver, a Scanner, // and the number of helpers matches or exceeds the desired helper_count. $hasCore = $this->driver_id && $this->scanner_id; $helpersMet = $this->helpers()->count() >= ($this->helper_count ?: 2); return $hasCore && $helpersMet; } public function getPerformanceStats(): array { $today = now()->startOfDay(); $week = now()->startOfWeek(); return [ 'current_trip_qr' => $this->currentTrip?->collectionLogs()->count() ?? 0, 'current_trip_kg' => (int) ($this->currentTrip?->total_load_kg ?? 0), 'daily_qr' => $this->collectionLogs()->where('scanned_at', '>=', $today)->count(), 'weekly_qr' => $this->collectionLogs()->where('scanned_at', '>=', $week)->count(), 'total_qr' => $this->collectionLogs()->count(), ]; } }