package service import ( "bugulma/backend/internal/domain" "context" "encoding/json" "time" "github.com/google/uuid" "gorm.io/datatypes" ) // SharedAssetService handles business logic for shared assets type SharedAssetService struct { repo domain.SharedAssetRepository } // NewSharedAssetService creates a new shared asset service func NewSharedAssetService(repo domain.SharedAssetRepository) *SharedAssetService { return &SharedAssetService{repo: repo} } // CreateSharedAssetRequest represents the request to create a shared asset type CreateSharedAssetRequest struct { OwnerBusinessID string SiteID string Type domain.AssetType Description string Capacity float64 CapacityUnit string UtilizationRate float64 AvailabilityPeriod string OperationalStatus domain.OperationalStatus CostSharingModel string MaintenanceSchedule string } // Create creates a new shared asset func (s *SharedAssetService) Create(ctx context.Context, req CreateSharedAssetRequest) (*domain.SharedAsset, error) { currentUsers := []string{} currentUsersJSON, _ := json.Marshal(currentUsers) asset := &domain.SharedAsset{ ID: uuid.New().String(), OwnerBusinessID: req.OwnerBusinessID, SiteID: req.SiteID, Type: req.Type, Description: req.Description, Capacity: req.Capacity, CapacityUnit: req.CapacityUnit, UtilizationRate: req.UtilizationRate, AvailabilityPeriod: req.AvailabilityPeriod, CostSharingModel: req.CostSharingModel, CurrentUsers: datatypes.JSON(currentUsersJSON), MaintenanceSchedule: req.MaintenanceSchedule, OperationalStatus: req.OperationalStatus, CreatedAt: time.Now(), UpdatedAt: time.Now(), } if err := s.repo.Create(ctx, asset); err != nil { return nil, err } return asset, nil } // GetByID retrieves a shared asset by ID func (s *SharedAssetService) GetByID(ctx context.Context, id string) (*domain.SharedAsset, error) { return s.repo.GetByID(ctx, id) } // GetBySiteID retrieves shared assets at a specific site func (s *SharedAssetService) GetBySiteID(ctx context.Context, siteID string) ([]*domain.SharedAsset, error) { return s.repo.GetBySiteID(ctx, siteID) } // GetByOwnerBusinessID retrieves shared assets owned by a business func (s *SharedAssetService) GetByOwnerBusinessID(ctx context.Context, businessID string) ([]*domain.SharedAsset, error) { return s.repo.GetByOwnerID(ctx, businessID) } // GetAvailableAssets retrieves assets with available capacity func (s *SharedAssetService) GetAvailableAssets(ctx context.Context) ([]*domain.SharedAsset, error) { return s.repo.GetAvailable(ctx) } // Update updates a shared asset func (s *SharedAssetService) Update(ctx context.Context, asset *domain.SharedAsset) error { asset.UpdatedAt = time.Now() return s.repo.Update(ctx, asset) } // Delete deletes a shared asset func (s *SharedAssetService) Delete(ctx context.Context, id string) error { return s.repo.Delete(ctx, id) } // Helper functions to convert strings to domain types func AssetTypeFromString(s string) domain.AssetType { switch s { case "infrastructure": return domain.AssetTypeInfrastructure case "utilities": return domain.AssetTypeUtilities case "equipment": return domain.AssetTypeEquipment case "space": return domain.AssetTypeSpace default: return domain.AssetTypeEquipment } } func OperationalStatusFromString(s string) domain.OperationalStatus { switch s { case "operational": return domain.StatusOperational case "maintenance": return domain.StatusMaintenance case "out_of_service": return domain.StatusOutOfService default: return domain.StatusOperational } }