package models

import (
	"time"

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

// BitrateAdaptationReason représente la raison de l'adaptation de bitrate
// T0346: Create Bitrate Adaptation Database Model
type BitrateAdaptationReason string

const (
	BitrateReasonNetworkSlow  BitrateAdaptationReason = "network_slow"
	BitrateReasonNetworkFast  BitrateAdaptationReason = "network_fast"
	BitrateReasonUserSelected BitrateAdaptationReason = "user_selected"
	BitrateReasonBufferLow    BitrateAdaptationReason = "buffer_low"
)

// BitrateAdaptationLog représente un log d'adaptation de bitrate
// T0346: Create Bitrate Adaptation Database Model
// MIGRATION UUID: UserID et TrackID migrés vers uuid.UUID
type BitrateAdaptationLog struct {
	ID               uuid.UUID               `gorm:"type:uuid;primaryKey" json:"id"`
	TrackID          uuid.UUID               `gorm:"type:uuid;not null;index:idx_bitrate_adaptation_track_id" json:"track_id"`
	Track            Track                   `gorm:"foreignKey:TrackID;constraint:OnDelete:CASCADE" json:"track,omitempty"`
	UserID           uuid.UUID               `gorm:"type:uuid;not null;index:idx_bitrate_adaptation_user_id" json:"user_id"`
	User             User                    `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"user,omitempty"`
	OldBitrate       int                     `gorm:"not null" json:"old_bitrate"`
	NewBitrate       int                     `gorm:"not null" json:"new_bitrate"`
	Reason           BitrateAdaptationReason `gorm:"type:varchar(50);not null" json:"reason"`
	NetworkBandwidth *int                    `gorm:"type:integer" json:"network_bandwidth,omitempty"`
	CreatedAt        time.Time               `gorm:"autoCreateTime;index:idx_bitrate_adaptation_created_at" json:"created_at"`
	UpdatedAt        time.Time               `gorm:"autoUpdateTime" json:"updated_at"`
}

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

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