//go:generate ${GOBIN}/mockgen -destination api_mock.go -package=api ${GO_MODULE}/pkg/service/controlplane/api DeploymentServiceAPI
package api

import (
	"context"

	cp "github.com/tidbcloud/devops/entity/controlplane"
)

type CreateDeploymentParam struct {
	Scenario cp.Scenario       `json:"scenario"`
	Params   map[string]string `json:"params"`
}

type PreviewResult struct {
	RunID   int64  `json:"run_id"`
	RunURL  string `json:"run_url"`
	Status  string `json:"status"`  // "succeeded" or "failed"
	Summary string `json:"summary"` // human-readable preview summary
}

type ApplyResult struct {
	RunID  int64  `json:"run_id"`
	RunURL string `json:"run_url"`
	Status string `json:"status"` // "succeeded" or "failed"
}

type ListDeploymentParam struct {
	Scenario *cp.Scenario `json:"scenario,omitempty"`
	Phase    *cp.Phase    `json:"phase,omitempty"`
}

type StartPreviewResult struct {
	RunID  int64  `json:"run_id"`
	RunURL string `json:"run_url"`
}

type DeploymentServiceAPI interface {
	// CreateDeployment creates a new deployment instance.
	CreateDeployment(ctx context.Context, param CreateDeploymentParam, applicant string) (*uint64, error)
	// StartPreview dispatches the preview workflow and returns the run ID and URL.
	StartPreview(ctx context.Context, id uint64) (*StartPreviewResult, error)
	// OnPreviewComplete is called when the preview workflow finishes.
	OnPreviewComplete(ctx context.Context, id uint64, result PreviewResult) error
	// StartApply dispatches the apply workflow after approval.
	StartApply(ctx context.Context, id uint64) error
	// OnApplyComplete is called when the apply workflow finishes.
	OnApplyComplete(ctx context.Context, id uint64, result ApplyResult) error
	// RetryApply retries a failed apply using the same frozen snapshot.
	RetryApply(ctx context.Context, id uint64) error
	// Cancel cancels a deployment if it is not in a terminal state.
	Cancel(ctx context.Context, id uint64) error
	// GetDeployment returns a single deployment by ID.
	GetDeployment(ctx context.Context, id uint64) (*cp.Deployment, error)
	// ListDeployments returns deployments matching the given criteria.
	ListDeployments(ctx context.Context, param ListDeploymentParam) ([]*cp.Deployment, error)
}
