package customer

import (
	"context"
	"log"

	"go.uber.org/zap"
	"google.golang.org/protobuf/types/known/emptypb"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/proto/customer"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
)

type Deps struct {
	Logger         *zap.Logger
	HistoryService *history.Service
}

type Controller struct {
	logger         *zap.Logger
	historyService *history.Service
}

func New(deps Deps) *Controller {
	if deps.Logger == nil {
		log.Panicln("logger is nil on <NewPaymentController>")
	}

	if deps.HistoryService == nil {
		log.Panicln("HistoryService is nil on <NewPaymentController>")
	}

	return &Controller{
		logger:         deps.Logger,
		historyService: deps.HistoryService,
	}
}

func (receiver *Controller) InsertHistory(ctx context.Context, in *customer.History) (*emptypb.Empty, error) {
	if _, err := receiver.historyService.CreateHistory(ctx, &models.History{
		UserID:     in.UserID,
		Comment:    in.Comment,
		Key:        in.Key,
		RawDetails: in.RawDetails,
	}); err != nil {
		receiver.logger.Error("failed to insert history on <InsertHistory> of <HistoryController>", zap.Error(err))
		return nil, errors.GRPC("failed to insert history", err)
	}

	return &emptypb.Empty{}, nil
}
