package request

import (
	"netidhub-saas-be/utils/paginator"

	"github.com/google/uuid"
)

// ========================================================================
// REQUEST STRUCTS - Updated for Multi-Client Hierarchy Support (camelCase)
// ========================================================================

// ClientsCreateRequest for creating new client with hierarchy support
type ClientsCreateRequest struct {
	Name           string     `json:"name" validate:"required"`
	Description    *string    `json:"description"`
	ClientType     string     `json:"clientType" validate:"required,oneof=parent_client sub_client standalone"` // Enum validation
	ParentClientId *uuid.UUID `json:"parentClientId"`                                                           // Optional: only for sub_client type

	// Resource limits
	MaxUsers   *int   `json:"maxUsers"`
	MaxStorage *int64 `json:"maxStorage"`

	// Custom settings (JSON string)
	Settings *string `json:"settings"`
}

// ClientWithUserCreateRequest for creating client and admin user in one request
type ClientWithUserCreateRequest struct {
	// Client information
	Client ClientsCreateRequest `json:"client" validate:"required"`

	// Admin user information
	AdminUser AdminUserCreateRequest `json:"adminUser" validate:"required"`
}

// AdminUserCreateRequest for creating admin user
type AdminUserCreateRequest struct {
	Username            string  `json:"username" validate:"required,lowercase"`
	Email               string  `json:"email" validate:"required,email"`
	Fullname            string  `json:"fullname" validate:"required"`
	Password            string  `json:"password" validate:"required"`
	PhoneNumber         *string `json:"phoneNumber"`
	Address             *string `json:"address"`
	WorkType            *string `json:"workType"`
	GenderType          *string `json:"genderType"`
	IdentityType        *string `json:"identityType"`
	IdentityGroup       *string `json:"identityGroup"`
	IdentityGroupNumber *string `json:"identityGroupNumber"`
	IdentityNumber      *string `json:"identityNumber"`
	DateOfBirth         *string `json:"dateOfBirth"`
	LastEducation       *string `json:"lastEducation"`
}

// ClientsUpdateRequest for updating existing client
type ClientsUpdateRequest struct {
	Name           *string    `json:"name"`
	Description    *string    `json:"description"`
	ClientType     *string    `json:"clientType" validate:"omitempty,oneof=parent_client sub_client standalone"`
	ParentClientId *uuid.UUID `json:"parentClientId"`

	// Resource limits
	MaxUsers   *int   `json:"maxUsers"`
	MaxStorage *int64 `json:"maxStorage"`

	// Custom settings
	Settings *string `json:"settings"`

	IsActive *bool `json:"isActive"`
}

// ClientsQueryRequest for filtering and pagination
type ClientsQueryRequest struct {
	// Search filters
	Name       *string `query:"name"`
	ClientType *string `query:"clientType" validate:"omitempty,oneof=parent_client sub_client standalone"`

	// Hierarchy filters
	ParentClientId    *uuid.UUID `query:"parentClientId"`    // Get children of specific parent
	IncludeSubClients *bool      `query:"includeSubClients"` // Include all descendants
	OnlyParentClients *bool      `query:"onlyParentClients"` // Only clients with children
	OnlyStandalone    *bool      `query:"onlyStandalone"`    // Only standalone clients

	// Status filter
	IsActive *bool `query:"isActive"`

	Pagination *paginator.Pagination
}

// MoveClientRequest for moving client to different parent
type MoveClientRequest struct {
	TargetParentId *uuid.UUID `json:"targetParentId"` // null = move to root (standalone)
}

// BulkCreateSubClientsRequest for creating multiple sub-clients at once
type BulkCreateSubClientsRequest struct {
	ParentClientId uuid.UUID                      `json:"parentClientId" validate:"required"`
	SubClients     []ClientsCreateSubClientDetail `json:"subClients" validate:"required,min=1,dive"`
}

type ClientsCreateSubClientDetail struct {
	Name        string  `json:"name" validate:"required"`
	Description *string `json:"description"`
	MaxUsers    *int    `json:"maxUsers"`
	MaxStorage  *int64  `json:"maxStorage"`
}

// ClientHierarchyRequest for getting client tree structure
type ClientHierarchyRequest struct {
	ClientId        uuid.UUID `json:"clientId" validate:"required"`
	MaxDepth        *int      `json:"maxDepth"` // Limit recursion depth
	IncludeInactive *bool     `json:"includeInactive"`
}

// ClientsQueryRequestContext for parsing query parameters from HTTP context
type ClientsQueryRequestContext struct {
	Name              string `query:"name"`
	ClientType        string `query:"clientType"`
	ParentClientId    string `query:"parentClientId"`
	IncludeSubClients string `query:"includeSubClients"`
	OnlyParentClients string `query:"onlyParentClients"`
	OnlyStandalone    string `query:"onlyStandalone"`
	IsActive          string `query:"isActive"`
	CreatedById       string `query:"createdById"`
}

// ToParamRequest converts context to ClientsQueryRequest
func (ctx *ClientsQueryRequestContext) ToParamRequest() ClientsQueryRequest {
	req := ClientsQueryRequest{}

	// Name filter
	if ctx.Name != "" {
		req.Name = &ctx.Name
	}

	// Client type filter
	if ctx.ClientType != "" {
		req.ClientType = &ctx.ClientType
	}

	// Parent client ID filter
	if ctx.ParentClientId != "" {
		if parentId, err := uuid.Parse(ctx.ParentClientId); err == nil {
			req.ParentClientId = &parentId
		}
	}

	// Boolean filters
	if ctx.IncludeSubClients != "" {
		includeSubClients := ctx.IncludeSubClients == "true"
		req.IncludeSubClients = &includeSubClients
	}

	if ctx.OnlyParentClients != "" {
		onlyParentClients := ctx.OnlyParentClients == "true"
		req.OnlyParentClients = &onlyParentClients
	}

	if ctx.OnlyStandalone != "" {
		onlyStandalone := ctx.OnlyStandalone == "true"
		req.OnlyStandalone = &onlyStandalone
	}

	if ctx.IsActive != "" {
		isActive := ctx.IsActive == "true"
		req.IsActive = &isActive
	}

	return req
}
