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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
"""

Production-grade Django REST Framework views with comprehensive error handling,

validation, logging, and proper HTTP status codes.

"""
import logging
import os
from typing import Any, Dict

from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.parsers import FormParser, JSONParser, MultiPartParser
from rest_framework.response import Response
from rest_framework.views import APIView

from solar_api.services.chatbot_service import (
    get_chatbot_response,
    APIKeyMissingError,
    EmbeddingError,
    DatabaseError,
    LLMError,
)
from solar_api.services.pdf_ingestion_service import (
    ingest_pdf,
    delete_tenant_knowledge_base,
    PDFExtractionError,
    InsufficientContentError,
    PDFIngestionError,
)

# =====================================================
# LOGGING SETUP
# =====================================================
logger = logging.getLogger(__name__)

# =====================================================
# VALIDATION HELPERS
# =====================================================
def validate_pdf_file(pdf_file: Any) -> Dict[str, Any]:
    """

    Validate uploaded PDF file.

    

    Args:

        pdf_file: Uploaded file object

        

    Returns:

        Dict with validation result

    """
    if not pdf_file:
        return {'valid': False, 'error': 'PDF file is required'}
    
    # Check file extension
    if not pdf_file.name.lower().endswith('.pdf'):
        return {'valid': False, 'error': 'File must be a PDF'}
    
    # Check file size (limit to 10MB)
    max_size = 10 * 1024 * 1024  # 10MB
    if pdf_file.size > max_size:
        return {'valid': False, 'error': f'File size exceeds maximum of {max_size / 1024 / 1024}MB'}
    
    return {'valid': True}


def validate_tenant_id(tenant_id: str) -> Dict[str, Any]:
    """

    Validate tenant_id parameter.

    

    Args:

        tenant_id: Tenant identifier

        

    Returns:

        Dict with validation result

    """
    if not tenant_id:
        return {'valid': False, 'error': 'tenant_id is required'}
    
    if not tenant_id.strip():
        return {'valid': False, 'error': 'tenant_id cannot be empty'}
    
    # Additional validation: alphanumeric + underscore/hyphen only
    if not all(c.isalnum() or c in ('_', '-') for c in tenant_id):
        return {'valid': False, 'error': 'tenant_id can only contain letters, numbers, underscores, and hyphens'}
    
    return {'valid': True}


def validate_question(question: str) -> Dict[str, Any]:
    """

    Validate question parameter.

    

    Args:

        question: User's question

        

    Returns:

        Dict with validation result

    """
    if not question:
        return {'valid': False, 'error': 'question is required'}
    
    if not question.strip():
        return {'valid': False, 'error': 'question cannot be empty'}
    
    # Check length limits
    if len(question) > 1000:
        return {'valid': False, 'error': 'question exceeds maximum length of 1000 characters'}
    
    if len(question.strip()) < 3:
        return {'valid': False, 'error': 'question must be at least 3 characters'}
    
    return {'valid': True}


# =====================================================
# API VIEWS
# =====================================================
class PDFIngestionAPIView(APIView):
    """

    Production-grade API endpoint for PDF ingestion.

    

    Features:

    - Input validation with clear error messages

    - Proper error handling with appropriate HTTP status codes

    - Structured logging for debugging

    - Temporary file cleanup

    - Transaction safety

    """
    parser_classes = [MultiPartParser, FormParser]
    
    @swagger_auto_schema(

        operation_description="""Upload a PDF file to ingest its content into the vector database.

        

The PDF will be:

1. Validated for format and size

2. Text extracted and cleaned

3. Chunked with metadata

4. Embedded in batches

5. Stored in vector database



Maximum file size: 10MB

Supported format: PDF only""",

        manual_parameters=[

            openapi.Parameter(

                'pdf_file',

                openapi.IN_FORM,

                type=openapi.TYPE_FILE,

                required=True,

                description='PDF file to upload and ingest (max 10MB)'

            ),

            openapi.Parameter(

                'tenant_id',

                openapi.IN_FORM,

                type=openapi.TYPE_STRING,

                required=True,

                description='Tenant identifier (alphanumeric, underscores, hyphens only)'

            ),

        ],

        responses={

            200: openapi.Response(

                description='PDF ingested successfully',

                schema=openapi.Schema(

                    type=openapi.TYPE_OBJECT,

                    properties={

                        'message': openapi.Schema(type=openapi.TYPE_STRING),

                        'file_name': openapi.Schema(type=openapi.TYPE_STRING),

                        'tenant_id': openapi.Schema(type=openapi.TYPE_STRING),

                        'chunks_generated': openapi.Schema(type=openapi.TYPE_INTEGER),

                        'chunks_inserted': openapi.Schema(type=openapi.TYPE_INTEGER),

                        'text_length': openapi.Schema(type=openapi.TYPE_INTEGER),

                    }

                )

            ),

            400: openapi.Response(

                description='Bad request - validation failed',

                schema=openapi.Schema(

                    type=openapi.TYPE_OBJECT,

                    properties={

                        'error': openapi.Schema(type=openapi.TYPE_STRING),

                        'details': openapi.Schema(type=openapi.TYPE_STRING),

                    }

                )

            ),

            422: openapi.Response(

                description='Unprocessable entity - PDF content issues',

                schema=openapi.Schema(

                    type=openapi.TYPE_OBJECT,

                    properties={

                        'error': openapi.Schema(type=openapi.TYPE_STRING),

                    }

                )

            ),

            500: openapi.Response(description='Internal server error'),

        },

        tags=['PDF Ingestion']

    )
    def post(self, request):
        """Handle PDF upload and ingestion."""
        temp_file_path = None
        
        try:
            # Extract parameters
            pdf_file = request.FILES.get('pdf_file')
            tenant_id = request.data.get('tenant_id')
            
            logger.info(f"PDF ingestion request for tenant: {tenant_id}")
            
            # Validate tenant_id
            tenant_validation = validate_tenant_id(tenant_id)
            if not tenant_validation['valid']:
                logger.warning(f"Tenant validation failed: {tenant_validation['error']}")
                return Response(
                    {
                        'error': tenant_validation['error'],
                        'field': 'tenant_id'
                    },
                    status=status.HTTP_400_BAD_REQUEST
                )
            
            # Validate PDF file
            file_validation = validate_pdf_file(pdf_file)
            if not file_validation['valid']:
                logger.warning(f"File validation failed: {file_validation['error']}")
                return Response(
                    {
                        'error': file_validation['error'],
                        'field': 'pdf_file'
                    },
                    status=status.HTTP_400_BAD_REQUEST
                )
            
            try:
                # Save uploaded file temporarily
                file_path = default_storage.save(
                    f'temp_pdfs/{pdf_file.name}',
                    ContentFile(pdf_file.read())
                )
                temp_file_path = default_storage.path(file_path)
                logger.debug(f"Temporary file saved: {temp_file_path}")
                
            except Exception as e:
                logger.error(f"Failed to save uploaded file: {e}")
                return Response(
                    {'error': 'Failed to process uploaded file', 'details': str(e)},
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR
                )
            
            try:
                # Ingest PDF
                result = ingest_pdf(temp_file_path, tenant_id)
                
                # Handle skipped case (unchanged content)
                if result.get('status') == 'skipped':
                    logger.info(f"PDF skipped (unchanged): {pdf_file.name}")
                    return Response(
                        {
                            'message': 'PDF already ingested with same content (skipped)',
                            'file_name': pdf_file.name,
                            'tenant_id': tenant_id,
                            'status': 'skipped'
                        },
                        status=status.HTTP_200_OK
                    )
                
                # Success response
                logger.info(f"PDF ingestion successful: {pdf_file.name}")
                return Response(
                    {
                        'message': 'PDF ingested successfully',
                        'file_name': pdf_file.name,
                        'tenant_id': tenant_id,
                        'chunks_generated': result.get('chunks_generated', 0),
                        'chunks_inserted': result.get('chunks_inserted', 0),
                        'text_length': result.get('text_length', 0),
                    },
                    status=status.HTTP_200_OK
                )
                
            except InsufficientContentError as e:
                # PDF doesn't have enough text - HTTP 422 (Unprocessable Entity)
                logger.warning(f"PDF has insufficient content: {e}")
                return Response(
                    {'error': 'PDF contains insufficient text content', 'details': str(e)},
                    status=status.HTTP_422_UNPROCESSABLE_ENTITY
                )
                
            except PDFExtractionError as e:
                # PDF extraction failed - HTTP 422
                logger.error(f"PDF extraction failed: {e}")
                return Response(
                    {'error': 'Failed to extract text from PDF', 'details': str(e)},
                    status=status.HTTP_422_UNPROCESSABLE_ENTITY
                )
                
            except PDFIngestionError as e:
                # General ingestion error - HTTP 500
                logger.error(f"PDF ingestion error: {e}")
                return Response(
                    {'error': 'PDF ingestion failed', 'details': str(e)},
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR
                )
                
        except Exception as e:
            # Unexpected error
            logger.error(f"Unexpected error in PDF ingestion: {e}", exc_info=True)
            return Response(
                {'error': 'An unexpected error occurred', 'details': str(e)},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )
            
        finally:
            # Always clean up temporary file
            if temp_file_path and os.path.exists(temp_file_path):
                try:
                    os.remove(temp_file_path)
                    # Try to remove directory if empty
                    try:
                        os.rmdir(os.path.dirname(temp_file_path))
                    except OSError:
                        pass
                except Exception as e:
                    logger.warning(f"Failed to clean up temp file: {e}")


class ChatbotAPIView(APIView):
    """

    Production-grade chatbot API with comprehensive error handling.

    

    Features:

    - Input validation

    - Graceful error handling with user-friendly messages

    - Structured logging

    - Proper HTTP status codes

    - API key validation

    """
    parser_classes = [JSONParser]
    
    @swagger_auto_schema(

        operation_description="""Query the chatbot with a question.

        

The system will:

1. Validate input

2. Expand query with synonyms

3. Retrieve relevant context via hybrid search (vector + keyword)

4. Generate answer using LLM (Groq)



Note: Requires GROQ_API_KEY environment variable to be set.""",

        request_body=openapi.Schema(

            type=openapi.TYPE_OBJECT,

            required=['question', 'tenant_id'],

            properties={

                'question': openapi.Schema(

                    type=openapi.TYPE_STRING,

                    description='The question to ask (3-1000 characters)',

                    min_length=3,

                    max_length=1000

                ),

                'tenant_id': openapi.Schema(

                    type=openapi.TYPE_STRING,

                    description='Tenant identifier (alphanumeric, underscores, hyphens only)'

                ),

            },

        ),

        responses={

            200: openapi.Response(

                description='Chatbot response generated successfully',

                schema=openapi.Schema(

                    type=openapi.TYPE_OBJECT,

                    properties={

                        'question': openapi.Schema(type=openapi.TYPE_STRING),

                        'answer': openapi.Schema(type=openapi.TYPE_STRING),

                        'tenant_id': openapi.Schema(type=openapi.TYPE_STRING),

                    }

                )

            ),

            400: openapi.Response(

                description='Bad request - validation failed',

                schema=openapi.Schema(

                    type=openapi.TYPE_OBJECT,

                    properties={

                        'error': openapi.Schema(type=openapi.TYPE_STRING),

                        'field': openapi.Schema(type=openapi.TYPE_STRING),

                    }

                )

            ),

            503: openapi.Response(

                description='Service unavailable - external API issues',

                schema=openapi.Schema(

                    type=openapi.TYPE_OBJECT,

                    properties={

                        'error': openapi.Schema(type=openapi.TYPE_STRING),

                    }

                )

            ),

            500: openapi.Response(description='Internal server error'),

        },

        tags=['Chatbot']

    )
    def post(self, request):
        """Handle chatbot query."""
        try:
            # Extract parameters
            question = request.data.get('question')
            tenant_id = request.data.get('tenant_id')
            
            logger.info(f"Chatbot query for tenant: {tenant_id}")
            
            # Validate question
            question_validation = validate_question(question)
            if not question_validation['valid']:
                logger.warning(f"Question validation failed: {question_validation['error']}")
                return Response(
                    {
                        'error': question_validation['error'],
                        'field': 'question'
                    },
                    status=status.HTTP_400_BAD_REQUEST
                )
            
            # Validate tenant_id
            tenant_validation = validate_tenant_id(tenant_id)
            if not tenant_validation['valid']:
                logger.warning(f"Tenant validation failed: {tenant_validation['error']}")
                return Response(
                    {
                        'error': tenant_validation['error'],
                        'field': 'tenant_id'
                    },
                    status=status.HTTP_400_BAD_REQUEST
                )
            
            try:
                # Get chatbot response
                answer, error = get_chatbot_response(question, tenant_id)
                
                # Check if there was an internal error
                if error:
                    logger.warning(f"Chatbot service returned error: {error}")
                    # Still return 200 with user-friendly message
                    # The service already provides a good user-facing message
                
                return Response(
                    {
                        'question': question,
                        'answer': answer,
                        'tenant_id': tenant_id,
                    },
                    status=status.HTTP_200_OK
                )
                
            except APIKeyMissingError as e:
                # Configuration error - HTTP 503
                logger.error(f"API key missing: {e}")
                return Response(
                    {'error': 'Chatbot service is not properly configured. Please contact support.'},
                    status=status.HTTP_503_SERVICE_UNAVAILABLE
                )
                
            except (EmbeddingError, DatabaseError) as e:
                # Internal service errors - HTTP 500
                logger.error(f"Service error: {e}")
                return Response(
                    {'error': 'An internal error occurred processing your request.'},
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR
                )
                
            except LLMError as e:
                # External API error - HTTP 503
                logger.error(f"LLM API error: {e}")
                return Response(
                    {'error': str(e)},
                    status=status.HTTP_503_SERVICE_UNAVAILABLE
                )
                
        except Exception as e:
            # Unexpected error
            logger.error(f"Unexpected error in chatbot endpoint: {e}", exc_info=True)
            return Response(
                {'error': 'An unexpected error occurred'},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )


class DeleteKnowledgeBaseAPIView(APIView):
    """

    Production-grade knowledge base deletion API.

    

    Features:

    - Input validation

    - Transaction safety

    - Comprehensive logging

    - Clear status reporting

    """
    parser_classes = [JSONParser]
    
    @swagger_auto_schema(

        operation_description="""Delete all knowledge base data for a specific tenant.

        

⚠️ WARNING: This operation is irreversible!



The operation will:

1. Validate tenant_id

2. Delete all associated documents

3. Delete all associated pages

4. Commit changes in a transaction



Returns details about deleted items.""",

        request_body=openapi.Schema(

            type=openapi.TYPE_OBJECT,

            required=['tenant_id'],

            properties={

                'tenant_id': openapi.Schema(

                    type=openapi.TYPE_STRING,

                    description='Tenant identifier for which to delete all knowledge base data'

                ),

            },

        ),

        responses={

            200: openapi.Response(

                description='Knowledge base deleted successfully',

                schema=openapi.Schema(

                    type=openapi.TYPE_OBJECT,

                    properties={

                        'message': openapi.Schema(type=openapi.TYPE_STRING),

                        'tenant_id': openapi.Schema(type=openapi.TYPE_STRING),

                        'deleted_documents': openapi.Schema(type=openapi.TYPE_INTEGER),

                        'deleted_pages': openapi.Schema(type=openapi.TYPE_INTEGER),

                        'status': openapi.Schema(type=openapi.TYPE_STRING),

                    }

                )

            ),

            400: openapi.Response(description='Bad request - missing or invalid tenant_id'),

            404: openapi.Response(description='No knowledge base found for tenant'),

            500: openapi.Response(description='Internal server error'),

        },

        tags=['Knowledge Base Management']

    )
    def delete(self, request):
        """Handle knowledge base deletion."""
        try:
            # Extract tenant_id
            tenant_id = request.data.get('tenant_id')
            
            logger.info(f"Knowledge base deletion request for tenant: {tenant_id}")
            
            # Validate tenant_id
            tenant_validation = validate_tenant_id(tenant_id)
            if not tenant_validation['valid']:
                logger.warning(f"Tenant validation failed: {tenant_validation['error']}")
                return Response(
                    {
                        'error': tenant_validation['error'],
                        'field': 'tenant_id'
                    },
                    status=status.HTTP_400_BAD_REQUEST
                )
            
            try:
                # Delete knowledge base
                result = delete_tenant_knowledge_base(tenant_id)
                
                # Handle not found case
                if result.get('status') == 'not_found':
                    logger.warning(f"No knowledge base found for tenant: {tenant_id}")
                    return Response(
                        {
                            'message': f'No knowledge base found for tenant: {tenant_id}',
                            'tenant_id': tenant_id,
                            'status': 'not_found'
                        },
                        status=status.HTTP_404_NOT_FOUND
                    )
                
                # Success response
                logger.info(f"Knowledge base deleted for tenant: {tenant_id}")
                return Response(
                    {
                        'message': f'Knowledge base deleted successfully for tenant: {tenant_id}',
                        'tenant_id': tenant_id,
                        'deleted_documents': result.get('deleted_documents', 0),
                        'deleted_pages': result.get('deleted_pages', 0),
                        'status': 'success'
                    },
                    status=status.HTTP_200_OK
                )
                
            except Exception as e:
                logger.error(f"Knowledge base deletion failed: {e}", exc_info=True)
                return Response(
                    {'error': 'Failed to delete knowledge base', 'details': str(e)},
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR
                )
                
        except Exception as e:
            logger.error(f"Unexpected error in delete endpoint: {e}", exc_info=True)
            return Response(
                {'error': 'An unexpected error occurred'},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )