package models

import (
	"time"

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

// PlaylistFollow représente un follow d'un utilisateur sur une playlist
// T0489: Create Playlist Follow Feature
// MIGRATION UUID: Completée. ID et PlaylistID sont des UUIDs.
type PlaylistFollow struct {
	ID         uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id" db:"id"`
	PlaylistID uuid.UUID      `gorm:"type:uuid;not null;index:idx_playlist_follows_playlist_id" json:"playlist_id" db:"playlist_id"`
	UserID     uuid.UUID      `gorm:"type:uuid;not null;index:idx_playlist_follows_user_id" json:"user_id" db:"user_id"`
	CreatedAt  time.Time      `gorm:"autoCreateTime" 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
	Playlist Playlist `gorm:"foreignKey:PlaylistID;constraint:OnDelete:CASCADE" json:"-"`
	User     User     `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"user,omitempty"`
}

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

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