package initialize

import (
	"time"

	"wails-app/internal/dao"
	"wails-app/internal/global"

	"github.com/gofrs/uuid/v5"
)

// SeedSystemData seeds system base data if not already present.
// Idempotent: checks if admin user exists, skips if so.
func SeedSystemData() {
	db := global.GVA_DB
	if db == nil {
		return
	}

	var count int64
	db.Model(&dao.SysUser{}).Where("username = ?", "admin").Count(&count)
	if count > 0 {
		return
	}

	now := time.Date(2024, 5, 5, 9, 33, 22, 0, time.UTC)

	// --- sys_users ---
	users := []dao.SysUser{
		{
			UUID:        uuid.Must(uuid.FromString("07c9149c-6a9a-4126-8d8a-dd11c7b914a1")),
			Username:    "admin",
			Password:    "$2a$10$WTDQCqEQzEi/FU2UKejgGuWDK3PtHT0yb6xwY.yhx4ugjAmS6iTNa",
			NickName:    "龙弛管理员",
			SideMode:    "dark",
			HeaderImg:   "uploads/file/70b1e321d2ec7357cb1b948f5bde35a6_20240606094912.jpg",
			BaseColor:   "#fff",
			ActiveColor: "#1890ff",
			AuthorityId: 888,
			Phone:       "17611111111",
			Email:       "333333333@qq.com",
			Enable:      1,
		},
		{
			UUID:        uuid.Must(uuid.FromString("0a5db918-c3cc-4034-a32a-6c15f37e78cb")),
			Username:    "xuwenhao",
			Password:    "$2a$10$EL7QgO5cVNdB6N2rYqAQNOLj8w3mSh9noKpjmPfFO98ip0GyR9x3y",
			NickName:    "许文浩",
			SideMode:    "dark",
			HeaderImg:   "uploads/file/c67b3af7ed83579cc083089fc0bbefd0_20240505115511.jpg",
			BaseColor:   "#fff",
			ActiveColor: "#1890ff",
			AuthorityId: 888,
			Phone:       "17365743261",
			Email:       "sn3115614529@163.com",
			Enable:      1,
		},
		{
			UUID:        uuid.Must(uuid.FromString("9fce89d6-8552-4637-9cd1-9ffd6c8d4dbd")),
			Username:    "chengqian",
			Password:    "$2a$10$0qbv0aeavXAxdqUNIAhIC.TK/hfgC8Ky.sP0dK9fAw9lIsR9cIive",
			NickName:    "程潜",
			SideMode:    "dark",
			HeaderImg:   "https://qmplusimg.henrongyi.top/1576554439myAvatar.png",
			BaseColor:   "#fff",
			ActiveColor: "#1890ff",
			AuthorityId: 618,
			Phone:       "18574363259",
			Email:       "",
			Enable:      1,
		},
	}
	for i := range users {
		users[i].CreatedAt = now
		users[i].UpdatedAt = now
	}
	db.Create(&users)

	// --- sys_authorities ---
	authorities := []dao.SysAuthority{
		{AuthorityId: 618, AuthorityName: "普通用户", ParentId: ptr(uint(0)), DefaultRouter: "dashboard", CreatedAt: now, UpdatedAt: now},
		{AuthorityId: 888, AuthorityName: "管理员", ParentId: ptr(uint(0)), DefaultRouter: "dashboard", CreatedAt: now, UpdatedAt: now},
		{AuthorityId: 9527, AuthorityName: "开发", ParentId: ptr(uint(0)), DefaultRouter: "dashboard", CreatedAt: now, UpdatedAt: now},
	}
	db.Create(&authorities)

	// --- sys_user_authority ---
	db.Create(&dao.SysUserAuthority{SysUserId: 1, SysAuthorityAuthorityId: 888})
	db.Create(&dao.SysUserAuthority{SysUserId: 3, SysAuthorityAuthorityId: 888})
	db.Create(&dao.SysUserAuthority{SysUserId: 4, SysAuthorityAuthorityId: 618})

	// --- sys_base_menus (active only, skipping soft-deleted; ParentId refs = new seq IDs) ---
	// New ID seq: 1=dashboard, 2=superAdmin, 3=authority, 4=menu, 5=api, 6=user, 7=dictionary, 8=operation,
	// 9=person, 10=systemTools, 11=autoCode, 12=formCreate, 13=system, 14=autoCodeAdmin,
	// 15=autoCodeEdit, 16=autoPkg, 17=state, 18=plugin, 19=exportTemplate
	menus := []dao.SysBaseMenu{
		{MenuLevel: 0, ParentId: 0, Path: "dashboard", Name: "dashboard", Hidden: false, Component: "view/dashboard/index.vue", Sort: 1, Meta: dao.Meta{Title: "仪表盘", Icon: "odometer"}},
		{MenuLevel: 0, ParentId: 0, Path: "admin", Name: "superAdmin", Hidden: false, Component: "view/superAdmin/index.vue", Sort: 3, Meta: dao.Meta{Title: "超级管理员", Icon: "user"}},
		{MenuLevel: 0, ParentId: 2, Path: "authority", Name: "authority", Hidden: false, Component: "view/superAdmin/authority/authority.vue", Sort: 1, Meta: dao.Meta{Title: "角色管理", Icon: "avatar"}},
		{MenuLevel: 0, ParentId: 2, Path: "menu", Name: "menu", Hidden: false, Component: "view/superAdmin/menu/menu.vue", Sort: 2, Meta: dao.Meta{KeepAlive: true, Title: "菜单管理", Icon: "tickets"}},
		{MenuLevel: 0, ParentId: 2, Path: "api", Name: "api", Hidden: false, Component: "view/superAdmin/api/api.vue", Sort: 3, Meta: dao.Meta{KeepAlive: true, Title: "api管理", Icon: "platform"}},
		{MenuLevel: 0, ParentId: 2, Path: "user", Name: "user", Hidden: false, Component: "view/superAdmin/user/user.vue", Sort: 4, Meta: dao.Meta{Title: "用户管理", Icon: "coordinate"}},
		{MenuLevel: 0, ParentId: 2, Path: "dictionary", Name: "dictionary", Hidden: false, Component: "view/superAdmin/dictionary/sysDictionary.vue", Sort: 5, Meta: dao.Meta{Title: "字典管理", Icon: "notebook"}},
		{MenuLevel: 0, ParentId: 2, Path: "operation", Name: "operation", Hidden: false, Component: "view/superAdmin/operation/sysOperationRecord.vue", Sort: 6, Meta: dao.Meta{Title: "操作历史", Icon: "pie-chart"}},
		{MenuLevel: 0, ParentId: 0, Path: "person", Name: "person", Hidden: true, Component: "view/person/person.vue", Sort: 4, Meta: dao.Meta{Title: "个人信息", Icon: "message"}},
		{MenuLevel: 0, ParentId: 0, Path: "systemTools", Name: "systemTools", Hidden: false, Component: "view/systemTools/index.vue", Sort: 5, Meta: dao.Meta{Title: "系统工具", Icon: "tools"}},
		{MenuLevel: 0, ParentId: 10, Path: "autoCode", Name: "autoCode", Hidden: false, Component: "view/systemTools/autoCode/index.vue", Sort: 1, Meta: dao.Meta{KeepAlive: true, Title: "代码生成器", Icon: "cpu"}},
		{MenuLevel: 0, ParentId: 10, Path: "formCreate", Name: "formCreate", Hidden: false, Component: "view/systemTools/formCreate/index.vue", Sort: 2, Meta: dao.Meta{KeepAlive: true, Title: "表单生成器", Icon: "magic-stick"}},
		{MenuLevel: 0, ParentId: 10, Path: "system", Name: "system", Hidden: false, Component: "view/systemTools/system/system.vue", Sort: 3, Meta: dao.Meta{Title: "系统配置", Icon: "operation"}},
		{MenuLevel: 0, ParentId: 10, Path: "autoCodeAdmin", Name: "autoCodeAdmin", Hidden: false, Component: "view/systemTools/autoCodeAdmin/index.vue", Sort: 1, Meta: dao.Meta{Title: "自动化代码管理", Icon: "magic-stick"}},
		{MenuLevel: 0, ParentId: 10, Path: "autoCodeEdit/:id", Name: "autoCodeEdit", Hidden: true, Component: "view/systemTools/autoCode/index.vue", Sort: 0, Meta: dao.Meta{Title: "自动化代码-${id}", Icon: "magic-stick"}},
		{MenuLevel: 0, ParentId: 10, Path: "autoPkg", Name: "autoPkg", Hidden: false, Component: "view/systemTools/autoPkg/autoPkg.vue", Sort: 0, Meta: dao.Meta{Title: "自动化package", Icon: "folder"}},
		{MenuLevel: 0, ParentId: 0, Path: "state", Name: "state", Hidden: false, Component: "view/system/state.vue", Sort: 8, Meta: dao.Meta{Title: "服务器状态", Icon: "cloudy"}},
		{MenuLevel: 0, ParentId: 0, Path: "plugin", Name: "plugin", Hidden: false, Component: "view/routerHolder.vue", Sort: 6, Meta: dao.Meta{Title: "插件系统", Icon: "cherry"}},
		{MenuLevel: 0, ParentId: 10, Path: "exportTemplate", Name: "exportTemplate", Hidden: false, Component: "view/systemTools/exportTemplate/exportTemplate.vue", Sort: 10, Meta: dao.Meta{Title: "表格模板", Icon: "reading"}},
		// 停车管理 (ID=20)
		{MenuLevel: 0, ParentId: 0, Path: "parking", Name: "parking", Hidden: false, Component: "", Sort: 2, Meta: dao.Meta{Title: "停车管理", Icon: "van"}},
		{MenuLevel: 0, ParentId: 20, Path: "parkingInfo", Name: "parkingInfo", Hidden: false, Component: "view/parking/parkingInfo/index.vue", Sort: 0, Meta: dao.Meta{Title: "停车场信息", Icon: "van"}},
		{MenuLevel: 0, ParentId: 20, Path: "carInfo", Name: "carInfo", Hidden: false, Component: "view/parking/carInfo.vue", Sort: 1, Meta: dao.Meta{Title: "车辆信息", Icon: "notebook"}},
		{MenuLevel: 0, ParentId: 20, Path: "carOwner", Name: "carOwner", Hidden: false, Component: "view/parking/carOwner.vue", Sort: 2, Meta: dao.Meta{Title: "车主管理", Icon: "stamp"}},
		{MenuLevel: 0, ParentId: 20, Path: "roster", Name: "roster", Hidden: false, Component: "view/parking/roster.vue", Sort: 3, Meta: dao.Meta{Title: "黑白名单", Icon: "memo"}},
		{MenuLevel: 0, ParentId: 20, Path: "pricing", Name: "pricing", Hidden: false, Component: "view/parking/pricing.vue", Sort: 4, Meta: dao.Meta{Title: "收费配置", Icon: "office-building"}},
		{MenuLevel: 0, ParentId: 20, Path: "entryExit", Name: "entryExit", Hidden: false, Component: "view/parking/entryExit.vue", Sort: 5, Meta: dao.Meta{Title: "进出场操作", Icon: "switch"}},
		{MenuLevel: 0, ParentId: 20, Path: "monthlyCard", Name: "monthlyCard", Hidden: false, Component: "view/parking/monthlyCard.vue", Sort: 6, Meta: dao.Meta{Title: "包月车管理", Icon: "calendar"}},
		{MenuLevel: 0, ParentId: 20, Path: "shiftRecord", Name: "shiftRecord", Hidden: false, Component: "view/parking/shiftRecord.vue", Sort: 7, Meta: dao.Meta{Title: "交接班管理", Icon: "clock"}},
		// 报表查询 (ID=26)
		{MenuLevel: 0, ParentId: 0, Path: "report", Name: "report", Hidden: false, Component: "", Sort: 3, Meta: dao.Meta{Title: "报表查询", Icon: "data-analysis"}},
		{MenuLevel: 0, ParentId: 25, Path: "enter", Name: "enter", Hidden: false, Component: "view/report/enter.vue", Sort: 1, Meta: dao.Meta{Title: "进出记录", Icon: "sort"}},
		{MenuLevel: 0, ParentId: 25, Path: "sheet", Name: "sheet", Hidden: false, Component: "view/report/sheet.vue", Sort: 2, Meta: dao.Meta{Title: "异常报表", Icon: "document-copy"}},
		{MenuLevel: 0, ParentId: 25, Path: "paymentRecord", Name: "paymentRecord", Hidden: false, Component: "view/report/paymentRecord.vue", Sort: 3, Meta: dao.Meta{Title: "收费记录", Icon: "money"}},
		{MenuLevel: 0, ParentId: 25, Path: "revenue", Name: "revenueReport", Hidden: false, Component: "view/report/revenue.vue", Sort: 4, Meta: dao.Meta{Title: "收入报表", Icon: "trend-charts"}},
		// 统计查询 (ID=28)
		{MenuLevel: 0, ParentId: 0, Path: "statistics", Name: "statistics", Hidden: false, Component: "", Sort: 4, Meta: dao.Meta{Title: "统计查询", Icon: "chat-line-round"}},
		{MenuLevel: 0, ParentId: 28, Path: "vehicle", Name: "vehicle", Hidden: false, Component: "view/statistics/vehicle.vue", Sort: 1, Meta: dao.Meta{Title: "在线车辆统计", Icon: "van"}},
	}
	for i := range menus {
		menus[i].CreatedAt = now
		menus[i].UpdatedAt = now
	}
	db.Create(&menus)

	// --- sys_authority_menus (new seq IDs: 1=dashboard,2=superAdmin,3-8=subs,9=person,
	//     20=parking,21-24=parking subs, 25=report,26-27=report subs, 28=statistics,29=stats sub) ---
	authMenus := []dao.SysAuthorityMenu{
		{MenuId: "1", AuthorityId: "618"},
		{MenuId: "1", AuthorityId: "888"},
		{MenuId: "1", AuthorityId: "9527"},
		{MenuId: "2", AuthorityId: "888"},
		{MenuId: "2", AuthorityId: "9527"},
		{MenuId: "3", AuthorityId: "888"},
		{MenuId: "3", AuthorityId: "9527"},
		{MenuId: "4", AuthorityId: "888"},
		{MenuId: "4", AuthorityId: "9527"},
		{MenuId: "5", AuthorityId: "888"},
		{MenuId: "5", AuthorityId: "9527"},
		{MenuId: "6", AuthorityId: "888"},
		{MenuId: "6", AuthorityId: "9527"},
		{MenuId: "7", AuthorityId: "888"},
		{MenuId: "7", AuthorityId: "9527"},
		{MenuId: "8", AuthorityId: "888"},
		{MenuId: "8", AuthorityId: "9527"},
		{MenuId: "9", AuthorityId: "618"},
		{MenuId: "9", AuthorityId: "888"},
		{MenuId: "9", AuthorityId: "9527"},
		// 停车管理 + 子菜单 (20-24,30) — admin + operator
		{MenuId: "20", AuthorityId: "618"}, {MenuId: "20", AuthorityId: "888"}, {MenuId: "20", AuthorityId: "9527"},
		{MenuId: "30", AuthorityId: "618"}, {MenuId: "30", AuthorityId: "888"}, {MenuId: "30", AuthorityId: "9527"},
		{MenuId: "21", AuthorityId: "618"}, {MenuId: "21", AuthorityId: "888"}, {MenuId: "21", AuthorityId: "9527"},
		{MenuId: "22", AuthorityId: "618"}, {MenuId: "22", AuthorityId: "888"}, {MenuId: "22", AuthorityId: "9527"},
		{MenuId: "23", AuthorityId: "618"}, {MenuId: "23", AuthorityId: "888"}, {MenuId: "23", AuthorityId: "9527"},
		{MenuId: "24", AuthorityId: "618"}, {MenuId: "24", AuthorityId: "888"}, {MenuId: "24", AuthorityId: "9527"},
		// 报表查询 + 子菜单 (25-27) — admin + operator
		{MenuId: "25", AuthorityId: "618"}, {MenuId: "25", AuthorityId: "888"}, {MenuId: "25", AuthorityId: "9527"},
		{MenuId: "26", AuthorityId: "618"}, {MenuId: "26", AuthorityId: "888"}, {MenuId: "26", AuthorityId: "9527"},
		{MenuId: "27", AuthorityId: "618"}, {MenuId: "27", AuthorityId: "888"}, {MenuId: "27", AuthorityId: "9527"},
		// 收费记录 (32) — admin + operator
		{MenuId: "32", AuthorityId: "618"}, {MenuId: "32", AuthorityId: "888"}, {MenuId: "32", AuthorityId: "9527"},
		// 统计查询 + 子菜单 (28-29) — admin + operator
		{MenuId: "28", AuthorityId: "618"}, {MenuId: "28", AuthorityId: "888"}, {MenuId: "28", AuthorityId: "9527"},
		{MenuId: "29", AuthorityId: "618"}, {MenuId: "29", AuthorityId: "888"}, {MenuId: "29", AuthorityId: "9527"},
		// 进出场操作 (31) — admin + operator
		{MenuId: "31", AuthorityId: "618"}, {MenuId: "31", AuthorityId: "888"}, {MenuId: "31", AuthorityId: "9527"},
		// 包月车管理 (33) — admin + operator
		{MenuId: "33", AuthorityId: "618"}, {MenuId: "33", AuthorityId: "888"}, {MenuId: "33", AuthorityId: "9527"},
		// 交接班 (34) / 收入报表 (35) — admin + operator
		{MenuId: "34", AuthorityId: "618"}, {MenuId: "34", AuthorityId: "888"}, {MenuId: "34", AuthorityId: "9527"},
		{MenuId: "35", AuthorityId: "618"}, {MenuId: "35", AuthorityId: "888"}, {MenuId: "35", AuthorityId: "9527"},
	}
	db.Create(&authMenus)
		// --- vehicle_type (默认车辆类型：临时车，标记为系统内置) ---
		db.FirstOrCreate(&dao.VehicleType{},
			dao.VehicleType{Name: "临时车", IsSystem: true, Remarks: "系统内置"},
		)

	// --- sys_apis (active only, skipping soft-deleted ids 44-66,84,90-98) ---
	apis := []dao.SysApi{
		{Path: "/jwt/jsonInBlacklist", Description: "jwt加入黑名单(退出，必选)", ApiGroup: "jwt", Method: "POST"},
		{Path: "/user/deleteUser", Description: "删除用户", ApiGroup: "系统用户", Method: "DELETE"},
		{Path: "/user/admin_register", Description: "用户注册", ApiGroup: "系统用户", Method: "POST"},
		{Path: "/user/getUserList", Description: "获取用户列表", ApiGroup: "系统用户", Method: "POST"},
		{Path: "/user/setUserInfo", Description: "设置用户信息", ApiGroup: "系统用户", Method: "PUT"},
		{Path: "/user/setSelfInfo", Description: "设置自身信息(必选)", ApiGroup: "系统用户", Method: "PUT"},
		{Path: "/user/getUserInfo", Description: "获取自身信息(必选)", ApiGroup: "系统用户", Method: "GET"},
		{Path: "/user/setUserAuthorities", Description: "设置权限组", ApiGroup: "系统用户", Method: "POST"},
		{Path: "/user/changePassword", Description: "修改密码（建议选择)", ApiGroup: "系统用户", Method: "POST"},
		{Path: "/user/setUserAuthority", Description: "修改用户角色(必选)", ApiGroup: "系统用户", Method: "POST"},
		{Path: "/user/resetPassword", Description: "重置用户密码", ApiGroup: "系统用户", Method: "POST"},
		{Path: "/api/createApi", Description: "创建api", ApiGroup: "api", Method: "POST"},
		{Path: "/api/deleteApi", Description: "删除Api", ApiGroup: "api", Method: "POST"},
		{Path: "/api/updateApi", Description: "更新Api", ApiGroup: "api", Method: "POST"},
		{Path: "/api/getApiList", Description: "获取api列表", ApiGroup: "api", Method: "POST"},
		{Path: "/api/getAllApis", Description: "获取所有api", ApiGroup: "api", Method: "POST"},
		{Path: "/api/getApiById", Description: "获取api详细信息", ApiGroup: "api", Method: "POST"},
		{Path: "/api/deleteApisByIds", Description: "批量删除api", ApiGroup: "api", Method: "DELETE"},
		{Path: "/authority/copyAuthority", Description: "拷贝角色", ApiGroup: "角色", Method: "POST"},
		{Path: "/authority/createAuthority", Description: "创建角色", ApiGroup: "角色", Method: "POST"},
		{Path: "/authority/deleteAuthority", Description: "删除角色", ApiGroup: "角色", Method: "POST"},
		{Path: "/authority/updateAuthority", Description: "更新角色信息", ApiGroup: "角色", Method: "PUT"},
		{Path: "/authority/getAuthorityList", Description: "获取角色列表", ApiGroup: "角色", Method: "POST"},
		{Path: "/authority/setDataAuthority", Description: "设置角色资源权限", ApiGroup: "角色", Method: "POST"},
		{Path: "/casbin/updateCasbin", Description: "更改角色api权限", ApiGroup: "casbin", Method: "POST"},
		{Path: "/casbin/getPolicyPathByAuthorityId", Description: "获取权限列表", ApiGroup: "casbin", Method: "POST"},
		{Path: "/menu/addBaseMenu", Description: "新增菜单", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/getMenu", Description: "获取菜单树(必选)", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/deleteBaseMenu", Description: "删除菜单", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/updateBaseMenu", Description: "更新菜单", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/getBaseMenuById", Description: "根据id获取菜单", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/getMenuList", Description: "分页获取基础menu列表", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/getBaseMenuTree", Description: "获取用户动态路由", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/getMenuAuthority", Description: "获取指定角色menu", ApiGroup: "菜单", Method: "POST"},
		{Path: "/menu/addMenuAuthority", Description: "增加menu和角色关联关系", ApiGroup: "菜单", Method: "POST"},
		{Path: "/fileUploadAndDownload/findFile", Description: "寻找目标文件（秒传）", ApiGroup: "分片上传", Method: "GET"},
		{Path: "/fileUploadAndDownload/breakpointContinue", Description: "断点续传", ApiGroup: "分片上传", Method: "POST"},
		{Path: "/fileUploadAndDownload/breakpointContinueFinish", Description: "断点续传完成", ApiGroup: "分片上传", Method: "POST"},
		{Path: "/fileUploadAndDownload/removeChunk", Description: "上传完成移除文件", ApiGroup: "分片上传", Method: "POST"},
		{Path: "/fileUploadAndDownload/upload", Description: "文件上传示例", ApiGroup: "文件上传与下载", Method: "POST"},
		{Path: "/fileUploadAndDownload/deleteFile", Description: "删除文件", ApiGroup: "文件上传与下载", Method: "POST"},
		{Path: "/fileUploadAndDownload/editFileName", Description: "文件名或者备注编辑", ApiGroup: "文件上传与下载", Method: "POST"},
		{Path: "/fileUploadAndDownload/getFileList", Description: "获取上传文件列表", ApiGroup: "文件上传与下载", Method: "POST"},
		{Path: "/sysDictionaryDetail/updateSysDictionaryDetail", Description: "更新字典内容", ApiGroup: "系统字典详情", Method: "PUT"},
		{Path: "/sysDictionaryDetail/createSysDictionaryDetail", Description: "新增字典内容", ApiGroup: "系统字典详情", Method: "POST"},
		{Path: "/sysDictionaryDetail/deleteSysDictionaryDetail", Description: "删除字典内容", ApiGroup: "系统字典详情", Method: "DELETE"},
		{Path: "/sysDictionaryDetail/findSysDictionaryDetail", Description: "根据ID获取字典内容", ApiGroup: "系统字典详情", Method: "GET"},
		{Path: "/sysDictionaryDetail/getSysDictionaryDetailList", Description: "获取字典内容列表", ApiGroup: "系统字典详情", Method: "GET"},
		{Path: "/sysDictionary/createSysDictionary", Description: "新增字典", ApiGroup: "系统字典", Method: "POST"},
		{Path: "/sysDictionary/deleteSysDictionary", Description: "删除字典", ApiGroup: "系统字典", Method: "DELETE"},
		{Path: "/sysDictionary/updateSysDictionary", Description: "更新字典", ApiGroup: "系统字典", Method: "PUT"},
		{Path: "/sysDictionary/findSysDictionary", Description: "根据ID获取字典", ApiGroup: "系统字典", Method: "GET"},
		{Path: "/sysDictionary/getSysDictionaryList", Description: "获取字典列表", ApiGroup: "系统字典", Method: "GET"},
		{Path: "/sysOperationRecord/createSysOperationRecord", Description: "新增操作记录", ApiGroup: "操作记录", Method: "POST"},
		{Path: "/sysOperationRecord/findSysOperationRecord", Description: "根据ID获取操作记录", ApiGroup: "操作记录", Method: "GET"},
		{Path: "/sysOperationRecord/getSysOperationRecordList", Description: "获取操作记录列表", ApiGroup: "操作记录", Method: "GET"},
		{Path: "/sysOperationRecord/deleteSysOperationRecord", Description: "删除操作记录", ApiGroup: "操作记录", Method: "DELETE"},
		{Path: "/sysOperationRecord/deleteSysOperationRecordByIds", Description: "批量删除操作历史", ApiGroup: "操作记录", Method: "DELETE"},
		{Path: "/simpleUploader/upload", Description: "插件版分片上传", ApiGroup: "断点续传(插件版)", Method: "POST"},
		{Path: "/simpleUploader/checkFileMd5", Description: "文件完整度验证", ApiGroup: "断点续传(插件版)", Method: "GET"},
		{Path: "/email/emailTest", Description: "发送测试邮件", ApiGroup: "email", Method: "POST"},
		{Path: "/email/emailSend", Description: "发送邮件示例", ApiGroup: "email", Method: "POST"},
		{Path: "/authorityBtn/setAuthorityBtn", Description: "设置按钮权限", ApiGroup: "按钮权限", Method: "POST"},
		{Path: "/authorityBtn/getAuthorityBtn", Description: "获取已有按钮权限", ApiGroup: "按钮权限", Method: "POST"},
		{Path: "/authorityBtn/canRemoveAuthorityBtn", Description: "删除按钮", ApiGroup: "按钮权限", Method: "POST"},
	}
	for i := range apis {
		apis[i].CreatedAt = now
		apis[i].UpdatedAt = now
	}
	db.Create(&apis)

	// --- casbin_rule ---
	type casbinRule struct {
		Ptype string `gorm:"column:ptype"`
		V0    string `gorm:"column:v0"`
		V1    string `gorm:"column:v1"`
		V2    string `gorm:"column:v2"`
		V3    string `gorm:"column:v3"`
		V4    string `gorm:"column:v4"`
		V5    string `gorm:"column:v5"`
	}
	casbinRules := []casbinRule{
		// 618 (普通用户)
		{"p", "618", "/base/login", "POST", "", "", ""},
		{"p", "618", "/jwt/jsonInBlacklist", "POST", "", "", ""},
		{"p", "618", "/menu/getMenu", "POST", "", "", ""},
		{"p", "618", "/user/admin_register", "POST", "", "", ""},
		{"p", "618", "/user/changePassword", "POST", "", "", ""},
		{"p", "618", "/user/getUserInfo", "GET", "", "", ""},
		{"p", "618", "/user/setUserAuthority", "POST", "", "", ""},
		{"p", "618", "/user/setUserInfo", "PUT", "", "", ""},
		// 888 (管理员)
		{"p", "888", "/user/admin_register", "POST", "", "", ""},
		{"p", "888", "/api/createApi", "POST", "", "", ""},
		{"p", "888", "/api/getApiList", "POST", "", "", ""},
		{"p", "888", "/api/getApiById", "POST", "", "", ""},
		{"p", "888", "/api/deleteApi", "POST", "", "", ""},
		{"p", "888", "/api/updateApi", "POST", "", "", ""},
		{"p", "888", "/api/getAllApis", "POST", "", "", ""},
		{"p", "888", "/api/deleteApisByIds", "DELETE", "", "", ""},
		{"p", "888", "/authority/copyAuthority", "POST", "", "", ""},
		{"p", "888", "/authority/updateAuthority", "PUT", "", "", ""},
		{"p", "888", "/authority/createAuthority", "POST", "", "", ""},
		{"p", "888", "/authority/deleteAuthority", "POST", "", "", ""},
		{"p", "888", "/authority/getAuthorityList", "POST", "", "", ""},
		{"p", "888", "/authority/setDataAuthority", "POST", "", "", ""},
		{"p", "888", "/menu/getMenu", "POST", "", "", ""},
		{"p", "888", "/menu/getMenuList", "POST", "", "", ""},
		{"p", "888", "/menu/addBaseMenu", "POST", "", "", ""},
		{"p", "888", "/menu/getBaseMenuTree", "POST", "", "", ""},
		{"p", "888", "/menu/addMenuAuthority", "POST", "", "", ""},
		{"p", "888", "/menu/getMenuAuthority", "POST", "", "", ""},
		{"p", "888", "/menu/deleteBaseMenu", "POST", "", "", ""},
		{"p", "888", "/menu/updateBaseMenu", "POST", "", "", ""},
		{"p", "888", "/menu/getBaseMenuById", "POST", "", "", ""},
		{"p", "888", "/user/getUserInfo", "GET", "", "", ""},
		{"p", "888", "/user/setUserInfo", "PUT", "", "", ""},
		{"p", "888", "/user/setSelfInfo", "PUT", "", "", ""},
		{"p", "888", "/user/getUserList", "POST", "", "", ""},
		{"p", "888", "/user/deleteUser", "DELETE", "", "", ""},
		{"p", "888", "/user/changePassword", "POST", "", "", ""},
		{"p", "888", "/user/setUserAuthority", "POST", "", "", ""},
		{"p", "888", "/user/setUserAuthorities", "POST", "", "", ""},
		{"p", "888", "/user/resetPassword", "POST", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/findFile", "GET", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/breakpointContinueFinish", "POST", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/breakpointContinue", "POST", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/removeChunk", "POST", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/upload", "POST", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/deleteFile", "POST", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/editFileName", "POST", "", "", ""},
		{"p", "888", "/fileUploadAndDownload/getFileList", "POST", "", "", ""},
		{"p", "888", "/casbin/updateCasbin", "POST", "", "", ""},
		{"p", "888", "/casbin/getPolicyPathByAuthorityId", "POST", "", "", ""},
		{"p", "888", "/jwt/jsonInBlacklist", "POST", "", "", ""},
		{"p", "888", "/sysDictionaryDetail/findSysDictionaryDetail", "GET", "", "", ""},
		{"p", "888", "/sysDictionaryDetail/updateSysDictionaryDetail", "PUT", "", "", ""},
		{"p", "888", "/sysDictionaryDetail/createSysDictionaryDetail", "POST", "", "", ""},
		{"p", "888", "/sysDictionaryDetail/getSysDictionaryDetailList", "GET", "", "", ""},
		{"p", "888", "/sysDictionaryDetail/deleteSysDictionaryDetail", "DELETE", "", "", ""},
		{"p", "888", "/sysDictionary/findSysDictionary", "GET", "", "", ""},
		{"p", "888", "/sysDictionary/updateSysDictionary", "PUT", "", "", ""},
		{"p", "888", "/sysDictionary/createSysDictionary", "POST", "", "", ""},
		{"p", "888", "/sysDictionary/deleteSysDictionary", "DELETE", "", "", ""},
		{"p", "888", "/sysDictionary/getSysDictionaryList", "GET", "", "", ""},
		{"p", "888", "/sysOperationRecord/findSysOperationRecord", "GET", "", "", ""},
		{"p", "888", "/sysOperationRecord/updateSysOperationRecord", "PUT", "", "", ""},
		{"p", "888", "/sysOperationRecord/createSysOperationRecord", "POST", "", "", ""},
		{"p", "888", "/sysOperationRecord/getSysOperationRecordList", "GET", "", "", ""},
		{"p", "888", "/sysOperationRecord/deleteSysOperationRecord", "DELETE", "", "", ""},
		{"p", "888", "/sysOperationRecord/deleteSysOperationRecordByIds", "DELETE", "", "", ""},
		{"p", "888", "/simpleUploader/upload", "POST", "", "", ""},
		{"p", "888", "/simpleUploader/checkFileMd5", "GET", "", "", ""},
		{"p", "888", "/email/emailTest", "POST", "", "", ""},
		{"p", "888", "/authorityBtn/setAuthorityBtn", "POST", "", "", ""},
		{"p", "888", "/authorityBtn/getAuthorityBtn", "POST", "", "", ""},
		{"p", "888", "/authorityBtn/canRemoveAuthorityBtn", "POST", "", "", ""},
		// 9527 (开发)
		{"p", "9527", "/jwt/jsonInBlacklist", "POST", "", "", ""},
		{"p", "9527", "/user/deleteUser", "DELETE", "", "", ""},
		{"p", "9527", "/user/admin_register", "POST", "", "", ""},
		{"p", "9527", "/user/getUserList", "POST", "", "", ""},
		{"p", "9527", "/user/setUserInfo", "PUT", "", "", ""},
		{"p", "9527", "/user/setSelfInfo", "PUT", "", "", ""},
		{"p", "9527", "/user/getUserInfo", "GET", "", "", ""},
		{"p", "9527", "/user/setUserAuthorities", "POST", "", "", ""},
		{"p", "9527", "/user/changePassword", "POST", "", "", ""},
		{"p", "9527", "/user/setUserAuthority", "POST", "", "", ""},
		{"p", "9527", "/user/resetPassword", "POST", "", "", ""},
		{"p", "9527", "/api/createApi", "POST", "", "", ""},
		{"p", "9527", "/api/deleteApi", "POST", "", "", ""},
		{"p", "9527", "/api/updateApi", "POST", "", "", ""},
		{"p", "9527", "/api/getApiList", "POST", "", "", ""},
		{"p", "9527", "/api/getAllApis", "POST", "", "", ""},
		{"p", "9527", "/api/getApiById", "POST", "", "", ""},
		{"p", "9527", "/api/deleteApisByIds", "DELETE", "", "", ""},
		{"p", "9527", "/authority/copyAuthority", "POST", "", "", ""},
		{"p", "9527", "/authority/createAuthority", "POST", "", "", ""},
		{"p", "9527", "/authority/deleteAuthority", "POST", "", "", ""},
		{"p", "9527", "/authority/updateAuthority", "PUT", "", "", ""},
		{"p", "9527", "/authority/getAuthorityList", "POST", "", "", ""},
		{"p", "9527", "/authority/setDataAuthority", "POST", "", "", ""},
		{"p", "9527", "/casbin/updateCasbin", "POST", "", "", ""},
		{"p", "9527", "/casbin/getPolicyPathByAuthorityId", "POST", "", "", ""},
		{"p", "9527", "/menu/addBaseMenu", "POST", "", "", ""},
		{"p", "9527", "/menu/getMenu", "POST", "", "", ""},
		{"p", "9527", "/menu/deleteBaseMenu", "POST", "", "", ""},
		{"p", "9527", "/menu/updateBaseMenu", "POST", "", "", ""},
		{"p", "9527", "/menu/getBaseMenuById", "POST", "", "", ""},
		{"p", "9527", "/menu/getMenuList", "POST", "", "", ""},
		{"p", "9527", "/menu/getBaseMenuTree", "POST", "", "", ""},
		{"p", "9527", "/menu/getMenuAuthority", "POST", "", "", ""},
		{"p", "9527", "/menu/addMenuAuthority", "POST", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/findFile", "GET", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/breakpointContinue", "POST", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/breakpointContinueFinish", "POST", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/removeChunk", "POST", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/upload", "POST", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/deleteFile", "POST", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/editFileName", "POST", "", "", ""},
		{"p", "9527", "/fileUploadAndDownload/getFileList", "POST", "", "", ""},
		{"p", "9527", "/sysDictionaryDetail/updateSysDictionaryDetail", "PUT", "", "", ""},
		{"p", "9527", "/sysDictionaryDetail/createSysDictionaryDetail", "POST", "", "", ""},
		{"p", "9527", "/sysDictionaryDetail/deleteSysDictionaryDetail", "DELETE", "", "", ""},
		{"p", "9527", "/sysDictionaryDetail/findSysDictionaryDetail", "GET", "", "", ""},
		{"p", "9527", "/sysDictionaryDetail/getSysDictionaryDetailList", "GET", "", "", ""},
		{"p", "9527", "/sysDictionary/createSysDictionary", "POST", "", "", ""},
		{"p", "9527", "/sysDictionary/deleteSysDictionary", "DELETE", "", "", ""},
		{"p", "9527", "/sysDictionary/updateSysDictionary", "PUT", "", "", ""},
		{"p", "9527", "/sysDictionary/findSysDictionary", "GET", "", "", ""},
		{"p", "9527", "/sysDictionary/getSysDictionaryList", "GET", "", "", ""},
		{"p", "9527", "/sysOperationRecord/createSysOperationRecord", "POST", "", "", ""},
		{"p", "9527", "/sysOperationRecord/findSysOperationRecord", "GET", "", "", ""},
		{"p", "9527", "/sysOperationRecord/getSysOperationRecordList", "GET", "", "", ""},
		{"p", "9527", "/sysOperationRecord/deleteSysOperationRecord", "DELETE", "", "", ""},
		{"p", "9527", "/sysOperationRecord/deleteSysOperationRecordByIds", "DELETE", "", "", ""},
		{"p", "9527", "/simpleUploader/upload", "POST", "", "", ""},
		{"p", "9527", "/simpleUploader/checkFileMd5", "GET", "", "", ""},
		{"p", "9527", "/email/emailTest", "POST", "", "", ""},
		{"p", "9527", "/email/emailSend", "POST", "", "", ""},
		{"p", "9527", "/authorityBtn/setAuthorityBtn", "POST", "", "", ""},
		{"p", "9527", "/authorityBtn/getAuthorityBtn", "POST", "", "", ""},
		{"p", "9527", "/authorityBtn/canRemoveAuthorityBtn", "POST", "", "", ""},
		// 收费结算 (vehicle/exit/preview, vehicle/exit/confirm) — admin + operator
		{"p", "888", "/vehicle/exit/preview", "POST", "", "", ""},
		{"p", "618", "/vehicle/exit/preview", "POST", "", "", ""},
		{"p", "888", "/vehicle/exit/confirm", "POST", "", "", ""},
		{"p", "618", "/vehicle/exit/confirm", "POST", "", "", ""},
		// 包月车管理
		{"p", "888", "/monthly-card/create", "POST", "", "", ""},
		{"p", "618", "/monthly-card/create", "POST", "", "", ""},
		{"p", "888", "/monthly-card/renew", "POST", "", "", ""},
		{"p", "618", "/monthly-card/renew", "POST", "", "", ""},
		{"p", "888", "/monthly-card/refund", "DELETE", "", "", ""},
		{"p", "618", "/monthly-card/refund", "DELETE", "", "", ""},
		{"p", "888", "/monthly-card/list", "GET", "", "", ""},
		{"p", "618", "/monthly-card/list", "GET", "", "", ""},
		// 交接班
		{"p", "888", "/shift/start", "POST", "", "", ""},
		{"p", "618", "/shift/start", "POST", "", "", ""},
		{"p", "888", "/shift/end", "POST", "", "", ""},
		{"p", "618", "/shift/end", "POST", "", "", ""},
		{"p", "888", "/shift/current", "GET", "", "", ""},
		{"p", "618", "/shift/current", "GET", "", "", ""},
		{"p", "888", "/shift/list", "GET", "", "", ""},
		{"p", "618", "/shift/list", "GET", "", "", ""},
		// 收入报表
		{"p", "888", "/report/revenue", "GET", "", "", ""},
		{"p", "618", "/report/revenue", "GET", "", "", ""},
		// 收费记录查询
		{"p", "888", "/payment/list", "GET", "", "", ""},
		{"p", "618", "/payment/list", "GET", "", "", ""},
	}
	// Ensure casbin_rule table exists before seeding
	db.Exec(`CREATE TABLE IF NOT EXISTS casbin_rule (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		ptype TEXT,
		v0 TEXT,
		v1 TEXT,
		v2 TEXT,
		v3 TEXT,
		v4 TEXT,
		v5 TEXT
	)`)

	// Use raw SQL to insert casbin rules
	for _, r := range casbinRules {
		db.Exec("INSERT INTO casbin_rule (ptype, v0, v1, v2, v3, v4, v5) VALUES (?, ?, ?, ?, ?, ?, ?)",
			r.Ptype, r.V0, r.V1, r.V2, r.V3, r.V4, r.V5)
	}

	// --- sys_dictionaries (active only) ---
	dictionaries := []dao.SysDictionary{
		{Name: "性别", Type: "sex", Status: ptr(true), Desc: "性别字典", SysDictionaryDetails: []dao.SysDictionaryDetail{
			{Label: "男", Value: "1", Status: ptr(true), Sort: 1},
			{Label: "女", Value: "2", Status: ptr(true), Sort: 2},
		}},
		{Name: "数据库int类型", Type: "int", Status: ptr(true), Desc: "int类型对应的数据库类型", SysDictionaryDetails: []dao.SysDictionaryDetail{
			{Label: "smallint", Value: "1", Extend: "mysql", Status: ptr(true), Sort: 1},
			{Label: "mediumint", Value: "2", Extend: "mysql", Status: ptr(true), Sort: 2},
			{Label: "int", Value: "3", Extend: "mysql", Status: ptr(true), Sort: 3},
			{Label: "bigint", Value: "4", Extend: "mysql", Status: ptr(true), Sort: 4},
			{Label: "int2", Value: "5", Extend: "pgsql", Status: ptr(true), Sort: 5},
			{Label: "int4", Value: "6", Extend: "pgsql", Status: ptr(true), Sort: 6},
			{Label: "int6", Value: "7", Extend: "pgsql", Status: ptr(true), Sort: 7},
			{Label: "int8", Value: "8", Extend: "pgsql", Status: ptr(true), Sort: 8},
		}},
		{Name: "数据库时间日期类型", Type: "time.Time", Status: ptr(true), Desc: "数据库时间日期类型", SysDictionaryDetails: []dao.SysDictionaryDetail{
			{Label: "date", Value: "", Status: ptr(true), Sort: 0},
			{Label: "time", Value: "1", Extend: "mysql", Status: ptr(true), Sort: 1},
			{Label: "year", Value: "2", Extend: "mysql", Status: ptr(true), Sort: 2},
			{Label: "datetime", Value: "3", Extend: "mysql", Status: ptr(true), Sort: 3},
			{Label: "timestamp", Value: "5", Extend: "mysql", Status: ptr(true), Sort: 5},
			{Label: "timestamptz", Value: "6", Extend: "pgsql", Status: ptr(true), Sort: 5},
		}},
		{Name: "数据库浮点型", Type: "float64", Status: ptr(true), Desc: "数据库浮点型", SysDictionaryDetails: []dao.SysDictionaryDetail{
			{Label: "float", Value: "", Status: ptr(true), Sort: 0},
			{Label: "double", Value: "1", Extend: "mysql", Status: ptr(true), Sort: 1},
			{Label: "decimal", Value: "2", Extend: "mysql", Status: ptr(true), Sort: 2},
			{Label: "numeric", Value: "3", Extend: "pgsql", Status: ptr(true), Sort: 3},
			{Label: "smallserial", Value: "4", Extend: "pgsql", Status: ptr(true), Sort: 4},
		}},
		{Name: "数据库字符串", Type: "string", Status: ptr(true), Desc: "数据库字符串", SysDictionaryDetails: []dao.SysDictionaryDetail{
			{Label: "char", Value: "", Status: ptr(true), Sort: 0},
			{Label: "varchar", Value: "1", Extend: "mysql", Status: ptr(true), Sort: 1},
			{Label: "tinyblob", Value: "2", Extend: "mysql", Status: ptr(true), Sort: 2},
			{Label: "tinytext", Value: "3", Extend: "mysql", Status: ptr(true), Sort: 3},
			{Label: "text", Value: "4", Extend: "mysql", Status: ptr(true), Sort: 4},
			{Label: "blob", Value: "5", Extend: "mysql", Status: ptr(true), Sort: 5},
			{Label: "mediumblob", Value: "6", Extend: "mysql", Status: ptr(true), Sort: 6},
			{Label: "mediumtext", Value: "7", Extend: "mysql", Status: ptr(true), Sort: 7},
			{Label: "longblob", Value: "8", Extend: "mysql", Status: ptr(true), Sort: 8},
			{Label: "longtext", Value: "9", Extend: "mysql", Status: ptr(true), Sort: 9},
		}},
		{Name: "数据库bool类型", Type: "bool", Status: ptr(true), Desc: "数据库bool类型", SysDictionaryDetails: []dao.SysDictionaryDetail{
			{Label: "tinyint", Value: "1", Extend: "mysql", Status: ptr(true), Sort: 0},
			{Label: "bool", Value: "2", Extend: "pgsql", Status: ptr(true), Sort: 0},
		}},
	}
	for i := range dictionaries {
		dictionaries[i].CreatedAt = now
		dictionaries[i].UpdatedAt = now
		for j := range dictionaries[i].SysDictionaryDetails {
			dictionaries[i].SysDictionaryDetails[j].CreatedAt = now
			dictionaries[i].SysDictionaryDetails[j].UpdatedAt = now
		}
	}
	db.Create(&dictionaries)

	global.GVA_LOG.Info("system data seeded successfully")
}

// ptr returns a pointer to the given value.
func ptr[T any](v T) *T {
	return &v
}
