// models/training_plan.go
package models

import (
	"time"

	"gorm.io/gorm"
)

type TrainingPlan struct {
	ID              uint      `json:"id" gorm:"primaryKey"`
	UserID          uint      `json:"user_id" gorm:"not null;index"`
	Title           string    `json:"title" gorm:"size:255;not null"`
	Description     string    `json:"description" gorm:"type:text"`
	Weeks           int       `json:"weeks" gorm:"not null;default:12"`            // Длительность плана в неделях
	WorkoutsPerWeek int       `json:"workouts_per_week" gorm:"not null;default:3"` // Тренировок в неделю
	TargetDistance  string    `json:"target_distance" gorm:"size:50"`              // Целевая дистанция
	TargetDate      time.Time `json:"target_date"`                                 // Дата цели
	CurrentWeek     int       `json:"current_week" gorm:"default:1"`               // Текущая неделя
	Completed       bool      `json:"completed" gorm:"default:false"`              // Завершен ли план
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`

	// Связи
	User     User              `json:"user,omitempty" gorm:"foreignKey:UserID"`
	Workouts []TrainingWorkout `json:"workouts,omitempty" gorm:"foreignKey:PlanID"`
}

type TrainingWorkout struct {
	ID          uint        `json:"id" gorm:"primaryKey"`
	PlanID      uint        `json:"plan_id" gorm:"not null;index"`
	Week        int         `json:"week" gorm:"not null"` // Неделя плана
	Day         int         `json:"day" gorm:"not null"`  // День недели (1-7)
	Type        WorkoutType `json:"type" gorm:"type:varchar(20);not null"`
	Description string      `json:"description" gorm:"type:text"`
	Distance    float64     `json:"distance_km" gorm:"type:decimal(5,2)"`
	Duration    int         `json:"duration_min"`
	Completed   bool        `json:"completed" gorm:"default:false"`
	CompletedAt *time.Time  `json:"completed_at"`
	CreatedAt   time.Time   `json:"created_at"`
}

// BeforeCreate hooks
func (tp *TrainingPlan) BeforeCreate(tx *gorm.DB) error {
	if tp.CreatedAt.IsZero() {
		tp.CreatedAt = time.Now()
	}
	if tp.UpdatedAt.IsZero() {
		tp.UpdatedAt = time.Now()
	}
	return nil
}

func (tw *TrainingWorkout) BeforeCreate(tx *gorm.DB) error {
	if tw.CreatedAt.IsZero() {
		tw.CreatedAt = time.Now()
	}
	return nil
}

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

// DTO для создания плана тренировок
type TrainingPlanCreateRequest struct {
	Title           string    `json:"title" validate:"required,min=5,max=255"`
	Description     string    `json:"description" validate:"max=1000"`
	Weeks           int       `json:"weeks" validate:"required,min=1,max=52"`
	WorkoutsPerWeek int       `json:"workouts_per_week" validate:"required,min=1,max=7"`
	TargetDistance  string    `json:"target_distance" validate:"max=50"`
	TargetDate      time.Time `json:"target_date"`
}

// DTO для обновления плана тренировок
type TrainingPlanUpdateRequest struct {
	Title           string    `json:"title" validate:"min=5,max=255"`
	Description     string    `json:"description" validate:"max=1000"`
	Weeks           int       `json:"weeks" validate:"min=1,max=52"`
	WorkoutsPerWeek int       `json:"workouts_per_week" validate:"min=1,max=7"`
	TargetDistance  string    `json:"target_distance" validate:"max=50"`
	TargetDate      time.Time `json:"target_date"`
}
