package methods

import (
	"context"

	"github.com/tech/sendico/pkg/merrors"
	methodsv1 "github.com/tech/sendico/pkg/proto/payments/methods/v1"
)

func (s *Service) SetPaymentMethodArchived(ctx context.Context, req *methodsv1.SetPaymentMethodArchivedRequest) (*methodsv1.SetPaymentMethodArchivedResponse, error) {
	if req == nil {
		return autoError[methodsv1.SetPaymentMethodArchivedResponse](ctx, s.logger, merrors.InvalidArgument("request is required"))
	}
	if s.pmstore == nil {
		return autoError[methodsv1.SetPaymentMethodArchivedResponse](ctx, s.logger, errStoreUnavailable)
	}

	accountRef, err := parseObjectID(req.GetAccountRef(), "account_ref")
	if err != nil {
		return autoError[methodsv1.SetPaymentMethodArchivedResponse](ctx, s.logger, err)
	}
	organizationRef, err := parseObjectID(req.GetOrganizationRef(), "organization_ref")
	if err != nil {
		return autoError[methodsv1.SetPaymentMethodArchivedResponse](ctx, s.logger, err)
	}
	methodRef, err := parseObjectID(req.GetPaymentMethodRef(), "payment_method_ref")
	if err != nil {
		return autoError[methodsv1.SetPaymentMethodArchivedResponse](ctx, s.logger, err)
	}

	if err := s.pmstore.SetArchived(ctx, accountRef, organizationRef, methodRef, req.GetArchived(), req.GetCascade()); err != nil {
		return autoError[methodsv1.SetPaymentMethodArchivedResponse](ctx, s.logger, err)
	}

	return &methodsv1.SetPaymentMethodArchivedResponse{}, nil
}
