package http

import (
	"context"
	"git.gocasts.ir/ebhomengo/niki/pkg/httpserver"
	"git.gocasts.ir/ebhomengo/niki/shoppingbasketapp/delivery/http/cart"
)

type Server struct {
	handler    cart.Handler
	HTTPServer *httpserver.Server
}

func NewServer(handler cart.Handler, hS *httpserver.Server) Server {
	return Server{handler: handler, HTTPServer: hS}
}

func (s Server) Serve() error {
	s.registerRoutes()
	if err := s.HTTPServer.Start(); err != nil {
		return err
	}

	return nil
}

func (s Server) Stop(ctx context.Context) error {
	return s.HTTPServer.Stop(ctx)
}

func (s Server) registerRoutes() {
	router := s.HTTPServer.GetRouter()

	router.GET("shoppingbasket/health-check", s.healthCheck)

	r := router.Group("shoppingbasket/cart") // Authentication is required

	r.GET("/", s.handler.GetCart)
	r.DELETE("/", s.handler.RemoveCart)

	r.POST("/items", s.handler.AddToBasket)
	r.DELETE("/items/:productID", s.handler.RemoveItem)
	r.PUT("/items/:productID/:quantity", s.handler.UpdateQuantity)
}
