package system

import (
	"time"

	"kra/internal/biz"

	"gorm.io/gorm"
)

type userPO struct {
	ID                 uint `gorm:"primaryKey"`
	CreatedAt          time.Time
	UpdatedAt          time.Time
	DeletedAt          gorm.DeletedAt `gorm:"index"`
	UUID               string         `gorm:"type:char(36);index"`
	Username           string         `gorm:"index"`
	Password           string
	NickName           string `gorm:"column:nick_name"`
	HeaderImg          string `gorm:"column:header_img"`
	AuthorityID        uint   `gorm:"column:authority_id"`
	DeptID             uint   `gorm:"column:dept_id"`
	Phone              string
	Email              string
	Enable             int    `gorm:"default:1"`
	OriginSetting      string `gorm:"type:text"`
	PasswordUpdatedAt  *time.Time
	MustChangePassword bool
}

func (userPO) TableName() string { return "sys_users" }

type authorityPO struct {
	CreatedAt     time.Time
	UpdatedAt     time.Time
	DeletedAt     gorm.DeletedAt `gorm:"index"`
	AuthorityID   uint           `gorm:"primaryKey;column:authority_id"`
	AuthorityName string
	ParentID      *uint
	DataScope     int    `gorm:"default:1"`
	DefaultRouter string `gorm:"default:dashboard"`
}

func (authorityPO) TableName() string { return "sys_authorities" }

type menuPO struct {
	ID             uint `gorm:"primaryKey"`
	CreatedAt      time.Time
	UpdatedAt      time.Time
	DeletedAt      gorm.DeletedAt `gorm:"index"`
	MenuLevel      uint
	ParentID       uint `gorm:"column:parent_id"`
	Path           string
	Name           string
	Hidden         bool
	Component      string
	Sort           int
	ActiveName     string
	KeepAlive      bool
	DefaultMenu    bool
	Title          string
	Icon           string
	CloseTab       bool
	TransitionType string
}

func (menuPO) TableName() string { return "sys_base_menus" }

type userAuthorityPO struct {
	// The reference join model deliberately has no primary key or uniqueness
	// constraint.  Keep duplicate IDs representable; association replacement
	// and validation are handled by the service transaction instead.
	SysUserID               uint `gorm:"column:sys_user_id"`
	SysAuthorityAuthorityID uint `gorm:"column:sys_authority_authority_id"`
}

func (userAuthorityPO) TableName() string { return "sys_user_authority" }

type authorityMenuPO struct {
	SysAuthorityAuthorityID uint `gorm:"column:sys_authority_authority_id"`
	SysBaseMenuID           uint `gorm:"column:sys_base_menu_id"`
}

func (authorityMenuPO) TableName() string { return "sys_authority_menus" }

type menuParameterPO struct {
	ID        uint `gorm:"primaryKey"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
	MenuID    uint           `gorm:"column:sys_base_menu_id;index"`
	Type      string
	Key       string
	Value     string
}

func (menuParameterPO) TableName() string { return "sys_base_menu_parameters" }

type userRepo struct{ data Provider }

func NewUserRepo(data Provider) biz.UserRepo { return &userRepo{data: data} }
