package server

import (
	"context"
	"fmt"

	"github.com/pkg/errors"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"

	"route256/loms/internal/domain/entity"
	"route256/loms/internal/domain/model"

	pb "route256/pkg/api/loms/v1"
)

var _ pb.LOMSServer = (*Server)(nil)

type LomsService interface {
	OrderCreate(ctx context.Context, orderReq *pb.OrderCreateRequest) (entity.ID, error)
	OrderInfo(ctx context.Context, orderID entity.ID) (*entity.Order, error)
	OrderPay(ctx context.Context, orderID entity.ID) error
	OrderCancel(ctx context.Context, orderID entity.ID) error
	StocksInfo(ctx context.Context, sku entity.Sku) (uint32, error)
}

type Server struct {
	pb.UnimplementedLOMSServer

	service LomsService
}

func NewServer(lomsService LomsService) *Server {
	return &Server{
		service: lomsService,
	}
}

func mapOrderStatus(pbStatus pb.OrderStatus) (string, error) {
	switch pbStatus {
	case pb.OrderStatus_ORDER_STATUS_AWAITING_PAYMENT:
		return "awaiting payment", nil
	case pb.OrderStatus_ORDER_STATUS_CANCELLED:
		return "cancelled", nil
	case pb.OrderStatus_ORDER_STATUS_FAILED:
		return "failed", nil
	case pb.OrderStatus_ORDER_STATUS_NEW:
		return "new", nil
	case pb.OrderStatus_ORDER_STATUS_PAYED:
		return "payed", nil
	default:
		return "", fmt.Errorf("unexpected OrderStatus: %v", pbStatus)
	}
}

func (s *Server) OrderCreate(ctx context.Context, req *pb.OrderCreateRequest) (*pb.OrderCreateResponse, error) {
	id, err := s.service.OrderCreate(ctx, req)
	switch {
	case errors.Is(err, model.ErrInvalidInput):
		return nil, status.Error(codes.InvalidArgument, err.Error())
	case errors.Is(err, model.ErrNotEnoughStocks):
		return nil, status.Error(codes.FailedPrecondition, err.Error())
	case err != nil:
		return nil, status.Error(codes.Internal, err.Error())
	}

	return &pb.OrderCreateResponse{OrderId: int64(id)}, nil
}

func (s *Server) OrderInfo(ctx context.Context, req *pb.OrderInfoRequest) (*pb.OrderInfoResponse, error) {
	ord, err := s.service.OrderInfo(ctx, entity.ID(req.OrderId))
	switch {
	case errors.Is(err, model.ErrInvalidInput):
		return nil, status.Error(codes.InvalidArgument, err.Error())
	case errors.Is(err, model.ErrOrderNotFound):
		return nil, status.Error(codes.NotFound, err.Error())
	case err != nil:
		return nil, status.Error(codes.Internal, err.Error())
	}

	items := make([]*pb.OrderItem, len(ord.Items))
	for i, item := range ord.Items {
		items[i] = &pb.OrderItem{
			Sku:   int64(item.ID),
			Count: item.Count,
		}
	}

	orderStatus, err := mapOrderStatus(pb.OrderStatus(pb.OrderStatus_value[ord.Status]))
	if err != nil {
		return nil, err
	}

	resp := &pb.OrderInfoResponse{
		Status: orderStatus,
		UserId: int64(ord.UserID),
		Items:  items,
	}

	return resp, nil
}

func (s *Server) OrderPay(ctx context.Context, req *pb.OrderPayRequest) (*emptypb.Empty, error) {
	err := s.service.OrderPay(ctx, entity.ID(req.OrderId))
	switch {
	case errors.Is(err, model.ErrInvalidInput):
		return nil, status.Error(codes.InvalidArgument, err.Error())
	case errors.Is(err, model.ErrOrderNotFound):
		return nil, status.Error(codes.NotFound, err.Error())
	case errors.Is(err, model.ErrOrderInvalidStatus):
		return nil, status.Error(codes.FailedPrecondition, err.Error())
	case err != nil:
		return nil, status.Error(codes.Internal, err.Error())
	}

	return &emptypb.Empty{}, nil
}

func (s *Server) OrderCancel(ctx context.Context, req *pb.OrderCancelRequest) (*emptypb.Empty, error) {
	err := s.service.OrderCancel(ctx, entity.ID(req.OrderId))
	switch {
	case errors.Is(err, model.ErrInvalidInput):
		return nil, status.Error(codes.InvalidArgument, err.Error())
	case errors.Is(err, model.ErrOrderNotFound):
		return nil, status.Error(codes.NotFound, err.Error())
	case errors.Is(err, model.ErrOrderInvalidStatus):
		return nil, status.Error(codes.FailedPrecondition, err.Error())
	case err != nil:
		return nil, status.Error(codes.Internal, err.Error())
	}

	return &emptypb.Empty{}, nil
}

func (s *Server) StocksInfo(ctx context.Context, req *pb.StocksInfoRequest) (*pb.StocksInfoResponse, error) {
	count, err := s.service.StocksInfo(ctx, entity.Sku(req.Sku))
	switch {
	case errors.Is(err, model.ErrInvalidInput):
		return nil, status.Error(codes.InvalidArgument, err.Error())
	case errors.Is(err, model.ErrOrderNotFound), errors.Is(err, model.ErrUnknownStock):
		return nil, status.Error(codes.NotFound, err.Error())
	case err != nil:
		return nil, status.Error(codes.Internal, err.Error())
	}

	return &pb.StocksInfoResponse{Count: count}, nil
}
