package user

import (
	"database/sql"
	"time"

	"github.com/google/uuid"
)

// User represents a user with password (for auth)
type User struct {
	ID          uuid.UUID      `db:"id" json:"id"`
	Username    string         `db:"username" json:"username"`
	Email       string         `db:"email" json:"email"`
	Password    string         `db:"password_hash" json:"-"` // Never serialize password
	FirstName   sql.NullString `db:"first_name" json:"first_name,omitempty"`
	LastName    sql.NullString `db:"last_name" json:"last_name,omitempty"`
	Bio         sql.NullString `db:"bio" json:"bio,omitempty"`
	Avatar      sql.NullString `db:"avatar" json:"avatar,omitempty"`
	Role        string         `db:"role" json:"role"`
	IsActive    bool           `db:"is_active" json:"is_active"`
	IsVerified  bool           `db:"is_verified" json:"is_verified"`
	LastLoginAt sql.NullTime   `db:"last_login_at" json:"last_login_at,omitempty"`
	CreatedAt   time.Time      `db:"created_at" json:"created_at"`
	UpdatedAt   time.Time      `db:"updated_at" json:"updated_at"`
}

// UserResponse represents user data without sensitive information
type UserResponse struct {
	ID          uuid.UUID      `json:"id"`
	Username    string         `json:"username"`
	Email       string         `json:"email"`
	FirstName   sql.NullString `json:"first_name,omitempty"`
	LastName    sql.NullString `json:"last_name,omitempty"`
	Bio         sql.NullString `json:"bio,omitempty"`
	Avatar      sql.NullString `json:"avatar,omitempty"`
	Role        string         `json:"role"`
	IsActive    bool           `json:"is_active"`
	IsVerified  bool           `json:"is_verified"`
	LastLoginAt sql.NullTime   `json:"last_login_at,omitempty"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
}

// CreateUserRequest represents a request to create a new user
type CreateUserRequest struct {
	Username  string `json:"username" binding:"required,min=3,max=50"`
	Email     string `json:"email" binding:"required,email"`
	Password  string `json:"password" binding:"required,min=8"`
	FirstName string `json:"first_name,omitempty"`
	LastName  string `json:"last_name,omitempty"`
	Role      string `json:"role,omitempty"`
}

// UpdateUserRequest represents a request to update user data
type UpdateUserRequest struct {
	Username   *string `json:"username,omitempty"`
	Email      *string `json:"email,omitempty"`
	FirstName  *string `json:"first_name,omitempty"`
	LastName   *string `json:"last_name,omitempty"`
	Bio        *string `json:"bio,omitempty"`
	Avatar     *string `json:"avatar,omitempty"`
	IsActive   *bool   `json:"is_active,omitempty"`
	IsVerified *bool   `json:"is_verified,omitempty"`
	Role       *string `json:"role,omitempty"`
}

// UserPreferencesRequest représente une requête de mise à jour des préférences
type UserPreferencesRequest struct {
	Theme         *string               `json:"theme,omitempty"`
	Language      *string               `json:"language,omitempty"`
	Timezone      *string               `json:"timezone,omitempty"`
	Notifications *NotificationSettings `json:"notifications,omitempty"`
	Privacy       *PrivacySettings      `json:"privacy,omitempty"`
	Audio         *AudioSettings        `json:"audio,omitempty"`
}

// UserPreferencesResponse représente les préférences utilisateur
type UserPreferencesResponse struct {
	UserID        uuid.UUID            `json:"user_id"`
	Theme         string               `json:"theme"`
	Language      string               `json:"language"`
	Timezone      string               `json:"timezone"`
	Notifications NotificationSettings `json:"notifications"`
	Privacy       PrivacySettings      `json:"privacy"`
	Audio         AudioSettings        `json:"audio"`
	UpdatedAt     time.Time            `json:"updated_at"`
}

// NotificationSettings paramètres de notification
type NotificationSettings struct {
	Email          bool `json:"email"`
	Push           bool `json:"push"`
	Desktop        bool `json:"desktop"`
	NewFollowers   bool `json:"new_followers"`
	TrackComments  bool `json:"track_comments"`
	DirectMessages bool `json:"direct_messages"`
	Mentions       bool `json:"mentions"`
	Likes          bool `json:"likes"`
}

// PrivacySettings paramètres de confidentialité
type PrivacySettings struct {
	ShowEmail         bool   `json:"show_email"`
	ShowActivity      bool   `json:"show_activity"`
	AllowDM           bool   `json:"allow_dm"`
	TrackVisibility   string `json:"track_visibility"`   // public, followers, private
	ProfileVisibility string `json:"profile_visibility"` // public, registered, private
}

// AudioSettings paramètres audio
type AudioSettings struct {
	AutoPlay  bool    `json:"auto_play"`
	Quality   string  `json:"quality"`   // low, medium, high, lossless
	Volume    float64 `json:"volume"`    // 0-1
	Crossfade int     `json:"crossfade"` // secondes
}

// AccountStatus statut du compte
type AccountStatus struct {
	UserID           uuid.UUID  `json:"user_id"`
	Status           string     `json:"status"` // active, suspended, deleted, pending_deletion
	IsActive         bool       `json:"is_active"`
	IsVerified       bool       `json:"is_verified"`
	CreatedAt        time.Time  `json:"created_at"`
	DeletedAt        *time.Time `json:"deleted_at,omitempty"`
	DeletionReason   string     `json:"deletion_reason,omitempty"`
	RecoveryDeadline *time.Time `json:"recovery_deadline,omitempty"`
}

// UserDataExport export des données utilisateur (RGPD)
type UserDataExport struct {
	UserID       uuid.UUID               `json:"user_id"`
	Profile      UserResponse            `json:"profile"`
	Preferences  UserPreferencesResponse `json:"preferences"`
	Activity     []UserActivity          `json:"activity"`
	Content      []UserContent           `json:"content"`
	Interactions []UserInteraction       `json:"interactions"`
	ExportedAt   time.Time               `json:"exported_at"`
}

// UserActivity activité utilisateur
type UserActivity struct {
	ID        uuid.UUID `json:"id"`
	Type      string    `json:"type"`
	Details   string    `json:"details"`
	IPAddress string    `json:"ip_address"`
	UserAgent string    `json:"user_agent"`
	CreatedAt time.Time `json:"created_at"`
}

// UserContent contenu utilisateur
type UserContent struct {
	ID        uuid.UUID `json:"id"`
	Type      string    `json:"type"`
	Title     string    `json:"title"`
	URL       string    `json:"url"`
	CreatedAt time.Time `json:"created_at"`
}

// UserInteraction interaction utilisateur
type UserInteraction struct {
	ID        uuid.UUID `json:"id"`
	Type      string    `json:"type"`
	TargetID  uuid.UUID `json:"target_id"`
	CreatedAt time.Time `json:"created_at"`
}
