package domain

import (
	"errors"
	"time"
)

type AgeGroup string

const (
	AgeUnder13 AgeGroup = "UNDER_13"
	Age13To17  AgeGroup = "13_17"
	Age18To24  AgeGroup = "18_24"
	Age25To34  AgeGroup = "25_34"
	Age35To44  AgeGroup = "35_44"
	Age45To54  AgeGroup = "45_54"
	Age55Plus  AgeGroup = "55_PLUS"
)

var (
	ErrUserNotVerified        = errors.New("user not verified")
	ErrUserNotFound           = errors.New("user not found")
	ErrEmailAlreadyRegistered = errors.New("email is already registered")
	ErrPhoneAlreadyRegistered = errors.New("phone number is already registered")
)

/*
UserStatus reflects the lifecycle state of a user account.
Matches DB column: users.status
*/
type UserStatus string

const (
	UserStatusPending     UserStatus = "PENDING"
	UserStatusActive      UserStatus = "ACTIVE"
	UserStatusSuspended   UserStatus = "SUSPENDED"
	UserStatusDeactivated UserStatus = "DEACTIVATED"
)

type UpdateKnowledgeLevelReq struct {
	UserID         int64  `json:"user_id"`
	KnowledgeLevel string `json:"knowledge_level"` // BEGINNER, INTERMEDIATE, ADVANCED
}

type User struct {
	ID        int64
	FirstName string
	LastName  string
	Gender    string
	BirthDay  time.Time `json:"birth_day"`
	// UserName    string
	Email       string
	PhoneNumber string
	Password    []byte
	Role        Role

	AgeGroup       string
	EducationLevel string
	Country        string
	Region         string

	// Profile fields
	KnowledgeLevel             string
	InitialAssessmentCompleted bool
	NickName                   string
	Occupation                 string
	LearningGoal               string
	LanguageGoal               string
	LanguageChallange          string
	FavouriteTopic             string

	EmailVerified bool
	PhoneVerified bool
	Status        UserStatus

	LastLogin                   *time.Time
	ProfileCompleted            bool
	ProfileCompletionPercentage int
	ProfilePictureURL           string
	PreferredLanguage           string

	CreatedAt time.Time
	UpdatedAt *time.Time
}

type UserProfileResponse struct {
	ID        int64  `json:"id"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Gender    string `json:"gender"`
	BirthDay  string `json:"birth_day,omitempty"` // formatted as YYYY-MM-DD
	// UserName    string `json:"user_name,omitempty"`
	Email       string `json:"email,omitempty"`
	PhoneNumber string `json:"phone_number,omitempty"`
	Role        Role   `json:"role"`

	AgeGroup       string `json:"age_group,omitempty"`
	EducationLevel string `json:"education_level,omitempty"`
	Country        string `json:"country,omitempty"`
	Region         string `json:"region,omitempty"`

	// Profile fields
	InitialAssessmentCompleted bool   `json:"initial_assessment_completed,omitempty"`
	NickName                   string `json:"nick_name,omitempty"`
	Occupation                 string `json:"occupation,omitempty"`
	LearningGoal               string `json:"learning_goal,omitempty"`
	LanguageGoal               string `json:"language_goal,omitempty"`
	LanguageChallange          string `json:"language_challange,omitempty"`
	FavouriteTopic             string `json:"favoutite_topic,omitempty"`

	EmailVerified bool       `json:"email_verified"`
	PhoneVerified bool       `json:"phone_verified"`
	Status        UserStatus `json:"status"`

	LastLogin                   *time.Time `json:"last_login,omitempty"`
	ProfileCompleted            bool       `json:"profile_completed"`
	ProfileCompletionPercentage int        `json:"profile_completion_percentage"`
	ProfilePictureURL           string     `json:"profile_picture_url"`
	PreferredLanguage           string     `json:"preferred_language,omitempty"`

	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt *time.Time `json:"updated_at,omitempty"`
}

type UserFilter struct {
	Role string

	Page          int64
	PageSize      int64
	Query         string
	CreatedBefore ValidTime
	CreatedAfter  ValidTime
}

type RegisterUserReq struct {
	Email       string    `json:"email"`
	PhoneNumber string    `json:"phone_number"`
	Password    string    `json:"password"`
	Role        string    `json:"role"`
	OtpMedium   OtpMedium `json:"otp_medium"`
}

type CreateUserReq struct {
	FirstName   string
	LastName    string
	Gender      string    `json:"gender"`
	BirthDay    time.Time `json:"birth_day"`
	Email       string
	PhoneNumber string
	Password    string
	Role        string

	Status UserStatus

	AgeGroup       string
	EducationLevel string
	Country        string
	Region         string

	// Profile fields
	NickName          string
	Occupation        string
	LearningGoal      string
	LanguageGoal      string
	LanguageChallange string
	FavouriteTopic    string

	PreferredLanguage string
}

type ResetPasswordReq struct {
	Email       string
	PhoneNumber string
	Password    string
	OtpCode     string
}

type UpdateUserStatusReq struct {
	Status string
	UserID int64
}

type UpdateUserReq struct {
	UserID int64 `json:"-"`

	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`

	Gender   string  `json:"gender"`
	BirthDay *string `json:"birth_day"` // YYYY-MM-DD

	AgeGroup *AgeGroup `json:"age_group"`

	EducationLevel string `json:"education_level"`
	Country        string `json:"country"`
	Region         string `json:"region"`

	KnowledgeLevel    string `json:"knowledge_level"`
	NickName          string `json:"nick_name"`
	Occupation        string `json:"occupation"`
	LearningGoal      string `json:"learning_goal"`
	LanguageGoal      string `json:"language_goal"`
	LanguageChallange string `json:"language_challange"`
	FavouriteTopic    string `json:"favourite_topic"`

	InitialAssessmentCompleted bool `json:"initial_assessment_completed"`

	ProfilePictureURL string `json:"profile_picture_url"`
	PreferredLanguage string `json:"preferred_language"`
}
