package models

import (
	"time"

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

// TrackComment représente un commentaire sur un track
// MIGRATION UUID: Completée. ID, TrackID, UserID et ParentID sont des UUIDs.
type TrackComment struct {
	ID        uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id" db:"id"`
	TrackID   uuid.UUID      `gorm:"type:uuid;not null;index:idx_track_comments_track_id" json:"track_id" db:"track_id"`
	UserID    uuid.UUID      `gorm:"not null;type:uuid;index:idx_track_comments_user_id" json:"user_id" db:"user_id"`
	ParentID  *uuid.UUID     `gorm:"type:uuid;index:idx_track_comments_parent_id" json:"parent_id,omitempty" db:"parent_id"`
	Content   string         `gorm:"type:text;not null" json:"content" db:"content"`
	Timestamp float64        `gorm:"default:0" json:"timestamp,omitempty" db:"timestamp"` // Position in seconds
	IsEdited  bool           `gorm:"default:false" json:"is_edited" db:"is_edited"`
	CreatedAt time.Time      `gorm:"autoCreateTime;index:idx_track_comments_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:"-"`
	User    User           `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"user"`
	Parent  *TrackComment  `gorm:"foreignKey:ParentID;constraint:OnDelete:CASCADE" json:"-"`
	Replies []TrackComment `gorm:"foreignKey:ParentID;constraint:OnDelete:CASCADE" json:"replies,omitempty"`
}

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

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