package errdefs

import (
	"errors"

	"github.com/containerd/typeurl/v2"
	"github.com/moby/buildkit/solver/pb"
	"github.com/moby/buildkit/util/grpcerrors"
	"google.golang.org/protobuf/encoding/protojson"
)

func init() {
	typeurl.Register((*Solve)(nil), "github.com/moby/buildkit", "errdefs.Solve+json")
}

//nolint:staticcheck
type IsSolve_Subject isSolve_Subject

// SolveError will be returned when an error is encountered during a solve that
// has an exec op.
type SolveError struct {
	*Solve
	Err error
}

func (e *SolveError) Error() string {
	return e.Err.Error()
}

func (e *SolveError) Unwrap() error {
	return e.Err
}

func (e *SolveError) ToProto() grpcerrors.TypedErrorProto {
	return e.Solve
}

func WithSolveError(err error, subject IsSolve_Subject, inputIDs, mountIDs []string) error {
	if err == nil {
		return nil
	}
	var (
		oe          *OpError
		op          *pb.Op
		description map[string]string
	)
	if errors.As(err, &oe) {
		op = oe.Op
		description = oe.Description
	}
	return &SolveError{
		Err: err,
		Solve: &Solve{
			InputIDs:    inputIDs,
			MountIDs:    mountIDs,
			Op:          op,
			Subject:     subject,
			Description: description,
		},
	}
}

func (v *Solve) WrapError(err error) error {
	return &SolveError{Err: err, Solve: v}
}

func (v *Solve) MarshalJSON() ([]byte, error) {
	return protojson.Marshal(v)
}

func (v *Solve) UnmarshalJSON(b []byte) error {
	return protojson.Unmarshal(b, v)
}
