// 权限状态类型
export type PermissionStatus = 'active' | 'inactive' | 'deprecated'

// 权限类型
export type PermissionType = 'module' | 'menu' | 'button' | 'action' | 'api'

// 角色类型
export type RoleType = 'system' | 'custom' | 'guest'

// 角色状态类型
export type RoleStatus = 'active' | 'inactive' | 'pending'

// 权限基本信息接口
export interface Permission {
  id: number
  name: string
  code: string
  description: string
  type: PermissionType
  parentId: number | null
  level: number
  status: PermissionStatus
  createTime: string
  updateTime: string
  // 扩展信息
  icon?: string
  route?: string
  component?: string
  sortOrder?: number
  // 子权限
  children?: Permission[]
  // 权限控制
  isPublic?: boolean
  requiresAuth?: boolean
  // 元数据
  metadata?: Record<string, any>
}

// 角色基本信息接口
export interface Role {
  id: number
  name: string
  code: string
  description: string
  type: RoleType
  status: RoleStatus
  permissionIds: number[]
  userCount: number
  createTime: string
  updateTime: string
  // 扩展信息
  permissions?: Permission[]
  // 角色属性
  isDefault?: boolean
  isSystem?: boolean
  // 访问控制
  maxUsers?: number
  expirationDate?: string
  // 审批流程
  requiresApproval?: boolean
  approvalWorkflow?: string
  // 元数据
  metadata?: Record<string, any>
}

// 用户角色关联接口
export interface UserRole {
  id: number
  userId: number
  roleId: number
  assignTime: string
  assignBy: string
  // 扩展信息
  expirationTime?: string
  // 审批信息
  approvedBy?: string
  approveTime?: string
  approvalStatus?: 'pending' | 'approved' | 'rejected'
  // 权限委托
  delegatedFrom?: number
  delegationReason?: string
  // 元数据
  metadata?: Record<string, any>
}

// 权限查询参数接口
export interface PermissionQueryParams {
  page: number
  pageSize: number
  keyword?: string
  status?: PermissionStatus
  type?: PermissionType
  parentId?: number | null
  level?: number
  // 高级筛选
  isPublic?: boolean
  requiresAuth?: boolean
  // 排序
  sortField?: string
  sortOrder?: 'asc' | 'desc'
}

// 角色查询参数接口
export interface RoleQueryParams {
  page: number
  pageSize: number
  keyword?: string
  status?: RoleStatus
  type?: RoleType
  // 高级筛选
  isDefault?: boolean
  isSystem?: boolean
  // 用户数量范围
  minUsers?: number
  maxUsers?: number
  // 排序
  sortField?: string
  sortOrder?: 'asc' | 'desc'
}

// 权限统计信息接口
export interface PermissionStatistics {
  totalPermissions: number
  activePermissions: number
  inactivePermissions: number
  totalRoles: number
  activeRoles: number
  inactiveRoles: number
  totalUsersWithRoles: number
  permissionUsage: Record<string, number>
  roleDistribution: Record<string, number>
  userPermissionStats: {
    averagePermissionsPerUser: number
    maxPermissionsPerUser: number
    minPermissionsPerUser: number
    usersWithExcessivePermissions: number
    // 权限分布
    permissionDistribution?: Record<string, number>
    // 风险指标
    permissionConflictRate?: number
    permissionRedundancyRate?: number
  }
  auditLogs: {
    totalLogs: number
    permissionChanges: number
    roleChanges: number
    userRoleChanges: number
    // 安全事件
    securityIncidents?: number
    complianceViolations?: number
  }
  // 性能指标
  permissionCheckPerformance?: number
  roleResolutionTime?: number
  // 合规性指标
  complianceScore?: number
  auditPassRate?: number
}

// 审计日志接口
export interface AuditLog {
  id: number
  action: string
  targetType: string
  targetId: number
  targetName: string
  performerId: number
  performerName: string
  details: string
  ipAddress: string
  userAgent: string
  timestamp: string
  status: 'success' | 'failure' | 'warning'
  // 扩展信息
  resourceType?: string
  resourceId?: number
  // 变更详情
  oldValue?: any
  newValue?: any
  // 安全信息
  securityLevel?: 'low' | 'medium' | 'high' | 'critical'
  riskScore?: number
  // 地理位置
  location?: string
  deviceInfo?: string
  // 调查信息
  investigationStatus?: 'open' | 'in_progress' | 'resolved' | 'closed'
  investigationNotes?: string
}

// 权限分配请求接口
export interface PermissionAssignmentRequest {
  userId: number
  permissionIds: number[]
  assignBy: string
  // 有效期
  expirationTime?: string
  // 审批信息
  requiresApproval?: boolean
  approvalWorkflow?: string
  // 委托信息
  delegatedFrom?: number
  delegationReason?: string
}

// 角色分配请求接口
export interface RoleAssignmentRequest {
  userId: number
  roleId: number
  assignBy: string
  // 有效期
  expirationTime?: string
  // 审批信息
  requiresApproval?: boolean
  approvalWorkflow?: string
  // 委托信息
  delegatedFrom?: number
  delegationReason?: string
}

// 权限检查请求接口
export interface PermissionCheckRequest {
  userId: number
  permissionCode: string
  // 上下文信息
  resourceType?: string
  resourceId?: number
  // 环境信息
  ipAddress?: string
  userAgent?: string
  // 安全验证
  sessionId?: string
  token?: string
}

// 权限检查响应接口
export interface PermissionCheckResponse {
  hasPermission: boolean
  // 详细权限信息
  permission?: Permission
  // 权限来源
  grantedBy: 'role' | 'direct' | 'inheritance' | 'delegation'
  role?: Role
  // 有效期
  validUntil?: string
  // 限制信息
  restrictions?: string[]
  // 审计信息
  auditId?: number
}

// 权限继承配置接口
export interface PermissionInheritanceConfig {
  enabled: boolean
  inheritanceRules: Array<{
    sourceType: string
    sourceId: number
    targetType: string
    targetId: number
    inheritanceType: 'full' | 'partial' | 'conditional'
    conditions?: Record<string, any>
    // 有效期
    validFrom?: string
    validUntil?: string
    // 优先级
    priority: number
  }>
  // 冲突解决策略
  conflictResolution: 'deny' | 'allow' | 'highest_priority' | 'most_specific'
  // 性能优化
  cachingEnabled: boolean
  cacheTTL: number
  // 审计配置
  auditInheritance: boolean
}

// 权限委托配置接口
export interface PermissionDelegationConfig {
  enabled: boolean
  maxDelegationDepth: number
  delegationTimeLimit: number
  allowedDelegators: number[]
  restrictedPermissions: number[]
  // 审批流程
  requiresApproval: boolean
  approvalWorkflow: string
  // 审计要求
  auditDelegation: boolean
  // 通知设置
  notifyOriginalOwner: boolean
  notifyDelegator: boolean
  notifyDelegatee: boolean
}

// 权限审计配置接口
export interface PermissionAuditConfig {
  enabled: boolean
  auditLevel: 'minimal' | 'standard' | 'detailed' | 'comprehensive'
  retentionPeriod: number
  // 审计事件
  auditedActions: string[]
  // 敏感操作
  sensitiveOperations: Array<{
    action: string
    sensitivityLevel: 'low' | 'medium' | 'high' | 'critical'
    additionalAuditing: boolean
  }>
  // 实时监控
  realTimeMonitoring: boolean
  alertThresholds: Record<string, number>
  // 合规要求
  complianceRequirements: string[]
  regulatoryStandards: string[]
}

// 权限导出参数接口
export interface PermissionExportParams {
  format: 'csv' | 'excel' | 'json'
  include: Array<'permissions' | 'roles' | 'user_roles' | 'audit_logs'>
  filters?: {
    status?: PermissionStatus | RoleStatus
    type?: PermissionType | RoleType
    dateRange?: [string, string]
  }
  // 数据范围
  dataScope: 'all' | 'active' | 'recent'
  // 安全选项
  encryptData: boolean
  passwordProtect: boolean
  // 元数据
  includeMetadata: boolean
}

// 权限导入参数接口
export interface PermissionImportParams {
  file: File
  format: 'csv' | 'excel' | 'json'
  importType: 'permissions' | 'roles' | 'assignments'
  // 冲突处理
  conflictResolution: 'skip' | 'overwrite' | 'merge' | 'rename'
  // 验证选项
  validateData: boolean
  dryRun: boolean
  // 通知设置
  notifyOnCompletion: boolean
  notifyOnError: boolean
  // 审计配置
  auditImport: boolean
}

// 权限同步配置接口
export interface PermissionSyncConfig {
  enabled: boolean
  syncFrequency: number
  syncDirection: 'bidirectional' | 'source_to_target' | 'target_to_source'
  // 数据源配置
  sourceSystem: string
  sourceConfig: Record<string, any>
  targetSystem: string
  targetConfig: Record<string, any>
  // 映射规则
  fieldMappings: Record<string, string>
  valueTransformations: Record<string, (value: any) => any>
  // 冲突解决
  conflictResolution: 'source_wins' | 'target_wins' | 'manual'
  // 性能优化
  batchSize: number
  parallelProcessing: boolean
  // 监控配置
  monitorSync: boolean
  alertOnFailure: boolean
}

// 权限健康检查接口
export interface PermissionHealthCheck {
  timestamp: string
  status: 'healthy' | 'degraded' | 'unhealthy'
  // 组件状态
  components: Array<{
    name: string
    status: 'up' | 'down' | 'degraded'
    responseTime: number
    errorRate: number
  }>
  // 性能指标
  performanceMetrics: {
    permissionCheckTime: number
    roleResolutionTime: number
    cacheHitRate: number
    memoryUsage: number
  }
  // 安全指标
  securityMetrics: {
    failedAttempts: number
    suspiciousActivities: number
    complianceViolations: number
  }
  // 建议措施
  recommendations: string[]
  // 历史趋势
  historicalTrend: Record<string, number>
}