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

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

func (cli *Client) PutFile(ctx context.Context, rawpath, mime string, src io.Reader, size int64) error {
	var err error
	ref, err := ParsePath(rawpath)
	if err != nil {
		return err
	}
	uri := ref.FileEP()
	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, src)
	if err != nil {
		return err
	}
	if mime == "" {
		mime = "application/octet-stream"
	}
	req.Header.Set("User-Agent", cli.userAgent)
	req.Header.Set("Content-Type", mime)
	req.Header.Set("Content-Size", strconv.FormatInt(size, 10))
	resp, err := cli.httpClient.Do(req)
	if err != nil {
		return err
	}
	resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		err = fmt.Errorf("File not accepted, code %s", resp.Status)
		return err
	}
	return err
}
