package provision

import (
	"context"
	"errors"
)

// Region is a vendor region/datacenter descriptor.
type Region struct {
	// ID is the vendor-native region identifier (e.g. "hkg", "nbg1").
	ID string
	// Country is the ISO-3166 alpha-2 country code (e.g. "HK", "JP").
	Country string
	// City is a human-readable city name (optional).
	City string
}

// Instance is the vendor-side view of a freshly created machine.
type Instance struct {
	// ID is the vendor-native instance identifier (used to destroy / rotate).
	ID string
	// IP is the public IPv4 address.
	IP string
	// Region echoes the region the instance was created in.
	Region string
}

// CreateInput carries everything an adapter needs to boot one node.
type CreateInput struct {
	// Region is the vendor-native region ID.
	Region string
	// Plan is the vendor-native instance size ID.
	Plan string
	// Label is a vendor-side label/hostname (we pass the node UUID).
	Label string
	// UserData is the rendered cloud-init document (base64-encoded by the
	// adapter if the vendor requires it). It carries the one-time bootstrap
	// token, so it MUST NOT be logged.
	UserData string
	// SSHKeyIDs are optional vendor-registered SSH key IDs.
	SSHKeyIDs []string
	// Tags are optional vendor-side tags.
	Tags []string
}

// CloudAdapter is the unified interface every vendor adapter implements
// (doc/04 §4: "Terraform + 厂商 API 适配层"). Implementations live in
// providers/ and read their credentials only from independent secrets.
type CloudAdapter interface {
	// Kind returns the adapter key matching providers.api_kind.
	Kind() string
	// CreateInstance boots a node and returns once the vendor has assigned an
	// instance ID and IP (not necessarily once the OS is up).
	CreateInstance(ctx context.Context, in CreateInput) (*Instance, error)
	// DestroyInstance tears down the instance and releases its primary IP.
	// It must be idempotent: destroying an already-gone instance is a no-op.
	DestroyInstance(ctx context.Context, instanceID string) error
	// AttachIP allocates a fresh elastic IP, binds it to the instance, and
	// returns the new public IP. Only meaningful when SupportsElasticIP.
	AttachIP(ctx context.Context, instanceID string) (newIP string, err error)
	// ListRegions enumerates the vendor's regions.
	ListRegions(ctx context.Context) ([]Region, error)
	// SupportsElasticIP reports whether RotateIP (change IP, keep the box) is
	// available for this vendor.
	SupportsElasticIP() bool
}

// AdapterFactory resolves the CloudAdapter for a given provider row.
// providers/Registry implements it; tests inject a fake.
type AdapterFactory interface {
	For(p *Provider) (CloudAdapter, error)
}

// ErrElasticIPUnsupported is returned by RotateIP when the provider's adapter
// does not support elastic IPs.
var ErrElasticIPUnsupported = errors.New("provision: provider does not support elastic IP")

// ErrNoCredentials signals an adapter could not find its credentials in the
// configured secrets (env/file).
var ErrNoCredentials = errors.New("provision: vendor credentials not configured")
