package threads_account

import (
	domusecase "haixun-backend/internal/model/threads_account/domain/usecase"
	"haixun-backend/internal/types"
)

func toThreadsAccountData(item domusecase.AccountSummary) types.ThreadsAccountData {
	return types.ThreadsAccountData{
		ID:               item.ID,
		DisplayName:      item.DisplayName,
		Username:         item.Username,
		ThreadsUserID:    item.ThreadsUserID,
		PersonaID:        item.PersonaID,
		BrowserConnected: item.BrowserConnected,
		ApiConnected:     item.ApiConnected,
		Status:           item.Status,
		CreateAt:         item.CreateAt,
		UpdateAt:         item.UpdateAt,
	}
}

func toListData(result *domusecase.ListResult) *types.ListThreadsAccountsData {
	if result == nil {
		return &types.ListThreadsAccountsData{List: []types.ThreadsAccountData{}}
	}
	list := make([]types.ThreadsAccountData, 0, len(result.List))
	for _, item := range result.List {
		list = append(list, toThreadsAccountData(item))
	}
	return &types.ListThreadsAccountsData{
		List:            list,
		ActiveAccountID: result.ActiveAccountID,
	}
}

func toConnectionData(data *domusecase.ConnectionData) *types.ThreadsAccountConnectionData {
	if data == nil {
		return nil
	}
	return &types.ThreadsAccountConnectionData{
		AccountID:        data.AccountID,
		AccountName:      data.AccountName,
		Username:         data.Username,
		BrowserConnected: data.BrowserConnected,
		ApiConnected:     data.ApiConnected,
		Prefs: types.ThreadsAccountConnectionPrefs{
			SearchViaApi:     data.Prefs.SearchViaApi,
			SearchSourceMode: data.Prefs.SearchSourceMode,
			PublishViaApi:    data.Prefs.PublishViaApi,
			DevMode:          data.Prefs.DevMode,
			ScrapeReplies:    data.Prefs.ScrapeReplies,
			RepliesPerPost:   data.Prefs.RepliesPerPost,
			PublishHeaded:    data.Prefs.PublishHeaded,
			PlaywrightDebug:  data.Prefs.PlaywrightDebug,
		},
	}
}

func toConnectionPatch(req *types.UpdateThreadsAccountConnectionReq) domusecase.ConnectionPrefsPatch {
	if req == nil {
		return domusecase.ConnectionPrefsPatch{}
	}
	return domusecase.ConnectionPrefsPatch{
		SearchViaApi:     req.SearchViaApi,
		SearchSourceMode: req.SearchSourceMode,
		PublishViaApi:    req.PublishViaApi,
		DevMode:          req.DevMode,
		ScrapeReplies:    req.ScrapeReplies,
		RepliesPerPost:   req.RepliesPerPost,
		PublishHeaded:    req.PublishHeaded,
		PlaywrightDebug:  req.PlaywrightDebug,
	}
}
