package router

import (
	"applet/app/cfg"
	"applet/app/hdl"
	"applet/app/mw"
	_ "applet/docs"
	"github.com/gin-gonic/gin"
)

// 初始化路由
// 1
func Init() *gin.Engine {
	// debug, release, test 项目阶段
	mode := "release"
	if cfg.Debug {
		mode = "debug"
	}
	gin.SetMode(mode)
	//创建一个新的启动器
	r := gin.New()
	r.Use(mw.ChangeHeader)

	// 是否打印访问日志, 在非正式环境都打印
	if mode != "release" {
		r.Use(gin.Logger())
	}
	r.Use(gin.Recovery())

	r.GET("/favicon.ico", func(c *gin.Context) {
		c.Status(204)
	})
	r.NoRoute(func(c *gin.Context) {
		c.JSON(404, gin.H{"code": 404, "msg": "page not found", "data": []struct{}{}})
	})
	r.NoMethod(func(c *gin.Context) {
		c.JSON(405, gin.H{"code": 405, "msg": "method not allowed", "data": []struct{}{}})
	})
	r.Use(mw.Cors)
	routeCommunityTeam(r.Group("/api/v1/communityTeam"))
	return r
}
func routeCommunityTeam(r *gin.RouterGroup) {
	r.Use(mw.DB)        // 下面接口再根据mid 获取数据库名
	r.Use(mw.CheckBody) //body参数转换
	r.Use(mw.CheckSign) //签名校验
	r.Use(mw.Checker)
	r.POST("/city", hdl.City)
	r.GET("/bank/store/cate", hdl.BankStoreCate)
	r.POST("/bank/store/list", hdl.BankStore)
	r.POST("/store", hdl.Store)
	r.GET("/goods/cate", hdl.Cate)
	r.POST("/goods", hdl.Goods)
	r.POST("/goods/sku", hdl.GoodsSku)
	r.GET("/user", hdl.User)
	// 用户授权后调用的接口
	r.Use(mw.AuthJWT)
	r.POST("/store/addLike", hdl.StoreAddLike)
	r.POST("/store/cancelLike", hdl.StoreCancelLike)
	r.POST("/goods/coupon", hdl.GoodsCoupon)
	r.POST("/order/total", hdl.OrderTotal)
	r.POST("/order/create", hdl.OrderCreate)
	r.POST("/order/cancel", hdl.OrderCancel)
	r.POST("/order/confirm", hdl.OrderConfirm)
	r.POST("/order/coupon", hdl.OrderCoupon)
	r.POST("/order/list", hdl.OrderList)
	r.POST("/order/detail", hdl.OrderDetail)
	r.GET("/order/cate", hdl.OrderCate)
	r.POST("/pay/:payMethod/:orderType", hdl.Pay)

	r.POST("/store/order/list", hdl.StoreOrderList)
	r.POST("/store/order/detail", hdl.StoreOrderDetail)
	r.POST("/store/order/confirm", hdl.StoreOrderConfirm)
	r.GET("/store/order/cate", hdl.StoreOrderCate)
}
