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

import (
	"context"
	"time"

	"github.com/spf13/cobra"

	"mstore/pkg/filecli"
)

// DeleteFile
type DeleteFileParams struct {
	Filepath string
}

type DeleteFileResult struct{}

func (util *FileUtil) DeleteFile(cmd *cobra.Command, args []string) {
	util.deleteFileParams.Filepath = args[0]
	res, err := util.deleteFile(&util.commonFileParams, &util.deleteFileParams)
	printResponse(res, err)
}
func (util *FileUtil) deleteFile(common *CommonFileParams, params *DeleteFileParams) (*DeleteFileResult, error) {
	var err error
	res := &DeleteFileResult{}
	ref, err := filecli.ParsePath(params.Filepath)
	if err != nil {
		return res, err
	}
	timeout := time.Duration(common.Timeout) * time.Second
	ctx, _ := context.WithTimeout(context.Background(), timeout)
	ref.SetUserinfo(common.Username, common.Password)

	mw := filecli.NewBasicAuthMiddleware(ref.Userinfo())
	cli := filecli.NewClient(nil, mw)
	_, err = cli.DeleteFile(ctx, ref.Raw())
	if err != nil {
		return res, err
	}
	return res, err
}
