package models

import (
	"gorm.io/gorm"
	"time"

	"github.com/google/uuid"
)

// QueueStatus représente le statut d'un job dans la queue
type QueueStatus string

const (
	QueueStatusPending    QueueStatus = "pending"
	QueueStatusProcessing QueueStatus = "processing"
	QueueStatusCompleted  QueueStatus = "completed"
	QueueStatusFailed     QueueStatus = "failed"
)

// HLSTranscodeQueue représente un job de transcodage HLS dans la queue
// MIGRATION UUID: Completée. TrackID est un UUID.
type HLSTranscodeQueue struct {
	ID           uuid.UUID   `gorm:"type:uuid;primaryKey" json:"id"`
	TrackID      uuid.UUID   `gorm:"type:uuid;not null;index" json:"track_id"`
	Track        Track       `gorm:"foreignKey:TrackID" json:"track,omitempty"`
	Priority     int         `gorm:"not null;default:5" json:"priority"`
	Status       QueueStatus `gorm:"type:varchar(20);not null;default:'pending';index" json:"status"`
	RetryCount   int         `gorm:"not null;default:0" json:"retry_count"`
	MaxRetries   int         `gorm:"not null;default:3" json:"max_retries"`
	ErrorMessage *string     `gorm:"type:text" json:"error_message,omitempty"`
	CreatedAt    time.Time   `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt    time.Time   `gorm:"autoUpdateTime" json:"updated_at"`
	StartedAt    *time.Time  `json:"started_at,omitempty"`
	CompletedAt  *time.Time  `json:"completed_at,omitempty"`
}

func (HLSTranscodeQueue) TableName() string {
	return "hls_transcode_queue"
}

// BeforeCreate hook GORM pour générer UUID si non défini
func (m *HLSTranscodeQueue) BeforeCreate(tx *gorm.DB) error {
	if m.ID == uuid.Nil {
		m.ID = uuid.New()
	}
	return nil
}
