//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//

package models

// Org 组织/租户
type Org struct {
	Base
	Name        string  `json:"name" gorm:"size:50;not null"`
	Code        string  `json:"code" gorm:"uniqueIndex;size:30;not null"`
	OwnerID     string  `json:"owner_id" gorm:"not null"`
	ParentID    *string `json:"parent_id" gorm:"index"`
	Path        string  `json:"path" gorm:"size:500;index"`
	Level       int     `json:"level" gorm:"default:0"`
	LeaderID    *string `json:"leader_id"`
	Description string  `json:"description" gorm:"size:200"`
	Logo        string  `json:"logo" gorm:"size:500"`
	Settings    string  `json:"-" gorm:"type:text"` // JSON配置
	Status      int     `json:"status" gorm:"default:1"`
	MaxMembers  int     `json:"max_members" gorm:"default:100"`
}

func (Org) TableName() string {
	return "orgs"
}

// OrgMember 组织成员关系
type OrgMember struct {
	Base
	OrgID      string `json:"org_id" gorm:"type:varchar(36);uniqueIndex:idx_org_user;not null"`
	UserID     string `json:"user_id" gorm:"type:varchar(36);uniqueIndex:idx_org_user;not null"`
	RoleIDs    string `json:"role_ids" gorm:"size:200"` // 逗号分隔
	Position   string `json:"position" gorm:"size:50"`
	Department string `json:"department" gorm:"size:50"`
	JoinedAt   string `json:"joined_at"`
	Status     int    `json:"status" gorm:"default:1"` // 0:待审核 1:正常 2:禁用
}

func (OrgMember) TableName() string {
	return "org_members"
}

// OrgStatus 组织状态
const (
	OrgStatusDisabled = 0
	OrgStatusActive   = 1
)

// MemberStatus 成员状态
const (
	MemberStatusPending  = 0
	MemberStatusActive   = 1
	MemberStatusDisabled = 2
)

// IsOwner 检查用户是否是组织所有者
func (o *Org) IsOwner(userID string) bool {
	return o.OwnerID == userID
}

// IsMemberActive 成员是否有效
func (m *OrgMember) IsMemberActive() bool {
	return m.Status == MemberStatusActive
}
