package http

import (
	"fmt"
	"git.gocasts.ir/ebhomengo/niki/pkg/httpserver"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

type RoleServer struct {
	cfg     httpserver.Config
	handler RoleHandler
}

func NewRoleServer(cfg httpserver.Config, handler RoleHandler) RoleServer {
	return RoleServer{cfg: cfg, handler: handler}
}

func (s RoleServer) Start() {
	e := echo.New()

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	group := e.Group("/role")
	group.POST("/create", s.handler.Store)
	group.PUT("/update/:id", s.handler.Update)

	address := fmt.Sprintf("%s:%s", s.cfg.Host, s.cfg.Port)
	err := e.Start(address)
	if err != nil {
		e.Logger.Fatal(err)
	}
}
