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
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
"""
Structured Logging Configuration - Rooting Future Strategy Engine v6.0

STAB-004: JSON structured logs for debugging and monitoring.

Features:
- JSON output for easy parsing and log aggregation
- Contextual logging with automatic metadata
- Performance timing helpers
- Error tracking with support codes
- Console output with colors (dev) vs JSON (prod)

Usage:
    from utils.logging_config import get_logger, log_event, timed_operation

    logger = get_logger(__name__)
    logger.info("plan_generated", plan_id=plan_id, duration=12.5)

    # Or use convenience functions
    log_event("plan_generated", plan_id=plan_id)

    # Timed operations
    with timed_operation("pdf_export", plan_id=plan_id):
        generate_pdf(...)
"""

import os
import sys
import time
import logging
from datetime import datetime
from typing import Any, Optional
from contextlib import contextmanager
from functools import wraps

import structlog
from structlog.stdlib import BoundLogger

# =============================================================================
# CONFIGURATION
# =============================================================================

# Environment: "development" or "production"
ENV = os.environ.get("ROOTING_ENV", "development")
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper()
LOG_FILE = os.environ.get("LOG_FILE", "logs/rooting_future.log")

# Ensure logs directory exists
LOG_DIR = os.path.dirname(LOG_FILE) if LOG_FILE else "logs"
if LOG_DIR:
    os.makedirs(LOG_DIR, exist_ok=True)


# =============================================================================
# PROCESSORS
# =============================================================================

def add_app_context(logger, method_name, event_dict):
    """Add application context to every log entry."""
    event_dict["app"] = "rooting_future"
    event_dict["version"] = "6.0"
    event_dict["env"] = ENV
    return event_dict


def add_timestamp(logger, method_name, event_dict):
    """Add ISO timestamp to every log entry."""
    event_dict["timestamp"] = datetime.now().isoformat()
    return event_dict


def censor_sensitive_data(logger, method_name, event_dict):
    """Remove or mask sensitive data from logs."""
    sensitive_keys = {"password", "api_key", "token", "secret", "credential"}

    for key in list(event_dict.keys()):
        if any(s in key.lower() for s in sensitive_keys):
            event_dict[key] = "***REDACTED***"

    return event_dict


# =============================================================================
# STRUCTLOG CONFIGURATION
# =============================================================================

def configure_structlog():
    """Configure structlog with appropriate processors for the environment."""

    # Common processors
    shared_processors = [
        structlog.stdlib.add_log_level,
        structlog.stdlib.add_logger_name,
        structlog.processors.TimeStamper(fmt="iso"),
        structlog.processors.StackInfoRenderer(),
        add_app_context,
        censor_sensitive_data,
    ]

    if ENV == "development":
        # Development: colored console output
        processors = shared_processors + [
            structlog.dev.ConsoleRenderer(colors=True)
        ]
    else:
        # Production: JSON output
        processors = shared_processors + [
            structlog.processors.format_exc_info,
            structlog.processors.JSONRenderer()
        ]

    structlog.configure(
        processors=processors,
        wrapper_class=structlog.stdlib.BoundLogger,
        context_class=dict,
        logger_factory=structlog.stdlib.LoggerFactory(),
        cache_logger_on_first_use=True,
    )

    # Configure standard library logging
    logging.basicConfig(
        format="%(message)s",
        stream=sys.stdout,
        level=getattr(logging, LOG_LEVEL, logging.INFO),
    )

    # Add file handler if specified
    if LOG_FILE:
        file_handler = logging.FileHandler(LOG_FILE, encoding="utf-8")
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(logging.Formatter("%(message)s"))
        logging.getLogger().addHandler(file_handler)


# Initialize on import
configure_structlog()


# =============================================================================
# LOGGER FACTORY
# =============================================================================

def get_logger(name: str = None) -> BoundLogger:
    """
    Get a structured logger instance.

    Args:
        name: Logger name (usually __name__)

    Returns:
        Structured logger instance

    Usage:
        logger = get_logger(__name__)
        logger.info("event_name", key="value")
    """
    return structlog.get_logger(name or "rooting_future")


# =============================================================================
# CONVENIENCE FUNCTIONS
# =============================================================================

# Global logger for convenience functions
_logger = get_logger("rooting_future")


def log_event(
    event: str,
    level: str = "info",
    **kwargs: Any
) -> None:
    """
    Log a structured event.

    Args:
        event: Event name (e.g., "plan_generated", "pdf_export_started")
        level: Log level (debug, info, warning, error)
        **kwargs: Additional context data

    Usage:
        log_event("plan_generated", plan_id="plan_123", duration=12.5)
    """
    log_func = getattr(_logger, level, _logger.info)
    log_func(event, **kwargs)


def log_error(
    event: str,
    error: Exception = None,
    error_id: str = None,
    **kwargs: Any
) -> None:
    """
    Log an error event with exception details.

    Args:
        event: Error event name
        error: Exception object (optional)
        error_id: Support code (optional)
        **kwargs: Additional context

    Usage:
        log_error("pdf_generation_failed", error=e, plan_id=plan_id)
    """
    if error:
        kwargs["error_type"] = type(error).__name__
        kwargs["error_message"] = str(error)
    if error_id:
        kwargs["error_id"] = error_id
        kwargs["support_code"] = f"RF-{error_id}"

    _logger.error(event, **kwargs, exc_info=bool(error))


def log_api_request(
    endpoint: str,
    method: str,
    user_id: int = None,
    **kwargs: Any
) -> None:
    """
    Log an API request.

    Args:
        endpoint: API endpoint path
        method: HTTP method
        user_id: User ID if authenticated
        **kwargs: Additional context
    """
    _logger.info(
        "api_request",
        endpoint=endpoint,
        method=method,
        user_id=user_id,
        **kwargs
    )


def log_api_response(
    endpoint: str,
    status_code: int,
    duration_ms: float = None,
    **kwargs: Any
) -> None:
    """
    Log an API response.

    Args:
        endpoint: API endpoint path
        status_code: HTTP status code
        duration_ms: Request duration in milliseconds
        **kwargs: Additional context
    """
    level = "info" if status_code < 400 else "warning" if status_code < 500 else "error"
    log_func = getattr(_logger, level)
    log_func(
        "api_response",
        endpoint=endpoint,
        status_code=status_code,
        duration_ms=duration_ms,
        **kwargs
    )


# =============================================================================
# TIMING HELPERS
# =============================================================================

@contextmanager
def timed_operation(operation: str, **context: Any):
    """
    Context manager for timing operations.

    Args:
        operation: Operation name for logging
        **context: Additional context to log

    Usage:
        with timed_operation("pdf_export", plan_id=plan_id):
            generate_pdf(...)

    Yields:
        dict with "start_time" that gets populated with "duration_seconds" on exit
    """
    start_time = time.time()
    timing = {"start_time": start_time}

    _logger.info(f"{operation}_started", **context)

    try:
        yield timing
        duration = time.time() - start_time
        timing["duration_seconds"] = duration
        _logger.info(
            f"{operation}_completed",
            duration_seconds=round(duration, 3),
            **context
        )
    except Exception as e:
        duration = time.time() - start_time
        timing["duration_seconds"] = duration
        _logger.error(
            f"{operation}_failed",
            duration_seconds=round(duration, 3),
            error_type=type(e).__name__,
            error_message=str(e),
            **context
        )
        raise


def timed(operation: str = None):
    """
    Decorator for timing function execution.

    Args:
        operation: Operation name (defaults to function name)

    Usage:
        @timed("plan_generation")
        def generate_plan(club_data):
            ...
    """
    def decorator(func):
        op_name = operation or func.__name__

        @wraps(func)
        def wrapper(*args, **kwargs):
            with timed_operation(op_name):
                return func(*args, **kwargs)
        return wrapper
    return decorator


# =============================================================================
# PLAN-SPECIFIC LOGGING
# =============================================================================

def log_plan_generation_started(
    plan_id: str,
    club_name: str,
    user_id: int = None,
    stakeholder_count: int = 0,
    **kwargs: Any
) -> None:
    """Log the start of plan generation."""
    _logger.info(
        "plan_generation_started",
        plan_id=plan_id,
        club_name=club_name,
        user_id=user_id,
        stakeholder_count=stakeholder_count,
        **kwargs
    )


def log_plan_generation_completed(
    plan_id: str,
    club_name: str,
    duration_seconds: float,
    agent_count: int = 6,
    sections_count: int = 0,
    **kwargs: Any
) -> None:
    """Log successful plan generation."""
    _logger.info(
        "plan_generation_completed",
        plan_id=plan_id,
        club_name=club_name,
        duration_seconds=round(duration_seconds, 2),
        agent_count=agent_count,
        sections_count=sections_count,
        **kwargs
    )


def log_agent_execution(
    agent_name: str,
    plan_id: str,
    duration_seconds: float,
    success: bool = True,
    **kwargs: Any
) -> None:
    """Log individual agent execution."""
    level = "info" if success else "warning"
    log_func = getattr(_logger, level)
    log_func(
        "agent_execution",
        agent_name=agent_name,
        plan_id=plan_id,
        duration_seconds=round(duration_seconds, 2),
        success=success,
        **kwargs
    )


def log_export_event(
    export_type: str,
    plan_id: str,
    success: bool,
    duration_seconds: float = None,
    file_size_kb: int = None,
    engine: str = None,
    **kwargs: Any
) -> None:
    """Log export events (PDF, DOCX, HTML)."""
    level = "info" if success else "error"
    log_func = getattr(_logger, level)

    data = {
        "export_type": export_type,
        "plan_id": plan_id,
        "success": success,
    }
    if duration_seconds is not None:
        data["duration_seconds"] = round(duration_seconds, 2)
    if file_size_kb is not None:
        data["file_size_kb"] = file_size_kb
    if engine:
        data["engine"] = engine

    log_func("export_event", **data, **kwargs)


def log_share_event(
    event_type: str,  # "created", "accessed", "revoked"
    plan_id: str,
    share_token: str = None,
    user_id: int = None,
    **kwargs: Any
) -> None:
    """Log sharing events."""
    _logger.info(
        f"share_{event_type}",
        plan_id=plan_id,
        share_token=share_token[:8] + "..." if share_token else None,
        user_id=user_id,
        **kwargs
    )


# =============================================================================
# EXPORT FOR CONVENIENCE
# =============================================================================

__all__ = [
    "get_logger",
    "log_event",
    "log_error",
    "log_api_request",
    "log_api_response",
    "timed_operation",
    "timed",
    "log_plan_generation_started",
    "log_plan_generation_completed",
    "log_agent_execution",
    "log_export_event",
    "log_share_event",
]