package models

import (
	"time"

	"gorm.io/gorm"
)

// TrackShare représente un lien de partage pour un track
type TrackShare struct {
	ID          int64          `gorm:"primaryKey;autoIncrement" json:"id" db:"id"`
	TrackID     int64          `gorm:"not null;index:idx_track_shares_track_id" json:"track_id" db:"track_id"`
	UserID      int64          `gorm:"not null;index:idx_track_shares_user_id" json:"user_id" db:"user_id"`
	ShareToken  string         `gorm:"uniqueIndex;not null;size:255" json:"share_token" db:"share_token"`
	Permissions string         `gorm:"type:varchar(50);default:'read'" json:"permissions" db:"permissions"` // "read", "download", "read,download"
	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
	Track *Track `gorm:"foreignKey:TrackID;constraint:OnDelete:CASCADE" json:"track,omitempty"`
	User  *User  `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"user,omitempty"`
}

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

