package steps

import (
	"context"
	"fmt"

	"github.com/pingcap/test-infra/caselib/pkg/host"
)

type showDDLLocksStep struct {
	dmHost     *host.DMHost
	sourceName string
	taskName   string
}

func (s *showDDLLocksStep) Execute(ctx context.Context) error {
	err, _ := s.dmHost.ShowDDLLocks(s.sourceName, s.taskName)
	if err != nil {
		return err
	}
	return nil
}

func (s *showDDLLocksStep) String() string {
	return fmt.Sprintf("show ddl lock by source-name: %s, task-name: %s", s.sourceName, s.taskName)
}

func (b *Builder) ShowDDLLocks(dmHost *host.DMHost, sourceName, taskName string) *Builder {
	b.steps = append(b.steps, &showDDLLocksStep{
		dmHost:     dmHost,
		sourceName: sourceName,
		taskName:   taskName,
	})
	return b
}

type unlockDDLLockStep struct {
	dmHost      *host.DMHost
	sourceName  string
	taskName    string
	forceRemove bool
}

func (s *unlockDDLLockStep) Execute(ctx context.Context) error {
	err := s.dmHost.UnlockDDLLock(s.sourceName, s.taskName, s.forceRemove)
	if err != nil {
		return err
	}
	return nil
}

func (s *unlockDDLLockStep) String() string {
	return fmt.Sprintf("unlock ddl lock by source: %s, task: %s, force-remove: %v", s.sourceName, s.taskName, s.forceRemove)
}

func (b *Builder) UnlockDDLLock(dmHost *host.DMHost, sourceName, taskName string, forceRemove bool) *Builder {
	b.steps = append(b.steps, &unlockDDLLockStep{
		dmHost:      dmHost,
		sourceName:  sourceName,
		taskName:    taskName,
		forceRemove: forceRemove,
	})
	return b
}
