package controlplane

type (
	Phase    string
	Scenario string
)

// Enums
// Update value list `XXXValues` when update a enum
const (
	PhaseCreated          Phase = "created"
	PhasePreviewPending   Phase = "preview_pending"
	PhasePreviewRunning   Phase = "preview_running"
	PhasePreviewSucceeded Phase = "preview_succeeded"
	PhasePreviewFailed    Phase = "preview_failed"
	PhaseApproving        Phase = "approving"
	PhaseApplyPending     Phase = "apply_pending"
	PhaseApplyRunning     Phase = "apply_running"
	PhaseApplySucceeded   Phase = "apply_succeeded"
	PhaseApplyFailed      Phase = "apply_failed"
	PhaseCanceled         Phase = "canceled"
)

var (
	PhaseValues = []Phase{
		PhaseCreated,
		PhasePreviewPending, PhasePreviewRunning, PhasePreviewSucceeded, PhasePreviewFailed,
		PhaseApproving,
		PhaseApplyPending, PhaseApplyRunning, PhaseApplySucceeded, PhaseApplyFailed,
		PhaseCanceled,
	}
	PhaseTerminalValues = []Phase{
		PhaseApplySucceeded, PhaseCanceled,
	}
)

func (p Phase) IsTerminal() bool {
	return p == PhaseApplySucceeded || p == PhaseCanceled
}

const (
	ScenarioAWSPremiumRegion Scenario = "aws_premium_region"
	ScenarioAWSBYOCRegion    Scenario = "aws_byoc_region"
	// Future scenarios:
	// ScenarioAzurePremiumRegion Scenario = "azure_premium_region"
	// ScenarioAzureBYOCRegion    Scenario = "azure_byoc_region"
	// ScenarioAliCloudPremiumRegion Scenario = "alicloud_premium_region"
)

var ScenarioValues = []Scenario{
	ScenarioAWSPremiumRegion,
	ScenarioAWSBYOCRegion,
}

// Deployment is the core entity for region deployment orchestration.
type Deployment struct {
	ID        uint64   `json:"id"`
	Scenario  Scenario `json:"scenario"`
	Phase     Phase    `json:"phase"`
	Applicant string   `json:"applicant"` // email

	// Params holds user-provided deployment parameters.
	Params map[string]string `json:"params"`

	// FrozenSnapshot is set when preview is dispatched, locking down the exact
	// workflow inputs used for preview. Apply always uses this snapshot.
	FrozenSnapshot *FrozenSnapshot `json:"frozen_snapshot,omitempty"`

	// PreviewMeta holds the GitHub Actions workflow run info for preview.
	PreviewMeta *WorkflowRunMeta `json:"preview_meta,omitempty"`
	// ApplyMeta holds the GitHub Actions workflow run info for apply.
	ApplyMeta *WorkflowRunMeta `json:"apply_meta,omitempty"`

	ApprovalID *uint64 `json:"approval_id,omitempty"`
	CreatedAt  int64   `json:"created_at"`
	ClosedAt   *int64  `json:"closed_at,omitempty"`
}

// FrozenSnapshot captures the exact parameters and workflow inputs that were
// sent to the preview workflow. Apply must use this snapshot verbatim.
type FrozenSnapshot struct {
	Params         map[string]string `json:"params"`
	WorkflowName   string            `json:"workflow_name"`
	WorkflowInputs map[string]string `json:"workflow_inputs"`
	FrozenAt       int64             `json:"frozen_at"`
}

// WorkflowRunMeta tracks a single GitHub Actions workflow run.
type WorkflowRunMeta struct {
	WorkflowName string `json:"workflow_name"`
	RunID        int64  `json:"run_id"`
	RunURL       string `json:"run_url"`
	Status       string `json:"status"`
	Summary      string `json:"summary,omitempty"`
	RetryCount   int    `json:"retry_count,omitempty"`
}
