package system

import (
	"context"
	"fmt"

	"kra/internal/biz/system"
	"kra/internal/data/model"

	"github.com/pkg/errors"
	"gorm.io/gorm"
)

const initOrderDictDetail = initOrderDict + 1

type initDictDetail struct{}

func NewInitDictDetail() system.SubInitializer {
	return &initDictDetail{}
}

func (i *initDictDetail) MigrateTable(ctx context.Context) (context.Context, error) {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return ctx, system.ErrMissingDBContext
	}
	return ctx, db.AutoMigrate(&model.SysDictionaryDetail{})
}

func (i *initDictDetail) TableCreated(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	return db.Migrator().HasTable(&model.SysDictionaryDetail{})
}

func (i *initDictDetail) InitializerName() string {
	return model.TableNameSysDictionaryDetail
}

func (i *initDictDetail) InitializeData(ctx context.Context) (context.Context, error) {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return ctx, system.ErrMissingDBContext
	}
	dicts, ok := ctx.Value(model.TableNameSysDictionary).([]model.SysDictionary)
	if !ok {
		return ctx, errors.Wrap(system.ErrMissingDependentContext,
			fmt.Sprintf("未找到 %s 表初始化数据", model.TableNameSysDictionary))
	}

	// 性别字典详情
	genderDetails := []model.SysDictionaryDetail{
		{Label: "男", Value: "1", Status: true, Sort: 1, SysDictionaryID: dicts[0].ID},
		{Label: "女", Value: "2", Status: true, Sort: 2, SysDictionaryID: dicts[0].ID},
	}

	// int类型字典详情
	intDetails := []model.SysDictionaryDetail{
		{Label: "smallint", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[1].ID},
		{Label: "mediumint", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[1].ID},
		{Label: "int", Value: "3", Status: true, Extend: "mysql", Sort: 3, SysDictionaryID: dicts[1].ID},
		{Label: "bigint", Value: "4", Status: true, Extend: "mysql", Sort: 4, SysDictionaryID: dicts[1].ID},
		{Label: "int2", Value: "5", Status: true, Extend: "pgsql", Sort: 5, SysDictionaryID: dicts[1].ID},
		{Label: "int4", Value: "6", Status: true, Extend: "pgsql", Sort: 6, SysDictionaryID: dicts[1].ID},
		{Label: "int6", Value: "7", Status: true, Extend: "pgsql", Sort: 7, SysDictionaryID: dicts[1].ID},
		{Label: "int8", Value: "8", Status: true, Extend: "pgsql", Sort: 8, SysDictionaryID: dicts[1].ID},
	}

	// 时间类型字典详情
	timeDetails := []model.SysDictionaryDetail{
		{Label: "date", Value: "0", Status: true, Extend: "mysql", Sort: 0, SysDictionaryID: dicts[2].ID},
		{Label: "time", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[2].ID},
		{Label: "year", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[2].ID},
		{Label: "datetime", Value: "3", Status: true, Extend: "mysql", Sort: 3, SysDictionaryID: dicts[2].ID},
		{Label: "timestamp", Value: "5", Status: true, Extend: "mysql", Sort: 5, SysDictionaryID: dicts[2].ID},
		{Label: "timestamptz", Value: "6", Status: true, Extend: "pgsql", Sort: 5, SysDictionaryID: dicts[2].ID},
	}

	// 浮点类型字典详情
	floatDetails := []model.SysDictionaryDetail{
		{Label: "float", Value: "0", Status: true, Extend: "mysql", Sort: 0, SysDictionaryID: dicts[3].ID},
		{Label: "double", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[3].ID},
		{Label: "decimal", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[3].ID},
		{Label: "numeric", Value: "3", Status: true, Extend: "pgsql", Sort: 3, SysDictionaryID: dicts[3].ID},
		{Label: "smallserial", Value: "4", Status: true, Extend: "pgsql", Sort: 4, SysDictionaryID: dicts[3].ID},
	}

	// 字符串类型字典详情
	stringDetails := []model.SysDictionaryDetail{
		{Label: "char", Value: "0", Status: true, Extend: "mysql", Sort: 0, SysDictionaryID: dicts[4].ID},
		{Label: "varchar", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[4].ID},
		{Label: "tinyblob", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[4].ID},
		{Label: "tinytext", Value: "3", Status: true, Extend: "mysql", Sort: 3, SysDictionaryID: dicts[4].ID},
		{Label: "text", Value: "4", Status: true, Extend: "mysql", Sort: 4, SysDictionaryID: dicts[4].ID},
		{Label: "blob", Value: "5", Status: true, Extend: "mysql", Sort: 5, SysDictionaryID: dicts[4].ID},
		{Label: "mediumblob", Value: "6", Status: true, Extend: "mysql", Sort: 6, SysDictionaryID: dicts[4].ID},
		{Label: "mediumtext", Value: "7", Status: true, Extend: "mysql", Sort: 7, SysDictionaryID: dicts[4].ID},
		{Label: "longblob", Value: "8", Status: true, Extend: "mysql", Sort: 8, SysDictionaryID: dicts[4].ID},
		{Label: "longtext", Value: "9", Status: true, Extend: "mysql", Sort: 9, SysDictionaryID: dicts[4].ID},
	}

	// bool类型字典详情
	boolDetails := []model.SysDictionaryDetail{
		{Label: "tinyint", Value: "1", Extend: "mysql", Status: true, SysDictionaryID: dicts[5].ID},
		{Label: "bool", Value: "2", Extend: "pgsql", Status: true, SysDictionaryID: dicts[5].ID},
	}

	// 批量创建所有字典详情
	allDetails := append(genderDetails, intDetails...)
	allDetails = append(allDetails, timeDetails...)
	allDetails = append(allDetails, floatDetails...)
	allDetails = append(allDetails, stringDetails...)
	allDetails = append(allDetails, boolDetails...)

	if err := db.Create(&allDetails).Error; err != nil {
		return ctx, errors.Wrap(err, model.TableNameSysDictionaryDetail+"表数据初始化失败!")
	}

	return ctx, nil
}

func (i *initDictDetail) DataInserted(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	var count int64
	if err := db.Model(&model.SysDictionaryDetail{}).Where("label = ?", "tinyint").Count(&count).Error; err != nil {
		return false
	}
	return count > 0
}
