package cmd

import (
	"context"
	"kami/internal/controller/camel_oil"
	"kami/internal/controller/card_info_apple"
	"kami/internal/controller/card_info_c_trip"
	"kami/internal/controller/card_info_jd"
	"kami/internal/controller/card_info_original_jd"
	"kami/internal/controller/card_info_t_mall_game"
	"kami/internal/controller/card_info_walmart"
	"kami/internal/controller/jd_cookie"
	monitorCtrl "kami/internal/controller/monitor"
	"kami/internal/controller/order"
	"kami/internal/controller/restriction"
	"kami/internal/controller/sysUser"
	"kami/internal/controller/sys_payment"
	"kami/internal/controller/sys_user_login"
	"kami/internal/controller/sys_user_login_log"
	"kami/internal/controller/user_center"
	"kami/utility/cron"
	"kami/utility/monitor"
	"kami/utility/pool"
	"os"
	"os/signal"
	"syscall"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gcron"
	"github.com/gogf/gf/v2/os/glog"

	"kami/internal/controller/channel"
	"kami/internal/controller/merchant"
	"kami/internal/controller/road"
	"kami/internal/controller/road_pool"
	"kami/internal/controller/validation"
	"kami/internal/middleware"
)

var Main = gcmd.Command{
	Name:  "main",
	Usage: "main",
	Brief: "start http server",
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
		s := g.Server()
		s.Group("/api", func(group *ghttp.RouterGroup) {
			group.Middleware(ghttp.MiddlewareCORS, ghttp.MiddlewareHandlerResponse,
				middleware.ErrorHandler, ghttp.MiddlewareGzip, ghttp.MiddlewareNeverDoneCtx)
			group.Bind(validation.NewV1()) // 验证码接口
			group.Bind(sys_user_login.NewV1())
			group.Bind(monitorCtrl.NewV1())
			group.Middleware(middleware.LoginOrIframeAuth)
			group.Bind(channel.NewV2())
			group.Bind(merchant.NewV1())
			group.Bind(road.NewV1())
			group.Bind(road_pool.NewV1())
			group.Bind(order.NewV1())
			group.Bind(sysUser.NewV1())
			// group.Bind(fund.NewV1())
			// group.Bind(channel.NewV1())
			// group.Bind(authority.NewV1())
			group.Bind(sys_payment.NewV1())
			group.Bind(user_center.NewV1())
			group.Bind(card_info_apple.NewV1())
			group.Bind(card_info_t_mall_game.NewV1())
			group.Bind(card_info_jd.NewV1())
			group.Bind(restriction.NewV1())
			group.Bind(card_info_walmart.NewV1())
			group.Bind(card_info_original_jd.NewV1())
			group.Bind(card_info_c_trip.NewV1())
			group.Bind(jd_cookie.NewV1())
			group.Bind(sys_user_login_log.New())
			group.Bind(camel_oil.NewV1()) // 骆驼加油订单处理模块
		})
		monitor.Register(ctx) // 注册监控任务
		cron.Register(ctx)    // 注册轮询任务
		quit := make(chan os.Signal, 1)
		signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
		s.Run()
		GracefulDown(ctx, quit)
		return nil
	},
}

func GracefulDown(ctx context.Context, quit chan os.Signal) {
	sig := <-quit
	glog.Printf(ctx, "Signal received: %s, stopping cron", sig)
	glog.Print(ctx, "Waiting for all cron jobs to complete...")
	//关闭轮询任务
	gcron.StopGracefully()
	glog.Print(ctx, "All cron jobs completed")
	//	关闭线程池
	pool.GracefulDown()
}
