// SolaceNet Capability Platform - Type Definitions

// ============================================================================
// Capability Lifecycle States
// ============================================================================

export enum CapabilityState {
  DISABLED = 'disabled',
  PILOT = 'pilot',
  ENABLED = 'enabled',
  SUSPENDED = 'suspended',
  DRAIN = 'drain',
}

// ============================================================================
// Capability Types
// ============================================================================

export interface Capability {
  id: string;
  capabilityId: string;
  name: string;
  version: string;
  description?: string;
  ownerTeam?: string;
  dependencies: string[]; // Array of capability IDs
  configSchema?: Record<string, unknown>;
  defaultState: CapabilityState;
  status: 'active' | 'deprecated' | 'archived';
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

export interface CapabilityBinding {
  id: string;
  capabilityId: string;
  providerId?: string;
  region: string;
  config?: Record<string, unknown>;
  secretsRef?: string;
  status: 'active' | 'inactive';
  createdAt: Date;
  updatedAt: Date;
}

export interface CapabilityDependency {
  id: string;
  dependentId: string;
  dependencyId: string;
  dependencyType: 'required' | 'optional' | 'recommended';
  createdAt: Date;
  updatedAt: Date;
}

// ============================================================================
// Entitlement Types
// ============================================================================

export interface Entitlement {
  id: string;
  tenantId: string;
  programId?: string;
  capabilityId: string;
  region?: string;
  channel?: string;
  stateOverride?: CapabilityState;
  allowlist: string[]; // Array of user/client IDs for pilot mode
  effectiveFrom: Date;
  effectiveTo?: Date;
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

export interface EntitlementCheckContext {
  tenantId: string;
  programId?: string;
  capabilityId: string;
  region?: string;
  channel?: string;
  actor?: string; // User/client ID
}

// ============================================================================
// Policy Types
// ============================================================================

export interface PolicyRule {
  id: string;
  ruleId: string;
  capabilityId: string;
  scope: 'tenant' | 'program' | 'region' | 'channel' | 'global';
  scopeValue?: string;
  condition: Record<string, unknown>; // JSON expression
  decision: 'allow' | 'deny' | 'limit';
  limits?: Record<string, unknown>;
  reason?: string;
  ticket?: string;
  priority: number;
  status: 'active' | 'inactive';
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

export interface PolicyDecisionRequest {
  tenantId: string;
  programId?: string;
  capabilityId: string;
  region?: string;
  channel?: string;
  actor?: string;
  context?: Record<string, unknown>; // Additional context (risk_score, kyc_level, etc.)
}

export interface PolicyDecisionResponse {
  allowed: boolean;
  mode: CapabilityState;
  limits?: Record<string, unknown>;
  reasonCode?: string;
  decisionId: string;
  evaluatedRules?: string[]; // Rule IDs that were evaluated
}

// ============================================================================
// Audit Types
// ============================================================================

export interface ToggleAuditLog {
  id: string;
  actor: string;
  action: 'enabled' | 'disabled' | 'suspended' | 'drain' | 'kill_switch';
  capabilityId: string;
  beforeState?: string;
  afterState: string;
  scope?: Record<string, unknown>;
  reason?: string;
  ticket?: string;
  metadata?: Record<string, unknown>;
  timestamp: Date;
}

// ============================================================================
// Provider Connector Types
// ============================================================================

export interface ProviderConnector {
  id: string;
  connectorId: string;
  name: string;
  providerType: 'payment-processor' | 'mobile-money' | 'fx-provider' | 'other';
  region?: string;
  configSchema?: Record<string, unknown>;
  secretsSchema?: Record<string, unknown>;
  status: 'active' | 'inactive' | 'deprecated';
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================================================
// Service Request/Response Types
// ============================================================================

export interface CreateCapabilityRequest {
  capabilityId: string;
  name: string;
  version?: string;
  description?: string;
  ownerTeam?: string;
  dependencies?: string[];
  configSchema?: Record<string, unknown>;
  defaultState?: CapabilityState;
  metadata?: Record<string, unknown>;
}

export interface UpdateCapabilityRequest {
  name?: string;
  version?: string;
  description?: string;
  ownerTeam?: string;
  dependencies?: string[];
  configSchema?: Record<string, unknown>;
  defaultState?: CapabilityState;
  status?: 'active' | 'deprecated' | 'archived';
  metadata?: Record<string, unknown>;
}

export interface CreateEntitlementRequest {
  tenantId: string;
  programId?: string;
  capabilityId: string;
  region?: string;
  channel?: string;
  stateOverride?: CapabilityState;
  allowlist?: string[];
  effectiveFrom?: Date;
  effectiveTo?: Date;
  metadata?: Record<string, unknown>;
}

export interface CreatePolicyRuleRequest {
  ruleId: string;
  capabilityId: string;
  scope: 'tenant' | 'program' | 'region' | 'channel' | 'global';
  scopeValue?: string;
  condition: Record<string, unknown>;
  decision: 'allow' | 'deny' | 'limit';
  limits?: Record<string, unknown>;
  reason?: string;
  ticket?: string;
  priority?: number;
  metadata?: Record<string, unknown>;
}
