package user

import "tm/pkg/response"

// User Registration DTOs

// CreateUserForm represents the data required to create a new user account
type CreateUserForm struct {
	FullName     string  `json:"full_name" valid:"required,length(2|100)" example:"Admin User"`              // Full name of the user
	Username     string  `json:"username" valid:"required,alphanum,length(3|30)" example:"admin"`            // Unique username (alphanumeric only)
	Email        string  `json:"email" valid:"required,email" example:"admin@opplens.com"`                   // Valid email address
	Password     string  `json:"password" valid:"required,length(8|128)" example:"Admin!1234"`               // Password (minimum 8 characters)
	Role         string  `json:"role" valid:"required,in(admin|manager|operator|viewer)" example:"admin"`    // User role (admin, manager, operator, viewer)
	Department   *string `json:"department" valid:"optional,length(2|100)" example:"Information Technology"` // Optional department name
	Position     *string `json:"position" valid:"optional,length(2|100)" example:"Lead"`                     // Optional job position
	Phone        *string `json:"phone" valid:"optional,length(10|20)" example:"+18289784438"`                // Optional phone number
	ProfileImage *string `json:"profile_image" valid:"optional,url" example:""`                              // Optional profile image URL
}

// UpdateUserForm represents the data for updating an existing user account
type UpdateUserForm struct {
	FullName     *string `json:"full_name" valid:"optional,length(2|100)" example:"Admin User"`              // Updated full name
	Username     *string `json:"username" valid:"optional,alphanum,length(3|30)" example:"admin"`            // Updated username
	Email        *string `json:"email" valid:"optional,email" example:"admin@opplens.com"`                   // Updated email address
	Role         *string `json:"role" valid:"optional,in(admin|manager|operator|viewer)" example:"admin"`    // Updated user role
	Department   *string `json:"department" valid:"optional,length(2|100)" example:"Information Technology"` // Updated department
	Position     *string `json:"position" valid:"optional,length(2|100)" example:"Lead"`                     // Updated position
	Phone        *string `json:"phone" valid:"optional,length(10|20)" example:"+18289784438"`                // Updated phone number
	ProfileImage *string `json:"profile_image" valid:"optional,url" example:""`                              // Updated profile image URL
}

// User Authentication DTOs

// LoginForm represents the credentials required for user authentication
type LoginForm struct {
	Username string `json:"username" valid:"required" example:"admin"`      // Username or email address
	Password string `json:"password" valid:"required" example:"Admin!1234"` // User password
}

// RefreshTokenForm represents the refresh token required to generate new access tokens
type RefreshTokenForm struct {
	RefreshToken string `json:"refresh_token" valid:"required" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."` // Valid refresh token
}

// ChangePasswordForm represents the data required to change user password
type ChangePasswordForm struct {
	OldPassword string `json:"old_password" valid:"required" example:"Admin!1234"`                  // Current password for verification
	NewPassword string `json:"new_password" valid:"required,length(8|128)" example:"NewAdmin!1234"` // New password (minimum 8 characters)
}

// UpdateProfileForm represents the data for updating user profile information
type UpdateProfileForm struct {
	FullName     *string `json:"full_name,omitempty" valid:"optional,length(2|100)" example:"Admin User"`              // Updated full name
	Department   *string `json:"department,omitempty" valid:"optional,length(2|100)" example:"Information Technology"` // Updated department
	Position     *string `json:"position,omitempty" valid:"optional,length(2|100)" example:"Lead"`                     // Updated position
	Phone        *string `json:"phone,omitempty" valid:"optional,length(10|20)" example:"+18289784438"`                // Updated phone number
	ProfileImage *string `json:"profile_image,omitempty" valid:"optional,url" example:""`                              // Updated profile image URL
}

// ResetPasswordForm represents the email address for password reset
type ResetPasswordForm struct {
	Email string `json:"email" valid:"required,email" example:"admin@opplens.com"` // Email address for password reset
}

// Admin DTOs
type ListUsersForm struct {
	Search    *string `query:"search" valid:"optional"`
	Status    *string `query:"status" valid:"optional,in(active|inactive|suspended)"`
	Role      *string `query:"role" valid:"optional,in(admin|manager|operator|viewer)"`
	CompanyID *string `query:"company_id" valid:"optional"`
	Limit     *int    `query:"limit" valid:"optional,range(1|100)"`
	Offset    *int    `query:"offset" valid:"optional,min(0)"`
	SortBy    *string `query:"sort_by" valid:"optional,in(full_name|email|username|role|created_at|last_login_at)"`
	SortOrder *string `query:"sort_order" valid:"optional,in(asc|desc)"`
}

type UpdateUserStatusForm struct {
	Status string `json:"status" valid:"required,in(active|inactive|suspended)"`
}

type UpdateUserRoleForm struct {
	Role string `json:"role" valid:"required,in(admin|manager|operator|viewer)"`
}

// Response DTOs
type UserResponse struct {
	ID           string  `json:"id"`
	FullName     string  `json:"full_name"`
	Username     string  `json:"username"`
	Email        string  `json:"email"`
	Role         string  `json:"role"`
	Status       string  `json:"status"`
	Department   *string `json:"department"`
	Position     *string `json:"position"`
	Phone        *string `json:"phone"`
	ProfileImage *string `json:"profile_image"`
	IsVerified   bool    `json:"is_verified"`
	LastLoginAt  *int64  `json:"last_login_at"`
	UpdatedAt    int64   `json:"updated_at"`
	CreatedAt    int64   `json:"created_at"`
}

type AuthResponse struct {
	User         *UserResponse `json:"user"`
	AccessToken  string        `json:"access_token"`
	RefreshToken string        `json:"refresh_token"`
	ExpiresAt    int64         `json:"expires_at"`
}

type UserListResponse struct {
	Users []*UserResponse `json:"users"`
	Meta  *response.Meta  `json:"meta"`
}

type SearchUsersForm struct {
	Search *string `query:"search" valid:"optional"`
	Status *string `query:"status" valid:"optional,in(active|inactive|suspended)"`
	Role   *string `query:"role" valid:"optional,in(admin|manager|operator|viewer)"`
}

// Helper function to convert User to UserResponse
func (u *User) ToResponse() *UserResponse {
	return &UserResponse{
		ID:           u.ID.Hex(),
		FullName:     u.FullName,
		Username:     u.Username,
		Email:        u.Email,
		Role:         string(u.Role),
		Status:       string(u.Status),
		Department:   u.Department,
		Position:     u.Position,
		Phone:        u.Phone,
		ProfileImage: u.ProfileImage,
		IsVerified:   u.IsVerified,
		LastLoginAt:  u.LastLoginAt,
		UpdatedAt:    u.UpdatedAt,
		CreatedAt:    u.CreatedAt,
	}
}
