package response

import (
	"time"

	"github.com/google/uuid"
)

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

// ClientResponse for single client with hierarchy info
type ClientsResponse struct {
	ID             uuid.UUID  `json:"id"`
	Name           string     `json:"name"`
	Description    *string    `json:"description"`
	ClientType     string     `json:"clientType"`
	ParentClientId *uuid.UUID `json:"parentClientId"`

	// Parent info (if sub_client)
	ParentClient *ParentClientInfo `json:"parentClient,omitempty"`

	// Children info (if parent_client)
	SubClients     []SubClientInfo `json:"subClients,omitempty"`
	SubClientCount int             `json:"subClientCount"`

	// Resource limits & usage
	MaxUsers       *int   `json:"maxUsers"`
	MaxStorage     *int64 `json:"maxStorage"`
	CurrentUsers   int    `json:"currentUsers"`   // Count of active users
	CurrentStorage *int64 `json:"currentStorage"` // Current storage usage (if tracked)

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

	// Metadata
	CreatedById *uint     `json:"createdById"`
	CreatedBy   *UserInfo `json:"createdBy,omitempty"`
	IsActive    *bool     `json:"isActive"`
	CreatedAt   time.Time `json:"createdAt"`
	UpdatedAt   time.Time `json:"updatedAt"`
}

// ClientWithUserResponse for creating client and admin user response
type ClientWithUserResponse struct {
	Client    ClientsResponse   `json:"client"`
	AdminUser AdminUserResponse `json:"adminUser"`
	Message   string            `json:"message"`
}

// AdminUserResponse for admin user info
type AdminUserResponse struct {
	ID                  uint      `json:"id"`
	Username            string    `json:"username"`
	Email               string    `json:"email"`
	Fullname            string    `json:"fullname"`
	UserLevelId         uint      `json:"userLevelId"`
	UserRoleId          uint      `json:"userRoleId"`
	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"`
	KeycloakId          *string   `json:"keycloakId"`
	ClientId            uuid.UUID `json:"clientId"`
	IsActive            bool      `json:"isActive"`
	CreatedAt           time.Time `json:"createdAt"`
	UpdatedAt           time.Time `json:"updatedAt"`
}

// ParentClientInfo minimal parent info
type ParentClientInfo struct {
	ID         uuid.UUID `json:"id"`
	Name       string    `json:"name"`
	ClientType string    `json:"clientType"`
}

// SubClientInfo minimal child info
type SubClientInfo struct {
	ID           uuid.UUID `json:"id"`
	Name         string    `json:"name"`
	ClientType   string    `json:"clientType"`
	CurrentUsers int       `json:"currentUsers"`
	IsActive     *bool     `json:"isActive"`
}

// UserInfo minimal user info
type UserInfo struct {
	ID       uint   `json:"id"`
	Username string `json:"username"`
	Fullname string `json:"fullname"`
}

// ClientHierarchyResponse full tree structure
type ClientHierarchyResponse struct {
	ID             uuid.UUID                 `json:"id"`
	Name           string                    `json:"name"`
	Description    *string                   `json:"description"`
	ClientType     string                    `json:"clientType"`
	Level          int                       `json:"level"` // Depth in tree (0 = root)
	Path           []string                  `json:"path"`  // Breadcrumb path
	ParentClientId *uuid.UUID                `json:"parentClientId"`
	SubClients     []ClientHierarchyResponse `json:"subClients,omitempty"`
	CurrentUsers   int                       `json:"currentUsers"`
	IsActive       *bool                     `json:"isActive"`
}

// ClientStatsResponse statistics for a client
type ClientStatsResponse struct {
	ClientId   uuid.UUID `json:"clientId"`
	ClientName string    `json:"clientName"`

	// User stats
	TotalUsers    int `json:"totalUsers"`
	ActiveUsers   int `json:"activeUsers"`
	InactiveUsers int `json:"inactiveUsers"`

	// Content stats
	TotalArticles     int `json:"totalArticles"`
	PublishedArticles int `json:"publishedArticles"`
	DraftArticles     int `json:"draftArticles"`

	// Storage stats
	StorageUsed    *int64   `json:"storageUsed"`
	StorageLimit   *int64   `json:"storageLimit"`
	StoragePercent *float64 `json:"storagePercent"`

	// Hierarchy stats
	IsParent         bool `json:"isParent"`
	SubClientCount   int  `json:"subClientCount"`
	TotalDescendants int  `json:"totalDescendants"` // All nested sub-clients
}

// ClientListResponse for list endpoint
type ClientListResponse struct {
	ID             uuid.UUID  `json:"id"`
	Name           string     `json:"name"`
	ClientType     string     `json:"clientType"`
	ParentClientId *uuid.UUID `json:"parentClientId"`
	ParentName     *string    `json:"parentName,omitempty"`
	SubClientCount int        `json:"subClientCount"`
	CurrentUsers   int        `json:"currentUsers"`
	IsActive       *bool      `json:"isActive"`
	CreatedAt      time.Time  `json:"createdAt"`
}

// ClientAccessResponse for user's accessible clients
type ClientAccessResponse struct {
	// User info
	UserId   uint   `json:"userId"`
	Username string `json:"username"`

	// Access summary
	IsSuperAdmin    bool       `json:"isSuperAdmin"`
	PrimaryClientId *uuid.UUID `json:"primaryClientId"`
	TotalAccessible int        `json:"totalAccessibleClients"`

	// Accessible clients
	Clients []AccessibleClientInfo `json:"clients"`
}

type AccessibleClientInfo struct {
	ClientId          uuid.UUID `json:"clientId"`
	ClientName        string    `json:"clientName"`
	ClientType        string    `json:"clientType"`
	AccessType        string    `json:"accessType"` // read, write, admin, owner
	CanManage         bool      `json:"canManage"`
	CanDelegate       bool      `json:"canDelegate"`
	IncludeSubClients bool      `json:"includeSubClients"`
	IsPrimaryClient   bool      `json:"isPrimaryClient"`
}

// BulkOperationResponse for bulk operations
type BulkOperationResponse struct {
	TotalRequested int                   `json:"totalRequested"`
	Successful     int                   `json:"successful"`
	Failed         int                   `json:"failed"`
	Results        []BulkOperationResult `json:"results"`
}

type BulkOperationResult struct {
	Index    int        `json:"index"`
	ClientId *uuid.UUID `json:"clientId,omitempty"`
	Name     string     `json:"name"`
	Success  bool       `json:"success"`
	Error    *string    `json:"error,omitempty"`
}
