package routes

import (
	"git.trj.tw/golang/go-gallery/models"
	"git.trj.tw/golang/go-gallery/modules/context"
	"git.trj.tw/golang/go-gallery/modules/middleware"
	"git.trj.tw/golang/go-gallery/routers/account"
	"git.trj.tw/golang/go-gallery/routers/album"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// NewServ - get new service
func NewServ() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	r.Use(cors.Default())
	return r
}

// SetDefaultRoutes -
func SetDefaultRoutes(r *gin.Engine) {
	r.GET("/", func(c *gin.Context) {
		accs, err := models.GetAllAccount()
		if err != nil {
			c.JSON(500, gin.H{
				"message": "db error",
			})
			return
		}
		c.JSON(200, accs)
	})

	api := r.Group("/api")
	{
		api.GET("/", func(c *gin.Context) {
			c.JSON(200, gin.H{
				"status":  200,
				"message": "Success",
			})
		})
	}
	accountAPI := api.Group("/account")
	{
		accountAPI.POST("/login", context.PatchContext(account.UserLogin))
		accountAPI.POST("/logout", context.PatchContext(account.UserLogout))
		accountAPI.POST("/signup", context.PatchContext(account.UserSignup))
	}
	api.GET("/albums", context.PatchContext(middleware.VerifyToken), context.PatchContext(album.GetAllAlbums))
	albumAPI := api.Group("/album", context.PatchContext(middleware.VerifyToken))
	{
		albumAPI.POST("/", context.PatchContext(album.CreateAlbum))
		albumAPI.GET("/:id", context.PatchContext(album.GetAlbum))
		albumAPI.PUT("/:id", context.PatchContext(album.UpdateAlbum))
	}
}
