package models

import (
	"github.com/google/uuid"
	"time"

	"gorm.io/gorm"
)

// PlaylistShareLink représente un lien de partage public pour une playlist
// T0488: Create Playlist Public Share Link
// MIGRATION UUID: Completée. ID et PlaylistID sont des UUIDs.
type PlaylistShareLink struct {
	ID          uuid.UUID      `gorm:"type:uuid;primaryKey" json:"id" db:"id"`
	PlaylistID  uuid.UUID      `gorm:"type:uuid;not null;index:idx_playlist_share_links_playlist_id" json:"playlist_id" db:"playlist_id"`
	UserID      uuid.UUID      `gorm:"type:uuid;not null;index:idx_playlist_share_links_user_id" json:"user_id" db:"user_id"`
	ShareToken  string         `gorm:"uniqueIndex;not null;size:255" json:"share_token" db:"share_token"`
	ExpiresAt   *time.Time     `json:"expires_at,omitempty" db:"expires_at"`
	AccessCount int64          `gorm:"default:0" json:"access_count" db:"access_count"`
	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:"-"`
}

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

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