/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package imagecmd

import (
	"context"
	"time"

	"github.com/spf13/cobra"

	"mstore/pkg/repocli"
)

// DeleteImage
type DeleteImageParams struct {
	Imagepath string
}

type DeleteImageResult struct {
}

func (util *ImageUtil) DeleteImage(cmd *cobra.Command, args []string) {
	util.deleteImageParams.Imagepath = args[0]
	res, err := util.deleteImage(&util.commonImageParams, &util.deleteImageParams)
	printResponse(res, err)
}

func (util *ImageUtil) deleteImage(common *CommonImageParams, params *DeleteImageParams) (*DeleteImageResult, error) {
	var err error
	res := &DeleteImageResult{}

	timeout := time.Duration(common.Timeout) * time.Second
	ctx, _ := context.WithTimeout(context.Background(), timeout)

	ref, err := repocli.NewReferer(params.Imagepath)
	if err != nil {
		return res, err
	}
	ref.SetUserinfo(common.Username, common.Password)
	mw := repocli.NewBasicAuthMiddleware(ref.Userinfo())
	cli := repocli.NewClient(nil, mw)
	_, err = cli.DeleteImage(ctx, ref.Raw())
	if err != nil {
		return res, err
	}

	return res, err
}
