package job

import (
	"context"
	"fmt"
	"strings"

	jobdom "haixun-backend/internal/model/job/domain/usecase"
	threadsaccountdomain "haixun-backend/internal/model/threads_account/domain/usecase"
)

type RefreshThreadsTokenDeps struct {
	Jobs           jobdom.UseCase
	ThreadsAccount threadsaccountdomain.UseCase
}

func RegisterRefreshThreadsTokenHandler(runner *Runner, deps RefreshThreadsTokenDeps) {
	if runner == nil {
		return
	}
	runner.RegisterStepHandler("refresh", func(ctx context.Context, step StepContext) error {
		return runRefreshThreadsToken(ctx, step, deps)
	})
}

func runRefreshThreadsToken(ctx context.Context, step StepContext, deps RefreshThreadsTokenDeps) error {
	payload := step.Run.Payload
	tenantID, ownerUID := runActorFromPayload(payload, step.Run)
	accountID := strings.TrimSpace(stringField(payload, "account_id"))
	if tenantID == "" || ownerUID == "" || accountID == "" {
		return fmt.Errorf("refresh-threads-token payload missing tenant_id, owner_uid, or account_id")
	}

	saved, err := deps.ThreadsAccount.RefreshAccountAPIAccessToken(ctx, tenantID, ownerUID, accountID)
	if err != nil {
		return fmt.Errorf("refresh account token: %w", err)
	}

	_, err = deps.Jobs.CompleteRun(ctx, jobdom.CompleteRunRequest{
		JobID:    step.JobID,
		WorkerID: step.WorkerID,
		Result: map[string]any{
			"account_id": accountID,
			"expires_at": saved.APITokenExpiresAt,
		},
	})
	return err
}
