package models

import (
	"time"

	"github.com/google/uuid" // Import uuid
	"gorm.io/gorm"
)

// TrackVersion représente une version d'un track
// MIGRATION UUID: Completée. TrackID est un UUID.
type TrackVersion struct {
	ID            uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id" db:"id"`
	TrackID       uuid.UUID      `gorm:"type:uuid;not null;index:idx_track_versions_track_id;uniqueIndex:idx_track_versions_unique" json:"track_id" db:"track_id"`
	VersionNumber int            `gorm:"not null;uniqueIndex:idx_track_versions_unique" json:"version_number" db:"version_number"`
	FilePath      string         `gorm:"not null;size:500" json:"file_path" db:"file_path"`
	FileSize      int64          `gorm:"not null" json:"file_size" db:"file_size"` // bytes
	Changelog     string         `gorm:"type:text" json:"changelog,omitempty" db:"changelog"`
	CreatedAt     time.Time      `gorm:"autoCreateTime;index:idx_track_versions_created_at" json:"created_at" db:"created_at"`
	UpdatedAt     time.Time      `gorm:"autoUpdateTime" json:"updated_at" db:"updated_at"`
	DeletedAt     gorm.DeletedAt `gorm:"index" json:"-" db:"deleted_at"`

	// Relations
	Track *Track `gorm:"foreignKey:TrackID;constraint:OnDelete:CASCADE" json:"track,omitempty"`
}

// TableName définit le nom de la table pour GORM
func (TrackVersion) TableName() string {
	return "track_versions"
}

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