package model

const (
	UserStatusActive   = "active"
	UserStatusLocked   = "locked"
	UserStatusDisabled = "disabled"

	JobStatusPending   = "pending"
	JobStatusRunning   = "running"
	JobStatusSucceeded = "succeeded"
	JobStatusFailed    = "failed"
	JobStatusCanceled  = "canceled"
)

type User struct {
	ID            uint   `gorm:"primaryKey" json:"id"`
	Username      string `gorm:"size:64;uniqueIndex;not null" json:"account"`
	Name          string `gorm:"size:80;not null" json:"name"`
	PasswordHash  string `gorm:"size:255;not null" json:"-"`
	Team          string `gorm:"size:80" json:"team"`
	Status        string `gorm:"size:24;index;not null;default:active" json:"status"`
	MFAEnabled    bool   `gorm:"not null;default:false" json:"mfaEnabled"`
	LastLoginAtMS *int64 `gorm:"column:last_login_at_ms" json:"lastLoginAtMs"`
	Roles         []Role `gorm:"many2many:admin_user_roles;" json:"roles"`
	CreatedAtMS   int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS   int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (User) TableName() string {
	return "admin_users"
}

type Role struct {
	ID          uint         `gorm:"primaryKey" json:"id"`
	Name        string       `gorm:"size:80;uniqueIndex;not null" json:"name"`
	Code        string       `gorm:"size:80;uniqueIndex;not null" json:"code"`
	Description string       `gorm:"size:255" json:"description"`
	Users       []User       `gorm:"many2many:admin_user_roles;" json:"-"`
	Permissions []Permission `gorm:"many2many:admin_role_permissions;" json:"permissions"`
	CreatedAtMS int64        `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64        `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (Role) TableName() string {
	return "admin_roles"
}

type Permission struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	Name        string `gorm:"size:100;not null" json:"name"`
	Code        string `gorm:"size:100;uniqueIndex;not null" json:"code"`
	Kind        string `gorm:"size:32;index;not null" json:"kind"`
	Description string `gorm:"size:255" json:"description"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (Permission) TableName() string {
	return "admin_permissions"
}

type Menu struct {
	ID             uint   `gorm:"primaryKey" json:"id"`
	ParentID       *uint  `gorm:"index" json:"parentId"`
	Title          string `gorm:"size:80;not null" json:"label"`
	Code           string `gorm:"size:100;uniqueIndex;not null" json:"code"`
	Path           string `gorm:"size:160" json:"path"`
	Icon           string `gorm:"size:64" json:"icon"`
	PermissionCode string `gorm:"size:100;index" json:"permissionCode"`
	Sort           int    `gorm:"not null;default:0" json:"sort"`
	Visible        bool   `gorm:"not null;default:true" json:"visible"`
	CreatedAtMS    int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS    int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (Menu) TableName() string {
	return "admin_menus"
}

type AppConfig struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	Group       string `gorm:"size:64;index:idx_admin_app_config_group_key,unique;not null" json:"group"`
	Key         string `gorm:"size:100;index:idx_admin_app_config_group_key,unique;not null" json:"key"`
	Value       string `gorm:"type:text" json:"value"`
	Description string `gorm:"size:255" json:"description"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (AppConfig) TableName() string {
	return "admin_app_configs"
}

type AppBanner struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	AppCode     string `gorm:"size:32;index:idx_admin_app_banners_app_sort,not null;default:lalu" json:"appCode"`
	CoverURL    string `gorm:"size:1024;not null" json:"coverUrl"`
	BannerType  string `gorm:"size:16;not null" json:"bannerType"`
	Param       string `gorm:"size:2048" json:"param"`
	Status      string `gorm:"size:24;index:idx_admin_app_banners_app_sort,not null;default:active" json:"status"`
	Platform    string `gorm:"size:24;index:idx_admin_app_banners_scope,not null" json:"platform"`
	SortOrder   int    `gorm:"index:idx_admin_app_banners_app_sort,not null;default:0" json:"sortOrder"`
	RegionID    int64  `gorm:"index:idx_admin_app_banners_scope,not null;default:0" json:"regionId"`
	CountryCode string `gorm:"size:8;index:idx_admin_app_banners_scope" json:"countryCode"`
	Description string `gorm:"size:255" json:"description"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (AppBanner) TableName() string {
	return "admin_app_banners"
}

type AppVersion struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	AppCode     string `gorm:"size:32;uniqueIndex:uk_admin_app_versions_build,not null;default:lalu" json:"appCode"`
	Platform    string `gorm:"size:24;uniqueIndex:uk_admin_app_versions_build;index:idx_admin_app_versions_latest,not null" json:"platform"`
	Version     string `gorm:"size:40;not null" json:"version"`
	BuildNumber int64  `gorm:"uniqueIndex:uk_admin_app_versions_build;index:idx_admin_app_versions_latest,not null" json:"buildNumber"`
	ForceUpdate bool   `gorm:"not null;default:false" json:"forceUpdate"`
	DownloadURL string `gorm:"size:2048;not null" json:"downloadUrl"`
	Description string `gorm:"type:text" json:"description"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (AppVersion) TableName() string {
	return "admin_app_versions"
}

type RefreshToken struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	UserID      uint   `gorm:"index;not null" json:"userId"`
	TokenHash   string `gorm:"size:128;uniqueIndex;not null" json:"-"`
	UserAgent   string `gorm:"size:255" json:"userAgent"`
	IP          string `gorm:"size:64" json:"ip"`
	ExpiresAtMS int64  `gorm:"column:expires_at_ms;index;not null" json:"expiresAtMs"`
	RevokedAtMS *int64 `gorm:"column:revoked_at_ms" json:"revokedAtMs"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (RefreshToken) TableName() string {
	return "admin_refresh_tokens"
}

type LoginLog struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	Username    string `gorm:"size:64;index" json:"username"`
	UserID      *uint  `gorm:"index" json:"userId"`
	IP          string `gorm:"size:64" json:"ip"`
	UserAgent   string `gorm:"size:255" json:"userAgent"`
	Status      string `gorm:"size:32;index;not null" json:"status"`
	Message     string `gorm:"size:255" json:"message"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
}

func (LoginLog) TableName() string {
	return "admin_login_logs"
}

type OperationLog struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	RequestID   string `gorm:"size:64;index;not null;default:''" json:"requestId"`
	UserID      uint   `gorm:"index;not null" json:"userId"`
	Username    string `gorm:"size:64;index;not null" json:"username"`
	Action      string `gorm:"size:80;index;not null" json:"action"`
	Resource    string `gorm:"size:120;index" json:"resource"`
	ResourceID  string `gorm:"size:80;index;not null;default:''" json:"resourceId"`
	Method      string `gorm:"size:12" json:"method"`
	Path        string `gorm:"size:180" json:"path"`
	IP          string `gorm:"size:64" json:"ip"`
	UserAgent   string `gorm:"size:255;not null;default:''" json:"userAgent"`
	Status      string `gorm:"size:32;index;not null" json:"status"`
	HTTPStatus  int    `gorm:"not null;default:0" json:"httpStatus"`
	RiskLevel   string `gorm:"size:24;index;not null;default:normal" json:"riskLevel"`
	LatencyMS   int64  `gorm:"not null;default:0" json:"latencyMs"`
	Detail      string `gorm:"type:text" json:"detail"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
}

func (OperationLog) TableName() string {
	return "admin_operation_logs"
}

type Notification struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	Title       string `gorm:"size:120;not null" json:"title"`
	Content     string `gorm:"size:500" json:"content"`
	Level       string `gorm:"size:24;index;not null;default:info" json:"level"`
	ReadAtMS    *int64 `gorm:"column:read_at_ms" json:"readAtMs"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (Notification) TableName() string {
	return "admin_notifications"
}

type DataScope struct {
	ID          uint   `gorm:"primaryKey" json:"id"`
	RoleID      uint   `gorm:"index;not null" json:"roleId"`
	ScopeType   string `gorm:"size:40;index;not null" json:"scopeType"`
	ScopeValue  string `gorm:"size:120;index;not null" json:"scopeValue"`
	CreatedAtMS int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (DataScope) TableName() string {
	return "admin_data_scopes"
}

type AdminJob struct {
	ID            uint   `gorm:"primaryKey" json:"id"`
	Type          string `gorm:"size:80;index;not null" json:"type"`
	Status        string `gorm:"size:32;index;not null" json:"status"`
	PayloadJSON   string `gorm:"type:text" json:"payload"`
	ResultJSON    string `gorm:"type:text" json:"result"`
	Error         string `gorm:"type:text" json:"error"`
	Attempt       int    `gorm:"not null;default:0" json:"attempt"`
	MaxAttempts   int    `gorm:"not null;default:3" json:"maxAttempts"`
	LockedBy      string `gorm:"size:120;index;not null;default:''" json:"lockedBy"`
	LockedUntilMS *int64 `gorm:"column:locked_until_ms;index" json:"lockedUntilMs"`
	ArtifactPath  string `gorm:"size:255;not null;default:''" json:"artifactPath"`
	CreatedBy     uint   `gorm:"index;not null" json:"createdBy"`
	CreatedByName string `gorm:"size:64;index;not null" json:"createdByName"`
	StartedAtMS   *int64 `gorm:"column:started_at_ms" json:"startedAtMs"`
	FinishedAtMS  *int64 `gorm:"column:finished_at_ms" json:"finishedAtMs"`
	CreatedAtMS   int64  `gorm:"column:created_at_ms;autoCreateTime:milli" json:"createdAtMs"`
	UpdatedAtMS   int64  `gorm:"column:updated_at_ms;autoUpdateTime:milli" json:"updatedAtMs"`
}

func (AdminJob) TableName() string {
	return "admin_jobs"
}
