package main

import (
	"context"

	"google.golang.org/grpc"
	"google.golang.org/protobuf/encoding/prototext"
	"google.golang.org/protobuf/types/known/emptypb"

	blog "github.com/letsencrypt/boulder/log"
	rapb "github.com/letsencrypt/boulder/ra/proto"
	sapb "github.com/letsencrypt/boulder/sa/proto"
)

type dryRunRAC struct {
	log blog.Logger
}

var _ adminRAClient = (*dryRunRAC)(nil)

func (d dryRunRAC) AdministrativelyRevokeCertificate(_ context.Context, req *rapb.AdministrativelyRevokeCertificateRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) {
	b, err := prototext.Marshal(req)
	if err != nil {
		return nil, err
	}
	d.log.Infof("dry-run: %#v", string(b))
	return &emptypb.Empty{}, nil
}

type dryRunSAC struct {
	log blog.Logger
}

var _ adminSAClient = (*dryRunSAC)(nil)

func (d dryRunSAC) AddBlockedKey(_ context.Context, req *sapb.AddBlockedKeyRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) {
	d.log.Infof("dry-run: Block SPKI hash %x by %s %s", req.KeyHash, req.Comment, req.Source)
	return &emptypb.Empty{}, nil
}

func (d dryRunSAC) AddRateLimitOverride(_ context.Context, req *sapb.AddRateLimitOverrideRequest, _ ...grpc.CallOption) (*sapb.AddRateLimitOverrideResponse, error) {
	d.log.Infof("dry-run: Add override for %q (%s)", req.Override.BucketKey, req.Override.Comment)
	return &sapb.AddRateLimitOverrideResponse{Inserted: true, Enabled: true}, nil
}

func (d dryRunSAC) DisableRateLimitOverride(_ context.Context, req *sapb.DisableRateLimitOverrideRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) {
	d.log.Infof("dry-run: Disable override for %q", req.BucketKey)
	return &emptypb.Empty{}, nil
}

func (d dryRunSAC) EnableRateLimitOverride(_ context.Context, req *sapb.EnableRateLimitOverrideRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) {
	d.log.Infof("dry-run: Enable override for %q", req.BucketKey)
	return &emptypb.Empty{}, nil
}

func (d dryRunSAC) PauseIdentifiers(_ context.Context, req *sapb.PauseRequest, _ ...grpc.CallOption) (*sapb.PauseIdentifiersResponse, error) {
	d.log.Infof("dry-run: Pause identifiers %#v for account %d", req.Identifiers, req.RegistrationID)
	return &sapb.PauseIdentifiersResponse{Paused: int64(len(req.Identifiers))}, nil
}

func (d dryRunSAC) UnpauseAccount(_ context.Context, req *sapb.RegistrationID, _ ...grpc.CallOption) (*sapb.Count, error) {
	d.log.Infof("dry-run: Unpause account %d", req.Id)
	return &sapb.Count{Count: 1}, nil
}
