package client

import (
	"context"
	"errors"
	"fmt"

	v2container "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container"
	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
	rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
	v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
	apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
	cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
)

// PrmContainerGet groups parameters of ContainerGet operation.
type PrmContainerGet struct {
	// FrostFS request X-Headers.
	XHeaders []string

	ContainerID *cid.ID
}

// SetContainer sets identifier of the container to be read.
// Required parameter.
//
// Deprecated: Use PrmContainerGet.ContainerID instead.
func (prm *PrmContainerGet) SetContainer(cid cid.ID) {
	prm.ContainerID = &cid
}

func (prm *PrmContainerGet) buildRequest(c *Client) (*v2container.GetRequest, error) {
	if prm.ContainerID == nil {
		return nil, errorMissingContainer
	}

	if len(prm.XHeaders)%2 != 0 {
		return nil, errorInvalidXHeaders
	}

	var cidV2 refs.ContainerID
	prm.ContainerID.WriteToV2(&cidV2)

	reqBody := new(v2container.GetRequestBody)
	reqBody.SetContainerID(&cidV2)

	var req v2container.GetRequest
	req.SetBody(reqBody)
	c.prepareRequest(&req, new(v2session.RequestMetaHeader))
	return &req, nil
}

// ResContainerGet groups resulting values of ContainerGet operation.
type ResContainerGet struct {
	statusRes

	cnr container.Container
}

// Container returns structured information about the requested container.
//
// Client doesn't retain value so modification is safe.
func (x ResContainerGet) Container() container.Container {
	return x.cnr
}

// ContainerGet reads FrostFS container by ID.
//
// Exactly one return value is non-nil. By default, server status is returned in res structure.
// Any client's internal or transport errors are returned as `error`.
// If PrmInit.ResolveFrostFSFailures has been called, unsuccessful
// FrostFS status codes are returned as `error`, otherwise, are included
// in the returned result structure.
//
// Returns an error if parameters are set incorrectly (see PrmContainerGet docs).
// Context is required and must not be nil. It is used for network communication.
//
// Return statuses:
//   - global (see Client docs);
//   - *apistatus.ContainerNotFound.
func (c *Client) ContainerGet(ctx context.Context, prm PrmContainerGet) (*ResContainerGet, error) {
	req, err := prm.buildRequest(c)
	if err != nil {
		return nil, err
	}

	if err := signature.SignServiceMessage(&c.prm.key, req); err != nil {
		return nil, fmt.Errorf("sign request: %w", err)
	}

	resp, err := rpcapi.GetContainer(&c.c, req, client.WithContext(ctx))
	if err != nil {
		return nil, err
	}

	var res ResContainerGet
	res.st, err = c.processResponse(resp)
	if err != nil || !apistatus.IsSuccessful(res.st) {
		return &res, err
	}

	cnrV2 := resp.GetBody().GetContainer()
	if cnrV2 == nil {
		return &res, errors.New("missing container in response")
	}
	if err := res.cnr.ReadFromV2(*cnrV2); err != nil {
		return &res, fmt.Errorf("invalid container in response: %w", err)
	}
	return &res, nil
}