package entity

import (
	"encoding/json"
	"time"
)

// Project 项目主表实体
type Project struct {
	ID        int64  `gorm:"primaryKey;autoIncrement" json:"id"`
	ProjectID string `gorm:"type:varchar(64);uniqueIndex;not null" json:"project_id"`
	Name      string `gorm:"type:varchar(128);not null" json:"name"`
	Namespace string `gorm:"type:varchar(64);uniqueIndex;not null" json:"namespace"`

	// 生命周期状态: INIT/DRAFTING/REVIEWING/RELEASED/MODIFYING/ARCHIVED
	LifecycleStatus string `gorm:"type:varchar(32);default:'INIT'" json:"lifecycle_status"`
	// 认证状态: draft/pending/official
	CertificationStatus string `gorm:"type:varchar(32);default:'draft'" json:"certification_status"`

	// 当前正式版本号
	CurrentVersion int `gorm:"default:0" json:"current_version"`

	// JSONB 存储
	BasicInfo        json.RawMessage `gorm:"type:jsonb" json:"basic_info"`
	DeployBusiness   json.RawMessage `gorm:"type:jsonb" json:"deploy_business"`
	DeployEnv        json.RawMessage `gorm:"type:jsonb" json:"deploy_env"`
	DeployMiddleware json.RawMessage `gorm:"type:jsonb" json:"deploy_middleware"`

	// 填写人
	DetailFillerID   int64  `json:"detail_filler_id"`
	DetailFillerName string `gorm:"type:varchar(64)" json:"detail_filler_name"`

	// 审计字段
	CreatedBy     int64      `json:"created_by"`
	CreatedByName string     `gorm:"type:varchar(64)" json:"created_by_name"`
	CreatedAt     time.Time  `json:"created_at"`
	UpdatedAt     time.Time  `json:"updated_at"`
	DeletedAt     *time.Time `gorm:"index" json:"deleted_at,omitempty"`
}

// TableName 指定表名
func (Project) TableName() string {
	return "projects"
}

// 生命周期状态常量
const (
	LifecycleInit      = "INIT"
	LifecycleDrafting  = "DRAFTING"
	LifecycleReviewing = "REVIEWING"
	LifecycleReleased  = "RELEASED"
	LifecycleModifying = "MODIFYING"
	LifecycleArchived  = "ARCHIVED"
)

// 认证状态常量
const (
	CertificationDraft    = "draft"
	CertificationPending  = "pending"
	CertificationOfficial = "official"
)

// ProjectVersion 项目版本表 (含草稿)
type ProjectVersion struct {
	ID        int64  `gorm:"primaryKey;autoIncrement" json:"id"`
	ProjectID string `gorm:"type:varchar(64);index;not null" json:"project_id"`

	// 版本号 (正式版本递增, 草稿为0)
	Version int `gorm:"not null;default:0" json:"version"`

	// 版本类型: official/fill_draft/modify_draft
	VersionType string `gorm:"type:varchar(32);not null" json:"version_type"`

	// 基准版本号（草稿基于哪个正式版本创建）
	BaseVersion int `gorm:"default:0" json:"base_version"`

	// 草稿所属用户ID (仅草稿类型有值)
	UserID   int64  `gorm:"index" json:"user_id"`
	UserName string `gorm:"type:varchar(64)" json:"user_name"`

	// 关联工单ID (1:1关系, 仅草稿类型有值)
	WorkflowID string `gorm:"type:varchar(64);index" json:"workflow_id"`

	// 完整快照数据
	SnapshotData json.RawMessage `gorm:"type:jsonb" json:"snapshot_data"`

	// 变更信息
	CommitMessage string `gorm:"type:varchar(255)" json:"commit_message"`
	CommitterID   int64  `json:"committer_id"`
	CommitterName string `gorm:"type:varchar(64)" json:"committer_name"`

	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// TableName 指定表名
func (ProjectVersion) TableName() string {
	return "project_versions"
}

// 版本类型常量
const (
	VersionTypeOfficial    = "official"
	VersionTypeFillDraft   = "fill_draft"
	VersionTypeModifyDraft = "modify_draft"
)

// ProjectAuthConfig 项目授权配置
type ProjectAuthConfig struct {
	ID        int64  `gorm:"primaryKey;autoIncrement" json:"id"`
	ProjectID string `gorm:"type:varchar(64);uniqueIndex;not null" json:"project_id"`

	TierOneSecret string `gorm:"type:varchar(128)" json:"tier_one_secret"` // 一级TOTP密钥
	TimeOffset    int    `gorm:"default:30" json:"time_offset"`            // 允许时间偏移(秒)
	TOTPEnabled   bool   `gorm:"default:false" json:"totp_enabled"`

	TierTwoSecret string `gorm:"type:varchar(128)" json:"tier_two_secret"` // 二级TOTP密钥

	AuthType     string     `gorm:"type:varchar(32)" json:"auth_type"` // permanent/time_limited
	AuthDays     int        `json:"auth_days"`                         // 授权有效期(天)
	AuthorizedAt *time.Time `json:"authorized_at"`
	RevokedAt    *time.Time `json:"revoked_at"`
	IsOffline    bool       `gorm:"default:false" json:"is_offline"`

	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// TableName 指定表名
func (ProjectAuthConfig) TableName() string {
	return "project_auth_configs"
}
