package models

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

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

// TrackHistoryAction représente le type d'action effectuée sur un track
type TrackHistoryAction string

const (
	TrackHistoryActionCreated     TrackHistoryAction = "created"
	TrackHistoryActionUpdated     TrackHistoryAction = "updated"
	TrackHistoryActionDeleted     TrackHistoryAction = "deleted"
	TrackHistoryActionPublished   TrackHistoryAction = "published"
	TrackHistoryActionUnpublished TrackHistoryAction = "unpublished"
	TrackHistoryActionRestored    TrackHistoryAction = "restored"
)

// TrackHistory représente l'historique des modifications d'un track
// MIGRATION UUID: Completée. TrackID et UserID sont des UUIDs.
type TrackHistory struct {
	ID        uuid.UUID          `gorm:"type:uuid;primaryKey" json:"id" db:"id"`
	TrackID   uuid.UUID          `gorm:"type:uuid;not null;index:idx_track_history_track_id" json:"track_id" db:"track_id"`
	UserID    uuid.UUID          `gorm:"not null;type:uuid;index:idx_track_history_user_id" json:"user_id" db:"user_id"`
	Action    TrackHistoryAction `gorm:"not null;size:50;index:idx_track_history_action" json:"action" db:"action"`
	OldValue  string             `gorm:"type:text" json:"old_value,omitempty" db:"old_value"`
	NewValue  string             `gorm:"type:text" json:"new_value,omitempty" db:"new_value"`
	CreatedAt time.Time          `gorm:"autoCreateTime;index:idx_track_history_created_at" json:"created_at" db:"created_at"`
	UpdatedAt time.Time          `gorm:"autoUpdateTime" json:"updated_at" db:"updated_at"`

	// Relations
	Track *Track `gorm:"foreignKey:TrackID;constraint:OnDelete:CASCADE" json:"track,omitempty"`
	User  *User  `gorm:"foreignKey:UserID;constraint:OnDelete:SET NULL" json:"user,omitempty"`
}

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

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