/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 *
 * This work is published and licensed under a Creative Commons
 * Attribution-NonCommercial-NoDerivatives 4.0 International License.
 *
 * Distribution of this work is permitted, but commercial use and
 * modifications are strictly prohibited.
 */
package imagecmd

import (
	"context"
	"time"

	"github.com/spf13/cobra"

	"mstore/pkg/repocli"
)

// ImageTags
type ImageTagsParams struct {
	Imagepath string
}

type ImageTagsResult struct {
	ImageTags []string `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{
		ImageTags: make([]string, 0),
	}

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