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

import (
	"context"
	"time"

	"github.com/spf13/cobra"

	"mstore/pkg/descr"
	"mstore/pkg/repocli"
)

// ImageTags
type ImageTagsParams struct {
	Imagepath string
}

type ImageTagsResult struct {
	Tags *descr.Tags `json:"imageTags"`
}

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

func (util *ImageUtil) imageTags(common *CommonImageParams, params *ImageTagsParams) (*ImageTagsResult, error) {
	var err error
	res := &ImageTagsResult{
		Tags: &descr.Tags{},
	}

	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)
	opres, err := cli.GetTags(ctx, ref.Raw())
	if err != nil {
		return res, err
	}
	res.Tags = opres
	return res, err
}
