package model

import "time"

// MeetingRecording 会议录制记录，对应 meeting_recordings 表
//
// 生命周期状态：
//   - recording → 录制进行中（media-server ffmpeg 进程在跑，尚未上传）
//   - uploading → media-server 已停止录制，Go 后端正在拉取本地 mp4 上传到 MinIO
//   - ready     → MinIO 上传完成，FileURL 可用
//   - failed    → 录制 / 上传失败，FailureReason 记录原因
//
// RemoteID 是 media-server 内部的 recordingId（hex32），Go 侧 stop / 状态反查时使用。
// 唯一索引落在 RemoteID 上，保证一对一关系。
type MeetingRecording struct {
	ID            int64      `json:"id" gorm:"primaryKey;autoIncrement"`
	RoomID        int64      `json:"room_id" gorm:"not null;index:idx_meeting_recordings_room"`
	RoomCode      string     `json:"room_code" gorm:"size:32;not null;index:idx_meeting_recordings_room_code"`
	StartedBy     int64      `json:"started_by" gorm:"not null;index:idx_meeting_recordings_started_by"`
	Status        string     `json:"status" gorm:"size:16;not null;default:recording"` // recording / uploading / ready / failed
	RemoteID      string     `json:"remote_id" gorm:"size:64;uniqueIndex"`             // media-server 侧 recordingId
	FileURL       string     `json:"file_url" gorm:"size:512"`
	FileObject    string     `json:"file_object" gorm:"size:255"` // MinIO 内的对象 Key，便于鉴权下载/删除
	SizeBytes     int64      `json:"size_bytes" gorm:"default:0"`
	DurationSec   int        `json:"duration_sec" gorm:"default:0"`
	FailureReason string     `json:"failure_reason" gorm:"size:255"`
	StartedAt     time.Time  `json:"started_at" gorm:"not null;type:timestamp(0)"`
	StoppedAt     *time.Time `json:"stopped_at" gorm:"type:timestamp(0)"`
	CreatedAt     time.Time  `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"`
	UpdatedAt     time.Time  `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"`
}

// TableName 指定数据库表名
func (MeetingRecording) TableName() string {
	return "meeting_recordings"
}

// 录制状态常量；与表中 status 字段一一对应
const (
	MeetingRecordingStatusRecording = "recording"
	MeetingRecordingStatusUploading = "uploading"
	MeetingRecordingStatusReady     = "ready"
	MeetingRecordingStatusFailed    = "failed"
)
