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

import (
	"bytes"
	"context"
	"fmt"
	"net/http"
	"strconv"
)

func (cli *Client) DeleteCollection(ctx context.Context, rawpath string) ([]byte, error) {
	var err error
	var list []byte

	ref, err := ParsePath(rawpath)
	if err != nil {
		return list, err
	}
	uri := ref.CollectionEP()
	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
	if err != nil {
		return list, err
	}
	req.Header.Set("User-Agent", cli.userAgent)
	req.Header.Set("Accept", "*/*")
	resp, err := cli.httpClient.Do(req)
	if err != nil {
		return list, err
	}
	defer resp.Body.Close()
	if resp.StatusCode == http.StatusNotFound {
		return list, err
	}
	if resp.StatusCode != http.StatusOK {
		err := fmt.Errorf("Unxected response code %s", resp.Status)
		return list, err
	}
	contentLength := resp.Header.Get("Content-Length")
	blobSize, err := strconv.ParseInt(contentLength, 10, 64)
	if err != nil {
		return list, err
	}
	buffer := bytes.NewBuffer(nil)
	recSize, err := Copy(ctx, buffer, resp.Body)
	if blobSize != recSize {
		err := fmt.Errorf("Mismatch declared and actual body size, %d and %d", blobSize, recSize)
		return list, err
	}
	list = buffer.Bytes()
	return list, err
}
