package cli

import (
	"context"
	"fmt"
	"os"

	"edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/core/domain"
	"github.com/spf13/cobra"
)

var (
	cloudletName string
	cloudletOrg  string
	instanceName string
	flavorName   string
)

var appInstanceCmd = &cobra.Command{
	Use:   "instance",
	Short: "Manage Edge Connect application instances",
	Long:  `Create, show, list, and delete Edge Connect application instances.`,
}

var createInstanceCmd = &cobra.Command{
	Use:   "create",
	Short: "Create a new Edge Connect application instance",
	Run: func(cmd *cobra.Command, args []string) {
		appInst := &domain.AppInstance{
			Key: domain.AppInstanceKey{
				Organization: organization,
				Name:         instanceName,
				CloudletKey: domain.CloudletKey{
					Organization: cloudletOrg,
					Name:         cloudletName,
				},
			},
			AppKey: domain.AppKey{
				Organization: organization,
				Name:         appName,
				Version:      appVersion,
			},
			Flavor: domain.Flavor{
				Name: flavorName,
			},
		}

		err := services.InstanceService.CreateAppInstance(context.Background(), region, appInst)
		if err != nil {
			fmt.Printf("Error creating app instance: %v\n", err)
			os.Exit(1)
		}
		fmt.Println("Application instance created successfully")
	},
}

var showInstanceCmd = &cobra.Command{
	Use:   "show",
	Short: "Show details of an Edge Connect application instance",
	Run: func(cmd *cobra.Command, args []string) {
		instanceKey := domain.AppInstanceKey{
			Organization: organization,
			Name:         instanceName,
			CloudletKey: domain.CloudletKey{
				Organization: cloudletOrg,
				Name:         cloudletName,
			},
		}

		instance, err := services.InstanceService.ShowAppInstance(context.Background(), region, instanceKey)
		if err != nil {
			fmt.Printf("Error showing app instance: %v\n", err)
			os.Exit(1)
		}
		fmt.Printf("Application instance details:\n%+v\n", instance)
	},
}

var listInstancesCmd = &cobra.Command{
	Use:   "list",
	Short: "List Edge Connect application instances",
	Run: func(cmd *cobra.Command, args []string) {
		instanceKey := domain.AppInstanceKey{
			Organization: organization,
			Name:         instanceName,
			CloudletKey: domain.CloudletKey{
				Organization: cloudletOrg,
				Name:         cloudletName,
			},
		}

		instances, err := services.InstanceService.ShowAppInstances(context.Background(), region, instanceKey)
		if err != nil {
			fmt.Printf("Error listing app instances: %v\n", err)
			os.Exit(1)
		}
		fmt.Println("Application instances:")
		for _, instance := range instances {
			fmt.Printf("%+v\n", instance)
		}
	},
}

var deleteInstanceCmd = &cobra.Command{
	Use:   "delete",
	Short: "Delete an Edge Connect application instance",
	Run: func(cmd *cobra.Command, args []string) {
		instanceKey := domain.AppInstanceKey{
			Organization: organization,
			Name:         instanceName,
			CloudletKey: domain.CloudletKey{
				Organization: cloudletOrg,
				Name:         cloudletName,
			},
		}

		err := services.InstanceService.DeleteAppInstance(context.Background(), region, instanceKey)
		if err != nil {
			fmt.Printf("Error deleting app instance: %v\n", err)
			os.Exit(1)
		}
		fmt.Println("Application instance deleted successfully")
	},
}

func init() {
	rootCmd.AddCommand(appInstanceCmd)
	appInstanceCmd.AddCommand(createInstanceCmd, showInstanceCmd, listInstancesCmd, deleteInstanceCmd)

	// Add flags to all instance commands
	instanceCmds := []*cobra.Command{createInstanceCmd, showInstanceCmd, listInstancesCmd, deleteInstanceCmd}
	for _, cmd := range instanceCmds {
		cmd.Flags().StringVarP(&organization, "org", "o", "", "organization name (required)")
		cmd.Flags().StringVarP(&instanceName, "name", "n", "", "instance name (required)")
		cmd.Flags().StringVarP(&cloudletName, "cloudlet", "c", "", "cloudlet name (required)")
		cmd.Flags().StringVarP(&cloudletOrg, "cloudlet-org", "", "", "cloudlet organization (required)")
		cmd.Flags().StringVarP(&region, "region", "r", "", "region (required)")

		cmd.MarkFlagRequired("org")
		cmd.MarkFlagRequired("name")
		cmd.MarkFlagRequired("cloudlet")
		cmd.MarkFlagRequired("cloudlet-org")
		cmd.MarkFlagRequired("region")
	}

	// Add additional flags for create command
	createInstanceCmd.Flags().StringVarP(&appName, "app", "a", "", "application name (required)")
	createInstanceCmd.Flags().StringVarP(&appVersion, "version", "v", "", "application version")
	createInstanceCmd.Flags().StringVarP(&flavorName, "flavor", "f", "", "flavor name (required)")
	createInstanceCmd.MarkFlagRequired("app")
	createInstanceCmd.MarkFlagRequired("flavor")
}