package threads_account

import (
	"context"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type RunThreadsAPISmokeTestLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRunThreadsAPISmokeTestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RunThreadsAPISmokeTestLogic {
	return &RunThreadsAPISmokeTestLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *RunThreadsAPISmokeTestLogic) RunThreadsAPISmokeTest(req *types.ThreadsAccountPath) (*types.RunThreadsAPISmokeTestData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	items, err := l.svcCtx.ThreadsAccount.RunAPISmokeTest(l.ctx, tenantID, uid, req.ID)
	if err != nil {
		return nil, err
	}
	list := make([]types.ThreadsAPISmokeTestItem, 0, len(items))
	var passed, failed, skipped int
	for _, item := range items {
		list = append(list, types.ThreadsAPISmokeTestItem{
			Scope: item.Scope, Name: item.Name, Status: item.Status,
			Message: item.Message, Count: item.Count,
		})
		switch item.Status {
		case "ok":
			passed++
		case "error":
			failed++
		default:
			skipped++
		}
	}
	return &types.RunThreadsAPISmokeTestData{
		List:    list,
		Passed:  passed,
		Failed:  failed,
		Skipped: skipped,
		Total:   len(list),
	}, nil
}
