// ABOUTME: Deletion planning types for EdgeConnect delete command
// ABOUTME: Defines structures for deletion plans and deletion results
package v1

import (
	"fmt"
	"strings"
	"time"
)

// DeletionPlan represents the complete deletion plan for a configuration
type DeletionPlan struct {
	// ConfigName is the name from metadata
	ConfigName string

	// AppToDelete defines the app that will be deleted (nil if app doesn't exist)
	AppToDelete *AppDeletion

	// InstancesToDelete defines the instances that will be deleted
	InstancesToDelete []InstanceDeletion

	// Summary provides a human-readable summary of the plan
	Summary string

	// TotalActions is the count of all actions that will be performed
	TotalActions int

	// EstimatedDuration is the estimated time to complete the deletion
	EstimatedDuration time.Duration

	// CreatedAt timestamp when the plan was created
	CreatedAt time.Time

	// DryRun indicates if this is a dry-run plan
	DryRun bool
}

// AppDeletion represents an application to be deleted
type AppDeletion struct {
	// Name of the application
	Name string

	// Version of the application
	Version string

	// Organization that owns the app
	Organization string

	// Region where the app is deployed
	Region string
}

// InstanceDeletion represents an application instance to be deleted
type InstanceDeletion struct {
	// Name of the instance
	Name string

	// Organization that owns the instance
	Organization string

	// Region where the instance is deployed
	Region string

	// CloudletOrg that hosts the cloudlet
	CloudletOrg string

	// CloudletName where the instance is running
	CloudletName string
}

// DeletionResult represents the result of a deletion operation
type DeletionResult struct {
	// Plan that was executed
	Plan *DeletionPlan

	// Success indicates if the deletion was successful
	Success bool

	// CompletedActions lists actions that were successfully completed
	CompletedActions []DeletionActionResult

	// FailedActions lists actions that failed
	FailedActions []DeletionActionResult

	// Error that caused the deletion to fail (if any)
	Error error

	// Duration taken to execute the plan
	Duration time.Duration
}

// DeletionActionResult represents the result of executing a single deletion action
type DeletionActionResult struct {
	// Type of resource that was deleted ("app" or "instance")
	Type string

	// Target describes what was being deleted
	Target string

	// Success indicates if the action succeeded
	Success bool

	// Error if the action failed
	Error error

	// Duration taken to complete the action
	Duration time.Duration
}

// IsEmpty returns true if the deletion plan has no actions to perform
func (dp *DeletionPlan) IsEmpty() bool {
	return dp.AppToDelete == nil && len(dp.InstancesToDelete) == 0
}

// GenerateSummary creates a human-readable summary of the deletion plan
func (dp *DeletionPlan) GenerateSummary() string {
	if dp.IsEmpty() {
		return "No resources found to delete"
	}

	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("Deletion plan for '%s':\n", dp.ConfigName))

	// Instance actions
	if len(dp.InstancesToDelete) > 0 {
		sb.WriteString(fmt.Sprintf("- DELETE %d instance(s)\n", len(dp.InstancesToDelete)))
		cloudletSet := make(map[string]bool)
		for _, inst := range dp.InstancesToDelete {
			key := fmt.Sprintf("%s:%s", inst.CloudletOrg, inst.CloudletName)
			cloudletSet[key] = true
		}
		sb.WriteString(fmt.Sprintf("  Across %d cloudlet(s)\n", len(cloudletSet)))
	}

	// App action
	if dp.AppToDelete != nil {
		sb.WriteString(fmt.Sprintf("- DELETE application '%s' version %s\n",
			dp.AppToDelete.Name, dp.AppToDelete.Version))
	}

	sb.WriteString(fmt.Sprintf("Estimated duration: %s", dp.EstimatedDuration.String()))

	return sb.String()
}

// Validate checks if the deletion plan is valid
func (dp *DeletionPlan) Validate() error {
	if dp.ConfigName == "" {
		return fmt.Errorf("deletion plan must have a config name")
	}

	if dp.IsEmpty() {
		return fmt.Errorf("deletion plan has no resources to delete")
	}

	return nil
}
