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

package repocli

import (
	"github.com/stretchr/testify/require"

	"bytes"
	"context"
	"fmt"
	"math/rand"
	"testing"
	"time"

	"mstore/pkg/digest"
)

func xxxTestClientGetUpload(t *testing.T) {
	rawrepos := []string{
		"localhost:1025/alpine:3.20.0",
	}
	mw := NewBasicAuthMiddleware("mstore", "mstore")
	cli := NewClient(nil, mw)

	for _, rawrepo := range rawrepos {
		var err error
		var loc string
		{
			ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
			id, loc, err := cli.GetUpload(ctx, rawrepo)
			require.NoError(t, err)
			fmt.Printf("Upload Location: %s\n", loc)
			fmt.Printf("Upload ID: %s\n", id)
		}
		{
			srcsize := 1024 + 145
			srcdata := make([]byte, srcsize)
			_, err = rand.Read(srcdata)
			require.NoError(t, err)

			src := bytes.NewReader(srcdata)
			//digest := SHA256Digest(srcdata)

			ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
			bloc, err := cli.PatchUpload(ctx, rawrepo, src, loc, int64(len(srcdata)))
			require.NoError(t, err)
			fmt.Printf("Path Location: %s\n", bloc)
		}
		{
			srcsize := 1024 + 145
			srcdata := make([]byte, srcsize)
			_, err = rand.Read(srcdata)
			require.NoError(t, err)

			reader := bytes.NewReader(srcdata)

			digobj := digest.NewDigest(digest.SHA256, srcdata)
			digstr := digobj.Encoded()
			size := int64(len(srcdata))

			ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
			bloc, err := cli.PutUpload(ctx, rawrepo, reader, loc, digstr, size)
			require.NoError(t, err)
			fmt.Printf("Put blob Location: %s\n", bloc)
		}

	}
}
