package activity

import (
	"context"
	"strings"

	"hyapp/pkg/xerr"
	activitydomain "hyapp/services/activity-service/internal/domain/activity"
)

// Repository 隔离活动状态读取。
type Repository interface {
	GetActivity(ctx context.Context, activityID string) (activitydomain.Activity, error)
}

// Config 保存活动服务底座配置。
type Config struct {
	NodeID string
}

// Service 承载活动同步查询用例。
type Service struct {
	cfg        Config
	repository Repository
}

// New 创建活动服务。
func New(cfg Config, repository Repository) *Service {
	return &Service{cfg: cfg, repository: repository}
}

// Ping 返回当前节点可接受同步 RPC。
func (s *Service) Ping() (string, error) {
	if strings.TrimSpace(s.cfg.NodeID) == "" {
		return "", xerr.New(xerr.Unavailable, "activity node_id is not configured")
	}

	return s.cfg.NodeID, nil
}

// GetActivityStatus 查询活动底座状态。
func (s *Service) GetActivityStatus(ctx context.Context, activityID string) (activitydomain.Activity, error) {
	if strings.TrimSpace(activityID) == "" {
		return activitydomain.Activity{}, xerr.New(xerr.InvalidArgument, "activity_id is required")
	}
	if s.repository == nil {
		return activitydomain.Activity{}, xerr.New(xerr.Unavailable, "activity repository is not configured")
	}

	return s.repository.GetActivity(ctx, activityID)
}
