# @dbs-portal/module-tenant-management

A comprehensive multi-tenancy management module for DBS Portal, providing tenant CRUD operations, context switching, analytics, feature management, and backup/restore capabilities.

## Features

### 🏢 Tenant Management
- Complete CRUD operations for tenant entities
- Tenant activation/deactivation with status tracking
- Bulk operations for multiple tenants
- Tenant cloning with configurable options
- Connection string management and testing

### 🔄 Multi-Tenant Context
- Seamless tenant switching with context preservation
- Tenant-aware React context provider
- Automatic tenant detection and validation
- Tenant isolation and security boundaries

### 📊 Analytics & Monitoring
- Comprehensive tenant analytics dashboard
- Usage metrics and growth tracking
- Performance monitoring and uptime statistics
- Revenue analytics and user engagement metrics

### ⚙️ Feature Management
- Dynamic feature flag management per tenant
- Feature expiration and lifecycle management
- Granular feature configuration with typed values
- Feature dependency and requirement tracking

### 💾 Backup & Restore
- Automated and manual backup creation
- Multiple backup types (full, incremental, differential)
- Backup scheduling and retention policies
- One-click restore with data validation

### 🎨 Advanced UI Components
- **TenantDetail**: Comprehensive tenant information display
- **TenantSwitcher**: Multi-tenant context switcher with search
- **TenantAnalytics**: Real-time analytics dashboard
- **TenantFeatureManager**: Feature flag management interface
- **TenantBackupManager**: Backup and restore operations
- **Tenant CRUD Operations**: Create, read, update, and delete tenants
- **Feature Management**: Configure tenant-specific features and limitations
- **Connection Management**: Manage tenant database connections
- **Analytics Dashboard**: Tenant usage statistics and analytics
- **Tenant Switching**: Easy tenant context switching for administrators

## Installation

```bash
yarn add @dbs-portal/module-tenant-management
```

## Usage

```typescript
import { 
  TenantList, 
  TenantForm, 
  TenantDetail,
  useTenants,
  TenantProvider 
} from '@dbs-portal/module-tenant-management'

// Wrap your app with TenantProvider
function App() {
  return (
    <TenantProvider>
      <TenantList />
    </TenantProvider>
  )
}

// Use tenant hooks
function TenantComponent() {
  const { data: tenants, isLoading } = useTenants()
  
  return (
    <div>
      {tenants?.map(tenant => (
        <div key={tenant.id}>{tenant.name}</div>
      ))}
    </div>
  )
}
```

## Components

### Core Components
- `TenantList` - List all tenants with filtering and pagination
- `TenantForm` - Create/edit tenant form with validation
- `TenantDetail` - Detailed tenant information and statistics
- `TenantSwitcher` - Tenant context switching component

### Advanced Components
- `TenantFeatureManager` - Manage tenant-specific features
- `TenantAnalytics` - Tenant usage analytics dashboard
- `TenantConnectionManager` - Database connection management
- `TenantBackupRestore` - Backup and restore functionality

## Hooks

### Data Hooks
- `useTenants()` - Fetch tenants with pagination
- `useTenant(id)` - Fetch single tenant
- `useCreateTenant()` - Create new tenant
- `useUpdateTenant()` - Update existing tenant
- `useDeleteTenant()` - Delete tenant

### Feature Hooks
- `useTenantFeatures(id)` - Manage tenant features
- `useTenantAnalytics(id)` - Get tenant analytics
- `useTenantConnections(id)` - Manage connections
- `useCurrentTenant()` - Get current tenant context

## Services

- `TenantService` - Core tenant CRUD operations
- `TenantFeatureService` - Feature management
- `TenantAnalyticsService` - Analytics and reporting
- `TenantConnectionService` - Connection management

## Types

```typescript
interface Tenant {
  id: string
  name: string
  displayName: string
  connectionString?: string
  isActive: boolean
  features: TenantFeature[]
  createdAt: Date
  updatedAt: Date
}

interface TenantFeature {
  name: string
  isEnabled: boolean
  value?: any
  expiresAt?: Date
}
```

## Multi-tenancy Support

This module provides complete multi-tenancy support including:

- **Tenant Isolation**: Complete data separation between tenants
- **Feature Flags**: Per-tenant feature configuration
- **Resource Limits**: Configurable limits per tenant
- **Analytics**: Usage tracking and reporting
- **Backup/Restore**: Tenant-specific data management

## Dependencies

- `@dbs-portal/core-shared` - Shared types and utilities
- `@dbs-portal/core-api` - API client functionality
- `@dbs-portal/core-ui` - UI components and themes
- `@dbs-portal/core-store` - State management
- `@dbs-portal/core-auth` - Authentication integration
- `@dbs-portal/module-identity` - User and role management

## License

MIT
