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

package repocli

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

func (cli *Client) DeleteImage(ctx context.Context, rawrepo string) (bool, error) {
	return cli.DeleteManifest(ctx, rawrepo)
}

func (cli *Client) DeleteManifest(ctx context.Context, rawrepo string) (bool, error) {
	var err error
	var exist bool

	ref, err := NewReferer(rawrepo)
	if err != nil {
		return exist, err
	}
	uri := ref.ManifestEP()
	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
	if err != nil {
		return exist, err
	}
	req.Header.Set("User-Agent", cli.userAgent)
	req.Header.Set("Accept", "*/*")
	resp, err := cli.httpClient.Do(req)
	if err != nil {
		return exist, err
	}
	defer resp.Body.Close()
	if resp.StatusCode == http.StatusNotFound {
		return exist, err
	}
	if resp.StatusCode != http.StatusAccepted {
		err := fmt.Errorf("Unxected response code %s", resp.Status)
		return exist, err
	}
	exist = true
	return exist, err
}
