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

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

func (cli *Client) FileInfo(ctx context.Context, rawpath string) (bool, int64, string, error) {
	var err error
	var exist bool
	var size int64
	var digest string

	ref, err := ParsePath(rawpath)
	if err != nil {
		return exist, size, digest, err
	}
	uri := ref.FileEP()

	fmt.Println(uri)
	req, err := http.NewRequestWithContext(ctx, http.MethodHead, uri, nil)
	if err != nil {
		return exist, size, digest, err
	}
	req.Header.Set("User-Agent", cli.userAgent)
	req.Header.Set("Accept", "*/*")

	resp, err := cli.httpClient.Do(req)
	if err != nil {
		return exist, size, digest, err
	}
	defer resp.Body.Close()

	if resp.StatusCode == http.StatusNotFound {
		return exist, size, digest, err
	}
	if resp.StatusCode != http.StatusOK {
		err := fmt.Errorf("Unxected response code %s", resp.Status)
		return exist, size, digest, err
	}
	contentLength := resp.Header.Get("Content-Size")
	size, err = strconv.ParseInt(contentLength, 10, 64)
	if err != nil {
		return exist, size, digest, err
	}
	digest = resp.Header.Get("Content-Digest")
	exist = true
	return exist, size, digest, err
}
