package server

import (
	"context"

	"gitea.benny.dog/torjus/ezshare/ezshare"
	"gitea.benny.dog/torjus/ezshare/pb"
	"gitea.benny.dog/torjus/ezshare/server/interceptors"
	"gitea.benny.dog/torjus/ezshare/store"
	"go.uber.org/zap"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type BinaryServiceServer struct {
	Logger *zap.SugaredLogger
	store  store.BinaryStore
	pb.UnimplementedBinaryServiceServer
}

func NewBinaryServiceServer(store store.BinaryStore) *BinaryServiceServer {
	return &BinaryServiceServer{Logger: zap.NewNop().Sugar(), store: store}
}

func (s *BinaryServiceServer) UploadBinaries(ctx context.Context, req *pb.UploadBinariesRequest) (*pb.Empty, error) {
	if !interceptors.RoleAtLeast(ctx, pb.User_ADMIN) {
		return nil, status.Error(codes.PermissionDenied, "you are not allowed to upload release.")
	}

	for _, release := range req.Binaries {
		if err := s.store.StoreBinary(release); err != nil {
			s.Logger.Errorw("Error storing binary.", "version", release.Version, "os", release.Os, "arch", release.Arch)
			return nil, status.Errorf(codes.Internal, "error storing release: %s", err)
		}
		s.Logger.Infow("Stored new binary.", "version", release.Version, "os", release.Os, "arch", release.Arch)
	}
	return &pb.Empty{}, nil
}

func (s *BinaryServiceServer) GetBinary(ctx context.Context, req *pb.GetBinaryRequest) (*pb.Binary, error) {
	version := "latest"
	if req.Version != "" {
		version = req.Version
	}

	binary, err := s.store.GetBinary(version, req.Os, req.Arch)
	if err != nil {
		if err == store.ErrNoSuchItem {
			return nil, status.Error(codes.NotFound, "release not found")
		}
		s.Logger.Errorw("Error getting binary.", "error", err, "version", req.Version, "os", req.Os, "arch", req.Arch)
		return nil, status.Errorf(codes.Internal, "release not found: %s", err)
	}

	s.Logger.Infow("Sent binary.", "version", binary.Version, "os", binary.Os, "arch", binary.Arch)
	return binary, nil
}

func (s *BinaryServiceServer) GetLatestVersion(_ context.Context, _ *pb.Empty) (*pb.LatestVersionResponse, error) {
	return &pb.LatestVersionResponse{Version: ezshare.Version}, nil
}