package generate

import (
	"os"

	"github.com/sirupsen/logrus"
	"gorm.io/driver/mysql"
	"gorm.io/gen"
	"gorm.io/gorm"
)

func GenModel() {
	g := gen.NewGenerator(gen.Config{
		OutPath:           "./internal/gorm/query",
		Mode:              gen.WithoutContext,
		FieldWithIndexTag: true,
		FieldWithTypeTag:  true,
	})

	GenDefaultModel(g)

	// GenGraveModel(g)
	// GenLoginModel(g)

	// execute the action of code generation
	g.Execute()
}

func GenDefaultModel(g *gen.Generator) {
	logrus.Info("is executing GenDefaultModel...")

	db, _ := gorm.Open(mysql.Open(os.Getenv("MYSQL_DSN")))
	g.UseDB(db)

	g.ApplyBasic(
		g.GenerateModel("abnormal_payment_logs"),
		g.GenerateModel("admin_email"),
		g.GenerateModel("admin_logs"),
		g.GenerateModel("admin_notice"),
		g.GenerateModel("admin_role"),
		g.GenerateModel("admin_users"),
		g.GenerateModel("adv_origin_logs", gen.FieldType("date", "string")),
		g.GenerateModel("advertisement_logs"),
		g.GenerateModel("advertisement_space"),

		g.GenerateModel("boss_logs", gen.FieldType("date", "string")),

		g.GenerateModel("cache"),
		g.GenerateModel("changed_logs"),
		g.GenerateModel("changed_player"),
		g.GenerateModel("changed_statistics", gen.FieldType("date", "string")),
		g.GenerateModel("changed_sync"),
		g.GenerateModel("channel"),
		g.GenerateModel("chapter"),
		g.GenerateModel("chapter_logs"),
		g.GenerateModel("chapter_logs_user_details"),
		g.GenerateModel("chapter_sync"),
		g.GenerateModel("config"),
		g.GenerateModel("customer_service_chat_log"),

		g.GenerateModel("disconnect_logs", gen.FieldType("date", "string")),
		g.GenerateModel("duel_logs", gen.FieldType("date", "string")),

		g.GenerateModel("event_config"),
		g.GenerateModel("expedition_logs", gen.FieldType("date", "string")),

		g.GenerateModel("first_adv_logs", gen.FieldType("date", "string")),

		g.GenerateModel("game_data_alarm"),
		g.GenerateModel("game_character_data_anomaly"),
		g.GenerateModel("game_alarm_combat_logs"),
		g.GenerateModel("gem_logs", gen.FieldType("date", "string")),
		g.GenerateModel("gem_player"),
		g.GenerateModel("gem_stat", gen.FieldType("date", "string")),
		g.GenerateModel("goods_logs"),
		g.GenerateModel("grandmaster_logs", gen.FieldType("date", "string")),
		g.GenerateModel("grandmaster_match"),
		g.GenerateModel("gudong_logs", gen.FieldType("date", "string")),

		g.GenerateModel("idiom_logs", gen.FieldType("date", "string")),
		g.GenerateModel("lady38rank2"),
		g.GenerateModel("limitgift_logs", gen.FieldType("date", "string")),
		g.GenerateModel("login_logs"),

		g.GenerateModel("menu"),
		g.GenerateModel("new_conv_logs", gen.FieldType("date", "string")),

		g.GenerateModel("online_duration_logs"),
		g.GenerateModel("online_nums_logs"),
		g.GenerateModel("orders", gen.FieldType("date", "string")),
		g.GenerateModel("orders_reg_stat", gen.FieldType("date", "string")),
		g.GenerateModel("orders_settle"),
		g.GenerateModel("orders_sync"),

		g.GenerateModel("player_ban_logs"),
		g.GenerateModel("player_battle_dps"),
		g.GenerateModel("player_cache"),
		g.GenerateModel("player_channel"),
		g.GenerateModel("player_dps"),
		g.GenerateModel("player_logins"),

		g.GenerateModel("redeem_code", gen.FieldType("date", "string")),
		g.GenerateModel("redeem_peripherals_received"),
		g.GenerateModel("redeem_received"),
		g.GenerateModel("report_day_basic", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_event_basic", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_adv_basic", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_goods_basic", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_gudong", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_chapter_pass", gen.FieldType("date", "string")),
		//g.GenerateModel("test_logs", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_expedition", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_duel", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_boss", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_idiom", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_disconnect", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_gem", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_limitgift", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_have_roles", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_adv_summ", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_treasure", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_new_conv", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_first_adv", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_grandmaster", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_treasure_chest", gen.FieldType("date", "string")),
		g.GenerateModel("report_day_seven", gen.FieldType("date", "string")),
		g.GenerateModel("report_events", gen.FieldType("date", "string")),
		g.GenerateModel("report_level_output"),
		g.GenerateModel("retrofit"),
		g.GenerateModel("retrofit_group"),
		g.GenerateModel("role"),
		g.GenerateModel("roles_logs", gen.FieldType("date", "string")),

		g.GenerateModel("server_deploy_stat"),
		g.GenerateModel("server_deploy"),
		g.GenerateModel("server_deploy_log"),
		g.GenerateModel("server_option"),
		g.GenerateModel("seven_logs", gen.FieldType("date", "string")),

		g.GenerateModel("tools_restart_log"),
		g.GenerateModel("treasure_chest_log", gen.FieldType("date", "string")),
		g.GenerateModel("treasure_log", gen.FieldType("date", "string")),

		g.GenerateModel("user_chapter_logs"),
	)
}

func GenGraveModel(g *gen.Generator) {
	logrus.Info("is executing GenGraveModel...")
	db, _ := gorm.Open(mysql.Open(os.Getenv("MYSQL_GRAVE_DSN")))
	g.UseDB(db)

	g.ApplyBasic(
		g.GenerateModel("bosswar"),     // 暗影突袭
		g.GenerateModel("buy_stamina"), // 购买体力
		g.GenerateModel("client_data"), // 客户端数据
		g.GenerateModel("climbfloor"),  // 悬魂梯房间
		g.GenerateModel("curiomake"),   // 古玩合成

		g.GenerateModel("globalletter"),    // 全局邮件
		g.GenerateModel("grandmaster"),     // 玩家活动进度
		g.GenerateModel("game_data_alarm"), //玩家游戏数据异常

		g.GenerateModel("idiommatch"), // 金榜题名

		g.GenerateModel("player_attr"),       // 玩家基本信息
		g.GenerateModel("player_day_tasks"),  // 玩家每日任务
		g.GenerateModel("player_divination"), // 玩家占卜，幸运密宝
		g.GenerateModel("player_letter"),     // 玩家邮件
		g.GenerateModel("player_mailbox"),    // 玩家邮件，最后查看ID
		g.GenerateModel("player_material"),   // 玩家材料
		g.GenerateModel("player_patrol"),     // 玩家收益记录
		//g.GenerateModel("player_score"),      // 用户得分
		g.GenerateModel("player_task"),  // 用户任务
		g.GenerateModel("processaward"), // 服务器奖励领取

		g.GenerateModel("signin"), // 登录记录

		//g.GenerateModel("soulfloor"),         // 悬魂梯每层
		g.GenerateModel("wxpay_order"),   // 微信订单
		g.GenerateModel("wxpay_player"),  // 玩家支付订单
		g.GenerateModel("zone_activity"), // 玩家活动进度
	)

	g.ApplyBasic(g.GenerateModelAs("virtualpayorder", "PayOrderAndriod")) // 微信订单
	g.ApplyBasic(g.GenerateModelAs("wxpay_iosorder", "PayOrderIos"))      // 微信ios订单

}

func GenLoginModel(g *gen.Generator) {
	logrus.Info("is executing GenLoginModel...")
	db, _ := gorm.Open(mysql.Open(os.Getenv("MYSQL_LOGIN_DSN")))
	g.UseDB(db)

	g.ApplyBasic(
		g.GenerateModel("user_account"), // 用户账号
	)

}
