package service

import (
	"context"
	"fmt"

	"oci-portal/internal/oci"
)

// Subscriptions 列出租户全部订阅摘要。
func (s *OciConfigService) Subscriptions(ctx context.Context, id uint) ([]oci.SubscriptionInfo, error) {
	cred, err := s.credentialsByID(ctx, id)
	if err != nil {
		return nil, err
	}
	return s.client.ListSubscriptions(ctx, cred)
}

// SubscriptionDetail 查询租户单个订阅的完整信息。
func (s *OciConfigService) SubscriptionDetail(ctx context.Context, id uint, subscriptionID string) (oci.SubscriptionDetail, error) {
	if subscriptionID == "" {
		return oci.SubscriptionDetail{}, fmt.Errorf("subscription detail: subscription id is empty")
	}
	cred, err := s.credentialsByID(ctx, id)
	if err != nil {
		return oci.SubscriptionDetail{}, err
	}
	return s.client.GetSubscription(ctx, cred, subscriptionID)
}

// LimitServices 列出配额体系支持的全部服务。
func (s *OciConfigService) LimitServices(ctx context.Context, id uint, region string) ([]oci.LimitService, error) {
	cred, err := s.credentialsByID(ctx, id)
	if err != nil {
		return nil, err
	}
	return s.client.ListLimitServices(ctx, cred, region)
}

// credentialsByID 加载配置快照并解密出完整凭据。
func (s *OciConfigService) credentialsByID(ctx context.Context, id uint) (oci.Credentials, error) {
	cfg, err := s.Get(ctx, id)
	if err != nil {
		return oci.Credentials{}, err
	}
	return s.credentialsOf(cfg)
}
