package contract

import (
	"time"

	"github.com/google/uuid"
)

type CreateUserRequest struct {
	OrganizationID uuid.UUID              `json:"organization_id" validate:"required"`
	OutletID       *uuid.UUID             `json:"outlet_id,omitempty"`
	Name           string                 `json:"name" validate:"required,min=1,max=255"`
	Email          string                 `json:"email" validate:"required,email"`
	Password       string                 `json:"password" validate:"required,min=6"`
	Role           string                 `json:"role" validate:"required,oneof=admin manager cashier waiter"`
	Permissions    map[string]interface{} `json:"permissions,omitempty"`
}

type UpdateUserRequest struct {
	Name        *string                 `json:"name,omitempty" validate:"omitempty,min=1,max=255"`
	Email       *string                 `json:"email,omitempty" validate:"omitempty,email"`
	Role        *string                 `json:"role,omitempty" validate:"omitempty,oneof=admin manager cashier waiter"`
	IsActive    *bool                   `json:"is_active,omitempty"`
	Permissions *map[string]interface{} `json:"permissions,omitempty"`
}

type ChangePasswordRequest struct {
	CurrentPassword string `json:"current_password" validate:"required"`
	NewPassword     string `json:"new_password" validate:"required,min=6"`
}

type UpdateUserOutletRequest struct {
	OutletID uuid.UUID `json:"outlet_id" validate:"required"`
}

type LoginRequest struct {
	Email    string `json:"email" validate:"required,email"`
	Password string `json:"password" validate:"required"`
}

type LoginResponse struct {
	Token       string               `json:"token"`
	ExpiresAt   time.Time            `json:"expires_at"`
	User        UserResponse         `json:"user"`
	Roles       []RoleResponse       `json:"roles"`
	Permissions []string             `json:"permissions"`
	Departments []DepartmentResponse `json:"departments"`
}

type UserResponse struct {
	ID                 uuid.UUID            `json:"id"`
	Name               string               `json:"name"`
	Email              string               `json:"email"`
	IsActive           bool                 `json:"is_active"`
	CreatedAt          time.Time            `json:"created_at"`
	UpdatedAt          time.Time            `json:"updated_at"`
	Roles              []RoleResponse       `json:"roles,omitempty"`
	DepartmentResponse []DepartmentResponse `json:"department_response"`
	Profile            *UserProfileResponse `json:"profile,omitempty"`
}

type ListUsersRequest struct {
	Page     int     `json:"page" validate:"min=1"`
	Limit    int     `json:"limit" validate:"min=1,max=100"`
	Role     *string `json:"role,omitempty"`
	IsActive *bool   `json:"is_active,omitempty"`
	Search   *string `json:"search,omitempty"`
	RoleCode *string `json:"role_code,omitempty"`
}

type ListUsersResponse struct {
	Users      []UserResponse     `json:"users"`
	Pagination PaginationResponse `json:"pagination"`
}

type RoleResponse struct {
	ID   uuid.UUID `json:"id"`
	Name string    `json:"name"`
	Code string    `json:"code"`
}

type DepartmentResponse struct {
	ID   uuid.UUID `json:"id"`
	Name string    `json:"name"`
	Code string    `json:"code"`
	Path string    `json:"path"`
}

type UserProfileResponse struct {
	UserID            uuid.UUID              `json:"user_id"`
	FullName          string                 `json:"full_name"`
	DisplayName       *string                `json:"display_name,omitempty"`
	Phone             *string                `json:"phone,omitempty"`
	AvatarURL         *string                `json:"avatar_url,omitempty"`
	JobTitle          *string                `json:"job_title,omitempty"`
	EmployeeNo        *string                `json:"employee_no,omitempty"`
	Bio               *string                `json:"bio,omitempty"`
	Timezone          string                 `json:"timezone"`
	Locale            string                 `json:"locale"`
	Preferences       map[string]interface{} `json:"preferences"`
	NotificationPrefs map[string]interface{} `json:"notification_prefs"`
	LastSeenAt        *time.Time             `json:"last_seen_at,omitempty"`
	CreatedAt         time.Time              `json:"created_at"`
	UpdatedAt         time.Time              `json:"updated_at"`
	Roles             []RoleResponse         `json:"roles"`
}

type UpdateUserProfileRequest struct {
	FullName          *string                 `json:"full_name,omitempty"`
	DisplayName       *string                 `json:"display_name,omitempty"`
	Phone             *string                 `json:"phone,omitempty"`
	AvatarURL         *string                 `json:"avatar_url,omitempty"`
	JobTitle          *string                 `json:"job_title,omitempty"`
	EmployeeNo        *string                 `json:"employee_no,omitempty"`
	Bio               *string                 `json:"bio,omitempty"`
	Timezone          *string                 `json:"timezone,omitempty"`
	Locale            *string                 `json:"locale,omitempty"`
	Preferences       *map[string]interface{} `json:"preferences,omitempty"`
	NotificationPrefs *map[string]interface{} `json:"notification_prefs,omitempty"`
}

type TitleResponse struct {
	ID          uuid.UUID `json:"id"`
	Name        string    `json:"name"`
	Code        *string   `json:"code,omitempty"`
	Description *string   `json:"description,omitempty"`
}

type ListTitlesResponse struct {
	Titles []TitleResponse `json:"titles"`
}

// MentionUsersRequest represents the request for getting users for mention purposes
type MentionUsersRequest struct {
	Search *string `json:"search,omitempty" form:"search"` // Optional search term for username
	Limit  *int    `json:"limit,omitempty" form:"limit"`   // Optional limit, defaults to 50, max 100
}

// MentionUsersResponse represents the response for getting users for mention purposes
type MentionUsersResponse struct {
	Users []UserResponse `json:"users"`
	Count int            `json:"count"`
}
