// models/achievement.go
package models

import (
	"time"
	
	"gorm.io/gorm"
)

type AchievementType string

const (
	AchievementTypeDistance  AchievementType = "distance"
	AchievementTypeSpeed     AchievementType = "speed"
	AchievementTypeConsistency AchievementType = "consistency"
	AchievementTypeEvent     AchievementType = "event"
	AchievementTypeSpecial   AchievementType = "special"
)

type Achievement struct {
	ID          uint            `json:"id" gorm:"primaryKey"`
	UserID      uint            `json:"user_id" gorm:"not null;index"`
	Type        AchievementType `json:"type" gorm:"type:varchar(20);not null"`
	Title       string          `json:"title" gorm:"size:255;not null"`
	Description string          `json:"description" gorm:"type:text"`
	Result      string          `json:"result" gorm:"size:100"`  // Достигнутый результат
	Distance    string          `json:"distance" gorm:"size:50"` // Дистанция достижения
	Date        time.Time       `json:"date" gorm:"not null"`
	Verified    bool            `json:"verified" gorm:"default:false"`
	BadgeImage  string          `json:"badge_image" gorm:"size:500"` // Изображение бейджа
	CreatedAt   time.Time       `json:"created_at"`
	UpdatedAt   time.Time       `json:"updated_at"`

	// Связи
	User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
}

// BeforeCreate hook
func (a *Achievement) BeforeCreate(tx *gorm.DB) error {
	if a.CreatedAt.IsZero() {
		a.CreatedAt = time.Now()
	}
	if a.UpdatedAt.IsZero() {
		a.UpdatedAt = time.Now()
	}
	return nil
}

// BeforeUpdate hook
func (a *Achievement) BeforeUpdate(tx *gorm.DB) error {
	a.UpdatedAt = time.Now()
	return nil
}

// DTO для создания достижения
type AchievementCreateRequest struct {
	Type        AchievementType `json:"type" validate:"required,oneof=distance speed consistency event special"`
	Title       string          `json:"title" validate:"required,min=5,max=255"`
	Description string          `json:"description" validate:"max=1000"`
	Result      string          `json:"result" validate:"max=100"`
	Distance    string          `json:"distance" validate:"max=50"`
	Date        time.Time       `json:"date" validate:"required"`
	BadgeImage  string          `json:"badge_image" validate:"max=500"`
}

// DTO для ответа с достижениями пользователя
type UserAchievementsResponse struct {
	TotalAchievements int          `json:"total_achievements"`
	Completed         int          `json:"completed"`
	ProgressPercent   float64      `json:"progress_percent"`
	Achievements      []Achievement `json:"achievements"`
}