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

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

type PatchUploadParams struct {
	ContentType   string
	ContentLength string
	ContentRange  string
	Name          string
	Reference     string
	Reader        io.Reader
}
type PatchUploadResult struct {
	Location string
	Range    string
}

// TODO: partial uploading by range? Does anyone use this?
func (oper *Operator) PatchUpload(ctx context.Context, operatorID string, params *PatchUploadParams) (*PatchUploadResult, int, error) {
	var err error
	res := &PatchUploadResult{}

	if params.Reference == "" {
		err = fmt.Errorf("Empty reference")
		return res, http.StatusBadRequest, err
	}
	if params.Name == "" {
		err = fmt.Errorf("Empty name")
		return res, http.StatusBadRequest, err
	}

	exists, uploadSize, err := oper.store.UploadExists(params.Name, params.Reference)
	if err != nil {
		return res, http.StatusInternalServerError, err
	}
	if exists {
		res.Location = fmt.Sprintf("/v2/%s/uploads/%s", params.Name, params.Reference)
		res.Range = fmt.Sprintf("0-%d", uploadSize-1)
		return res, http.StatusNoContent, err
	}

	if params.ContentType != "application/octet-stream" {
		err = fmt.Errorf("Wrong Conten-Type header: %s", params.ContentType)
		return res, http.StatusBadRequest, err
	}
	var contentLength int64

	// Unfortunately, podman and github.com/containers/image don't sent
	// Content-Length header for docker transport

	if params.ContentLength != "" {
		contentLength, err = strconv.ParseInt(params.ContentLength, 10, 64)
		if err != nil {
			err = fmt.Errorf("Wrong Content-length header")
			return res, http.StatusBadRequest, err
		}
	}

	recsize, err := oper.store.WriteUpload(params.Reference, params.Reader)
	if err != nil {
		return res, http.StatusInternalServerError, err
	}
	if contentLength != 0 && recsize != contentLength {
		oper.store.RemoveUpload(params.Reference)
		err = fmt.Errorf("Mismatch upload recorded size and content length")
		return res, http.StatusInternalServerError, err
	}
	res.Location = fmt.Sprintf("/v2/%s/uploads/%s", params.Name, params.Reference)
	res.Range = fmt.Sprintf("0-%d", recsize-1)
	return res, http.StatusAccepted, err
}
