package images

import (
	"context"
	"encoding/json"
	"floares/config"
	"fmt"
	"log"
	"net/http"
)

type DeleteRsp struct {
	Cause    string `json:"cause"` //cause image is in use by a container
	Message  string `json:"message"`
	Response int    `json:"response"`
}
type DeleteRsult struct {
	Code  int    `json:"code"`
	Error string `json:"err,omitempty"`
}

func Delete(id string) DeleteRsult {
	req, _ := http.NewRequestWithContext(context.Background(), http.MethodDelete, config.Entrypoint+fmt.Sprintf("/images/%s", id), nil)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return DeleteRsult{
			Error: err.Error(),
		}
	}
	var deleteRsult DeleteRsp
	var response DeleteRsult
	response.Code = resp.StatusCode
	_ = json.NewDecoder(resp.Body).Decode(&deleteRsult)
	log.Println(deleteRsult)
	if deleteRsult.Cause != "" {
		return DeleteRsult{
			Error: deleteRsult.Cause,
		}
	}
	return response
}
