package aws

import (
	"context"
	"fmt"

	"github.com/tidbcloud/controlplane9/internal/cluster"
	"github.com/tidbcloud/controlplane9/internal/host"
)

// CrossplaneInstallStep establishes the initial in-cluster C2 scaffolding.
// This PR intentionally keeps the install surface small: namespace + config
// markers that later PRs can replace with the full provider package wiring
// without changing the lifecycle contract.
type CrossplaneInstallStep struct {
	Selected *host.Session
}

func (s *CrossplaneInstallStep) Name() cluster.StepName { return cluster.StepCrossplaneInstall }

func (s *CrossplaneInstallStep) Run(ctx context.Context) error {
	if s.Selected == nil {
		return fmt.Errorf("selected host is required")
	}
	manifest := `apiVersion: v1
kind: Namespace
metadata:
  name: crossplane-system
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cp9-crossplane-bootstrap
  namespace: crossplane-system
data:
  installer: cp9
  providerFamily: aws
`
	output, err := host.RunKubectl(ctx, s.Selected, []byte(manifest), "apply", "-f", "-")
	if err != nil {
		return fmt.Errorf("kubectl apply: %w\n%s", err, string(output))
	}
	return nil
}
