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

package repocli

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"io"
	"net/http"
	"strconv"
	"strings"

	ocidigest "github.com/opencontainers/go-digest"
)

const (
	ddmMediaType  = "application/vnd.docker.distribution.manifest.v2+json"
	oimMediaType  = "application/vnd.oci.image.manifest.v1+json"
	ddmlMediaType = "application/vnd.docker.distribution.manifest.list.v2+json"
	oiiMeiaType   = "application/vnd.oci.image.index.v1+json"

	//application/vnd.docker.distribution.manifest.v1+json
	//application/vnd.docker.distribution.manifest.v1+prettyjws
)

func (cli *Client) GetRawManifest(ctx context.Context, rawrepo string, accepts []string) (bool, string, []byte, string, error) {
	var err error
	var exist bool
	var mime string
	var man []byte
	var digstr string

	ref, err := NewReferer(rawrepo)
	if err != nil {
		return exist, mime, man, digstr, err
	}
	uri := ref.ManifestEP()

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
	if err != nil {
		return exist, mime, man, digstr, err
	}
	req.Header.Set("User-Agent", cli.userAgent)
	if len(accepts) == 0 {
		accepts = []string{
			MediaTypeDDMLv2,
			MediaTypeDDMv2,
			MediaTypeOIMv1,
			MediaTypeOIIv1,
		}
	}
	req.Header.Set("Accept", strings.Join(accepts, ","))

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

	if resp.StatusCode == http.StatusNotFound {
		return exist, mime, man, digstr, err
	}
	if resp.StatusCode != http.StatusOK {
		err := fmt.Errorf("Unxected response code %s", resp.Status)
		return exist, mime, man, digstr, err
	}
	mime = resp.Header.Get("Content-Type")
	if mime == "" {
		err := fmt.Errorf("Empty MIME type declaration")
		return exist, mime, man, digstr, err
	}
	accepted := false
	for _, accept := range accepts {
		if mime == accept {
			accepted = true
		}
	}
	if !accepted {
		err := fmt.Errorf("Unknown content type: %s", mime)
		return exist, mime, man, digstr, err
	}
	digstr = resp.Header.Get("Docker-Content-Digest")
	if digstr == "" {
		err := fmt.Errorf("Empty manifest digest declaration")
		return exist, mime, man, digstr, err
	}
	contentLength := resp.Header.Get("Content-Length")
	if contentLength == "" {
		err = errors.New("Empty Content-Length header")
		return exist, mime, man, digstr, err
	}
	manSize, err := strconv.ParseInt(contentLength, 10, 64)
	if err != nil {
		return exist, mime, man, digstr, err
	}
	digstr = strings.ToLower(digstr)
	digobj, err := ocidigest.Parse(digstr)
	if err != nil {
		return exist, mime, man, digstr, err
	}
	verifier := digobj.Verifier()
	buffer := bytes.NewBuffer(nil)
	mwriter := io.MultiWriter(buffer, verifier)
	recSize, err := Copy(ctx, mwriter, resp.Body)
	if manSize != recSize {
		err := fmt.Errorf("Mismatch declared and actual size")
		return exist, mime, man, digstr, err
	}
	man = buffer.Bytes()
	if !verifier.Verified() {
		err := fmt.Errorf("Mismatch digest declaration and actual")
		return exist, mime, man, digstr, err
	}
	exist = true
	return exist, mime, man, digstr, err
}
