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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
"""
🏒 ENTERPRISE FEATURES - DataVision Production-Ready Capabilities
==================================================================

Enterprise-grade features:
- Action Engine (exports, alerts, webhooks)
- Audit Logging
- Rate Limiting
- Multi-user Support
- Scheduled Reports

Ready for production deployment.
"""

import json
import logging
import os
from typing import Dict, List, Optional, Any, Callable
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from enum import Enum
import hashlib
import threading
from collections import defaultdict

logger = logging.getLogger(__name__)


# =============================================================================
# ACTION ENGINE - Exports, Alerts, Webhooks
# =============================================================================

class ActionType(Enum):
    """Types of actions"""
    EXPORT_CSV = "export_csv"
    EXPORT_EXCEL = "export_excel"
    EXPORT_PDF = "export_pdf"
    SEND_EMAIL = "send_email"
    WEBHOOK = "webhook"
    SLACK = "slack"
    SCHEDULE = "schedule"


@dataclass
class ActionResult:
    """Result of an action"""
    success: bool
    action_type: ActionType
    message: str
    output_path: Optional[str] = None
    metadata: Dict[str, Any] = field(default_factory=dict)


class ActionEngine:
    """
    ⚑ Action Engine
    
    Execute actions on data and insights:
    - Export to CSV, Excel, PDF
    - Send email alerts
    - Trigger webhooks
    - Schedule recurring actions
    """
    
    def __init__(self, storage_path: str = "storage/exports"):
        self.storage_path = storage_path
        os.makedirs(storage_path, exist_ok=True)
        self.scheduled_actions: Dict[str, Dict] = {}
    
    async def execute(
        self,
        action_type: ActionType,
        data: Any,
        config: Dict[str, Any],
        user_id: str = "default"
    ) -> ActionResult:
        """
        Execute an action
        
        Args:
            action_type: Type of action
            data: Data to process
            config: Action configuration
            user_id: User identifier
            
        Returns:
            Action result
        """
        try:
            if action_type == ActionType.EXPORT_CSV:
                return await self._export_csv(data, config, user_id)
            elif action_type == ActionType.EXPORT_EXCEL:
                return await self._export_excel(data, config, user_id)
            elif action_type == ActionType.EXPORT_PDF:
                return await self._export_pdf(data, config, user_id)
            elif action_type == ActionType.SEND_EMAIL:
                return await self._send_email(data, config)
            elif action_type == ActionType.WEBHOOK:
                return await self._trigger_webhook(data, config)
            elif action_type == ActionType.SCHEDULE:
                return await self._schedule_action(config, user_id)
            else:
                return ActionResult(
                    success=False,
                    action_type=action_type,
                    message=f"Unknown action type: {action_type}"
                )
        except Exception as e:
            logger.error(f"Action execution error: {e}")
            return ActionResult(
                success=False,
                action_type=action_type,
                message=str(e)
            )
    
    async def _export_csv(
        self, 
        data: Any, 
        config: Dict, 
        user_id: str
    ) -> ActionResult:
        """Export data to CSV"""
        import pandas as pd
        
        filename = config.get("filename", f"export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv")
        filepath = os.path.join(self.storage_path, user_id, filename)
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        
        if isinstance(data, pd.DataFrame):
            data.to_csv(filepath, index=False)
        elif isinstance(data, dict):
            pd.DataFrame([data]).to_csv(filepath, index=False)
        elif isinstance(data, list):
            pd.DataFrame(data).to_csv(filepath, index=False)
        else:
            return ActionResult(
                success=False,
                action_type=ActionType.EXPORT_CSV,
                message="Invalid data type for CSV export"
            )
        
        return ActionResult(
            success=True,
            action_type=ActionType.EXPORT_CSV,
            message=f"Exported to {filename}",
            output_path=filepath
        )
    
    async def _export_excel(
        self, 
        data: Any, 
        config: Dict, 
        user_id: str
    ) -> ActionResult:
        """Export data to Excel"""
        import pandas as pd
        
        try:
            filename = config.get("filename", f"export_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx")
            filepath = os.path.join(self.storage_path, user_id, filename)
            os.makedirs(os.path.dirname(filepath), exist_ok=True)
            
            if isinstance(data, pd.DataFrame):
                data.to_excel(filepath, index=False, engine='openpyxl')
            else:
                return ActionResult(
                    success=False,
                    action_type=ActionType.EXPORT_EXCEL,
                    message="Invalid data type for Excel export"
                )
            
            return ActionResult(
                success=True,
                action_type=ActionType.EXPORT_EXCEL,
                message=f"Exported to {filename}",
                output_path=filepath
            )
        except ImportError:
            return ActionResult(
                success=False,
                action_type=ActionType.EXPORT_EXCEL,
                message="openpyxl not installed. Use: pip install openpyxl"
            )
    
    async def _export_pdf(
        self, 
        data: Any, 
        config: Dict, 
        user_id: str
    ) -> ActionResult:
        """Export report to PDF"""
        try:
            from reportlab.lib import colors
            from reportlab.lib.pagesizes import letter
            from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
            from reportlab.lib.styles import getSampleStyleSheet
            
            filename = config.get("filename", f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf")
            filepath = os.path.join(self.storage_path, user_id, filename)
            os.makedirs(os.path.dirname(filepath), exist_ok=True)
            
            doc = SimpleDocTemplate(filepath, pagesize=letter)
            elements = []
            styles = getSampleStyleSheet()
            
            # Add title
            title = config.get("title", "DataVision Report")
            elements.append(Paragraph(title, styles['Title']))
            
            # Add content
            if isinstance(data, str):
                elements.append(Paragraph(data, styles['Normal']))
            elif isinstance(data, dict):
                for key, value in data.items():
                    elements.append(Paragraph(f"<b>{key}:</b> {value}", styles['Normal']))
            
            doc.build(elements)
            
            return ActionResult(
                success=True,
                action_type=ActionType.EXPORT_PDF,
                message=f"Report exported to {filename}",
                output_path=filepath
            )
        except ImportError:
            return ActionResult(
                success=False,
                action_type=ActionType.EXPORT_PDF,
                message="reportlab not installed. Use: pip install reportlab"
            )
    
    async def _send_email(self, data: Any, config: Dict) -> ActionResult:
        """Send email (placeholder - integrate with email service)"""
        to_email = config.get("to")
        subject = config.get("subject", "DataVision Notification")
        
        if not to_email:
            return ActionResult(
                success=False,
                action_type=ActionType.SEND_EMAIL,
                message="No recipient email specified"
            )
        
        # Placeholder - would integrate with email service
        logger.info(f"Email would be sent to {to_email}: {subject}")
        
        return ActionResult(
            success=True,
            action_type=ActionType.SEND_EMAIL,
            message=f"Email queued for {to_email}",
            metadata={"to": to_email, "subject": subject}
        )
    
    async def _trigger_webhook(self, data: Any, config: Dict) -> ActionResult:
        """Trigger a webhook"""
        try:
            import httpx
            
            url = config.get("url")
            if not url:
                return ActionResult(
                    success=False,
                    action_type=ActionType.WEBHOOK,
                    message="No webhook URL specified"
                )
            
            headers = config.get("headers", {"Content-Type": "application/json"})
            
            async with httpx.AsyncClient() as client:
                response = await client.post(
                    url,
                    json=data if isinstance(data, dict) else {"data": str(data)},
                    headers=headers,
                    timeout=30
                )
            
            return ActionResult(
                success=response.is_success,
                action_type=ActionType.WEBHOOK,
                message=f"Webhook triggered: {response.status_code}",
                metadata={"status_code": response.status_code}
            )
        except ImportError:
            return ActionResult(
                success=False,
                action_type=ActionType.WEBHOOK,
                message="httpx not installed. Use: pip install httpx"
            )
        except Exception as e:
            return ActionResult(
                success=False,
                action_type=ActionType.WEBHOOK,
                message=str(e)
            )
    
    async def _schedule_action(self, config: Dict, user_id: str) -> ActionResult:
        """Schedule a recurring action"""
        schedule_id = config.get("id", hashlib.md5(str(config).encode()).hexdigest()[:8])
        
        self.scheduled_actions[schedule_id] = {
            "user_id": user_id,
            "config": config,
            "created_at": datetime.now().isoformat(),
            "next_run": config.get("next_run"),
            "frequency": config.get("frequency", "daily")
        }
        
        return ActionResult(
            success=True,
            action_type=ActionType.SCHEDULE,
            message=f"Action scheduled: {schedule_id}",
            metadata={"schedule_id": schedule_id}
        )


# =============================================================================
# AUDIT LOGGING
# =============================================================================

@dataclass
class AuditEntry:
    """An audit log entry"""
    timestamp: str
    user_id: str
    action: str
    resource: str
    details: Dict[str, Any]
    ip_address: Optional[str] = None
    success: bool = True


class AuditLogger:
    """
    πŸ“‹ Audit Logger
    
    Track all user actions for compliance:
    - Query logs
    - Data access
    - Exports
    - Configuration changes
    """
    
    def __init__(self, storage_path: str = "storage/audit"):
        self.storage_path = storage_path
        os.makedirs(storage_path, exist_ok=True)
        self.logs: List[AuditEntry] = []
        self._lock = threading.Lock()
    
    def log(
        self,
        user_id: str,
        action: str,
        resource: str,
        details: Dict[str, Any] = None,
        ip_address: str = None,
        success: bool = True
    ):
        """Log an action"""
        entry = AuditEntry(
            timestamp=datetime.now().isoformat(),
            user_id=user_id,
            action=action,
            resource=resource,
            details=details or {},
            ip_address=ip_address,
            success=success
        )
        
        with self._lock:
            self.logs.append(entry)
            
            # Persist every 100 entries
            if len(self.logs) >= 100:
                self._persist_logs()
    
    def _persist_logs(self):
        """Persist logs to disk"""
        if not self.logs:
            return
        
        filename = f"audit_{datetime.now().strftime('%Y%m%d')}.jsonl"
        filepath = os.path.join(self.storage_path, filename)
        
        with open(filepath, 'a') as f:
            for entry in self.logs:
                f.write(json.dumps({
                    "timestamp": entry.timestamp,
                    "user_id": entry.user_id,
                    "action": entry.action,
                    "resource": entry.resource,
                    "details": entry.details,
                    "ip_address": entry.ip_address,
                    "success": entry.success
                }) + "\n")
        
        self.logs = []
    
    def get_user_logs(
        self,
        user_id: str,
        start_date: datetime = None,
        end_date: datetime = None,
        action_filter: str = None
    ) -> List[Dict]:
        """Get logs for a user"""
        results = []
        
        for entry in self.logs:
            if entry.user_id != user_id:
                continue
            
            if action_filter and action_filter not in entry.action:
                continue
            
            entry_time = datetime.fromisoformat(entry.timestamp)
            if start_date and entry_time < start_date:
                continue
            if end_date and entry_time > end_date:
                continue
            
            results.append({
                "timestamp": entry.timestamp,
                "action": entry.action,
                "resource": entry.resource,
                "success": entry.success
            })
        
        return results
    
    def flush(self):
        """Force persist all logs"""
        with self._lock:
            self._persist_logs()


# =============================================================================
# RATE LIMITER β€” Delegated to core.rate_limiter
# =============================================================================
# The unified rate limiter lives in core/rate_limiter.py (Redis + in-memory).
# This module re-exports a convenience function for backward compatibility.



# =============================================================================
# MULTI-USER SESSION MANAGER
# =============================================================================

@dataclass
class UserSession:
    """User session data"""
    user_id: str
    session_id: str
    created_at: datetime
    last_activity: datetime
    metadata: Dict[str, Any] = field(default_factory=dict)


class SessionManager:
    """
    πŸ‘₯ Multi-User Session Manager
    
    Manage user sessions:
    - Session creation/validation
    - Activity tracking
    - Session cleanup
    """
    
    def __init__(self, session_timeout_minutes: int = 60):
        self.session_timeout = timedelta(minutes=session_timeout_minutes)
        self.sessions: Dict[str, UserSession] = {}
        self._lock = threading.Lock()
    
    def create_session(self, user_id: str, metadata: Dict = None) -> str:
        """Create a new session"""
        session_id = hashlib.sha256(
            f"{user_id}{datetime.now().isoformat()}{os.urandom(16).hex()}".encode()
        ).hexdigest()[:32]
        
        session = UserSession(
            user_id=user_id,
            session_id=session_id,
            created_at=datetime.now(),
            last_activity=datetime.now(),
            metadata=metadata or {}
        )
        
        with self._lock:
            self.sessions[session_id] = session
        
        return session_id
    
    def validate_session(self, session_id: str) -> Optional[UserSession]:
        """Validate and refresh a session"""
        with self._lock:
            session = self.sessions.get(session_id)
            
            if not session:
                return None
            
            # Check timeout
            if datetime.now() - session.last_activity > self.session_timeout:
                del self.sessions[session_id]
                return None
            
            # Refresh activity
            session.last_activity = datetime.now()
            return session
    
    def end_session(self, session_id: str) -> bool:
        """End a session"""
        with self._lock:
            if session_id in self.sessions:
                del self.sessions[session_id]
                return True
            return False
    
    def get_active_sessions(self, user_id: str) -> List[Dict]:
        """Get all active sessions for a user"""
        results = []
        now = datetime.now()
        
        with self._lock:
            for sid, session in list(self.sessions.items()):
                if session.user_id != user_id:
                    continue
                
                if now - session.last_activity > self.session_timeout:
                    del self.sessions[sid]
                    continue
                
                results.append({
                    "session_id": sid,
                    "created_at": session.created_at.isoformat(),
                    "last_activity": session.last_activity.isoformat()
                })
        
        return results
    
    def cleanup_expired(self):
        """Remove all expired sessions"""
        now = datetime.now()
        
        with self._lock:
            expired = [
                sid for sid, session in self.sessions.items()
                if now - session.last_activity > self.session_timeout
            ]
            
            for sid in expired:
                del self.sessions[sid]
            
            return len(expired)


# =============================================================================
# EXPORTS
# =============================================================================

action_engine = ActionEngine()
audit_logger = AuditLogger()
# rate_limiter β€” now managed by core.rate_limiter module
session_manager = SessionManager()


async def export_data(
    data: Any,
    format: str,  # csv, excel, pdf
    user_id: str,
    config: Dict = None
) -> Dict[str, Any]:
    """Quick function to export data"""
    action_map = {
        "csv": ActionType.EXPORT_CSV,
        "excel": ActionType.EXPORT_EXCEL,
        "pdf": ActionType.EXPORT_PDF
    }
    
    action_type = action_map.get(format.lower(), ActionType.EXPORT_CSV)
    result = await action_engine.execute(action_type, data, config or {}, user_id)
    
    return {
        "success": result.success,
        "message": result.message,
        "path": result.output_path
    }


def log_action(
    user_id: str,
    action: str,
    resource: str,
    details: Dict = None
):
    """Quick function to log an action"""
    audit_logger.log(user_id, action, resource, details)


def check_rate_limit(user_id: str) -> Dict[str, Any]:
    """Quick synchronous rate limit check (delegates to core.rate_limiter)."""
    from core.rate_limiter import get_rate_limiter, RATE_LIMITS
    import asyncio
    import time

    limiter = get_rate_limiter()
    limits = RATE_LIMITS["default"]
    key = f"user:{user_id}:default"

    try:
        loop = asyncio.get_event_loop()
        if loop.is_running():
            # Can't await in a sync context inside a running loop
            return {"allowed": True, "retry_after": None}
        is_limited, remaining, retry_after = loop.run_until_complete(
            limiter.is_rate_limited(key, limits["max_requests"], limits["window_seconds"])
        )
    except RuntimeError:
        return {"allowed": True, "retry_after": None}

    return {
        "allowed": not is_limited,
        "retry_after": retry_after if is_limited else None,
        "requests_last_minute": limits["max_requests"] - remaining,
        "limit_per_minute": limits["max_requests"]
    }