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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
import logging
import sqlite3
from pathlib import Path
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, asdict
from datetime import datetime
import json
import os 
import sys 



sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.document_processor import ProcessingStatus, DocumentType


try:
    from logger.custom_logger import CustomLoggerTracker
    custom_log = CustomLoggerTracker()
    logger = custom_log.get_logger("meta_manager")

except ImportError:
    # Fallback to standard logging if custom logger not available
    logger = logging.getLogger("meta_manager")


@dataclass
class DocumentMetadata:
    """Metadata for a processed document."""
    document_id: str
    filename: str
    file_path: str
    file_type: str
    upload_timestamp: datetime
    processing_status: ProcessingStatus
    total_chunks: int
    file_size: int
    checksum: str
    error_message: Optional[str] = None
    processing_time: Optional[float] = None
    metadata_json: Optional[str] = None  # Additional metadata as JSON


@dataclass
class CitationInfo:
    """Citation information for a document chunk."""
    chunk_id: str
    document_id: str
    source_document: str
    location_reference: str
    extraction_method: str
    confidence_level: float
    page_number: Optional[int] = None
    worksheet_name: Optional[str] = None
    cell_range: Optional[str] = None
    section_title: Optional[str] = None


class MetadataManager:
    """
    SQLite-based metadata manager for document tracking and citation management.
    
    This manager provides persistent storage for document metadata, processing status,
    and citation information with efficient querying capabilities.
    """
    
    def __init__(self, config: Dict[str, Any]):
        """
        Initialize the metadata manager.
        
        Args:
            config: Configuration dictionary containing database settings
        """
        self.config = config
        self.db_path = config.get('metadata_db_path', './data/metadata.db')
        
        # Ensure database directory exists
        Path(self.db_path).parent.mkdir(parents=True, exist_ok=True)
        
        # Initialize database
        self._init_database()
        
        logger.info(f"Metadata manager initialized with database: {self.db_path}")
    
    def _init_database(self):
        """Initialize the SQLite database with required tables."""
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                # Create documents table
                cursor.execute('''
                    CREATE TABLE IF NOT EXISTS documents (
                        document_id TEXT PRIMARY KEY,
                        filename TEXT NOT NULL,
                        file_path TEXT NOT NULL,
                        file_type TEXT NOT NULL,
                        upload_timestamp TEXT NOT NULL,
                        processing_status TEXT NOT NULL,
                        total_chunks INTEGER DEFAULT 0,
                        file_size INTEGER DEFAULT 0,
                        checksum TEXT,
                        error_message TEXT,
                        processing_time REAL,
                        metadata_json TEXT,
                        created_at TEXT DEFAULT CURRENT_TIMESTAMP,
                        updated_at TEXT DEFAULT CURRENT_TIMESTAMP
                    )
                ''')
                
                # Create citations table
                cursor.execute('''
                    CREATE TABLE IF NOT EXISTS citations (
                        id INTEGER PRIMARY KEY AUTOINCREMENT,
                        chunk_id TEXT NOT NULL,
                        document_id TEXT NOT NULL,
                        source_document TEXT NOT NULL,
                        location_reference TEXT NOT NULL,
                        extraction_method TEXT NOT NULL,
                        confidence_level REAL NOT NULL,
                        page_number INTEGER,
                        worksheet_name TEXT,
                        cell_range TEXT,
                        section_title TEXT,
                        created_at TEXT DEFAULT CURRENT_TIMESTAMP,
                        FOREIGN KEY (document_id) REFERENCES documents (document_id)
                    )
                ''')
                
                # Create indexes for efficient querying
                cursor.execute('CREATE INDEX IF NOT EXISTS idx_documents_status ON documents (processing_status)')
                cursor.execute('CREATE INDEX IF NOT EXISTS idx_documents_type ON documents (file_type)')
                cursor.execute('CREATE INDEX IF NOT EXISTS idx_citations_document ON citations (document_id)')
                cursor.execute('CREATE INDEX IF NOT EXISTS idx_citations_chunk ON citations (chunk_id)')
                
                conn.commit()
                logger.debug("Database tables initialized successfully")
                
        except Exception as e:
            logger.error(f"Failed to initialize database: {e}")
            raise
    
    def store_document_metadata(self, doc_id: str, metadata: DocumentMetadata) -> bool:
        """
        Store document metadata in the database.
        
        Args:
            doc_id: Document ID
            metadata: DocumentMetadata object
            
        Returns:
            True if successful, False otherwise
        """
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                # Convert datetime to ISO string
                upload_timestamp = metadata.upload_timestamp.isoformat()
                
                cursor.execute('''
                    INSERT OR REPLACE INTO documents (
                        document_id, filename, file_path, file_type, upload_timestamp,
                        processing_status, total_chunks, file_size, checksum,
                        error_message, processing_time, metadata_json, updated_at
                    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                ''', (
                    doc_id,
                    metadata.filename,
                    metadata.file_path,
                    metadata.file_type,
                    upload_timestamp,
                    metadata.processing_status.value,
                    metadata.total_chunks,
                    metadata.file_size,
                    metadata.checksum,
                    metadata.error_message,
                    metadata.processing_time,
                    metadata.metadata_json,
                    datetime.now().isoformat()
                ))
                
                conn.commit()
                logger.debug(f"Stored metadata for document: {doc_id}")
                return True
                
        except Exception as e:
            logger.error(f"Failed to store document metadata: {e}")
            return False
    
    def get_document_metadata(self, doc_id: str) -> Optional[DocumentMetadata]:
        """
        Retrieve document metadata by ID.
        
        Args:
            doc_id: Document ID
            
        Returns:
            DocumentMetadata object or None if not found
        """
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                cursor.execute('''
                    SELECT document_id, filename, file_path, file_type, upload_timestamp,
                           processing_status, total_chunks, file_size, checksum,
                           error_message, processing_time, metadata_json
                    FROM documents WHERE document_id = ?
                ''', (doc_id,))
                
                row = cursor.fetchone()
                if row:
                    return DocumentMetadata(
                        document_id=row[0],
                        filename=row[1],
                        file_path=row[2],
                        file_type=row[3],
                        upload_timestamp=datetime.fromisoformat(row[4]),
                        processing_status=ProcessingStatus(row[5]),
                        total_chunks=row[6],
                        file_size=row[7],
                        checksum=row[8],
                        error_message=row[9],
                        processing_time=row[10],
                        metadata_json=row[11]
                    )
                
                return None
                
        except Exception as e:
            logger.error(f"Failed to get document metadata: {e}")
            return None
    
    def update_document_status(self, doc_id: str, status: ProcessingStatus, 
                              error_message: Optional[str] = None,
                              processing_time: Optional[float] = None) -> bool:
        """
        Update document processing status.
        
        Args:
            doc_id: Document ID
            status: New processing status
            error_message: Optional error message
            processing_time: Optional processing time
            
        Returns:
            True if successful, False otherwise
        """
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                cursor.execute('''
                    UPDATE documents 
                    SET processing_status = ?, error_message = ?, processing_time = ?, updated_at = ?
                    WHERE document_id = ?
                ''', (
                    status.value,
                    error_message,
                    processing_time,
                    datetime.now().isoformat(),
                    doc_id
                ))
                
                conn.commit()
                logger.debug(f"Updated status for document {doc_id}: {status.value}")
                return True
                
        except Exception as e:
            logger.error(f"Failed to update document status: {e}")
            return False
    
    def store_citation_info(self, citation: CitationInfo) -> bool:
        """
        Store citation information.
        
        Args:
            citation: CitationInfo object
            
        Returns:
            True if successful, False otherwise
        """
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                cursor.execute('''
                    INSERT OR REPLACE INTO citations (
                        chunk_id, document_id, source_document, location_reference,
                        extraction_method, confidence_level, page_number,
                        worksheet_name, cell_range, section_title
                    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                ''', (
                    citation.chunk_id,
                    citation.document_id,
                    citation.source_document,
                    citation.location_reference,
                    citation.extraction_method,
                    citation.confidence_level,
                    citation.page_number,
                    citation.worksheet_name,
                    citation.cell_range,
                    citation.section_title
                ))
                
                conn.commit()
                logger.debug(f"Stored citation for chunk: {citation.chunk_id}")
                return True
                
        except Exception as e:
            logger.error(f"Failed to store citation info: {e}")
            return False
    
    def get_citation_info(self, chunk_id: str) -> Optional[CitationInfo]:
        """
        Retrieve citation information by chunk ID.
        
        Args:
            chunk_id: Chunk ID
            
        Returns:
            CitationInfo object or None if not found
        """
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                cursor.execute('''
                    SELECT chunk_id, document_id, source_document, location_reference,
                           extraction_method, confidence_level, page_number,
                           worksheet_name, cell_range, section_title
                    FROM citations WHERE chunk_id = ?
                ''', (chunk_id,))
                
                row = cursor.fetchone()
                if row:
                    return CitationInfo(
                        chunk_id=row[0],
                        document_id=row[1],
                        source_document=row[2],
                        location_reference=row[3],
                        extraction_method=row[4],
                        confidence_level=row[5],
                        page_number=row[6],
                        worksheet_name=row[7],
                        cell_range=row[8],
                        section_title=row[9]
                    )
                
                return None
                
        except Exception as e:
            logger.error(f"Failed to get citation info: {e}")
            return None
    
    def list_documents(self, status: Optional[ProcessingStatus] = None,
                      file_type: Optional[str] = None,
                      limit: int = 100) -> List[DocumentMetadata]:
        
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                query = '''
                    SELECT document_id, filename, file_path, file_type, upload_timestamp,
                           processing_status, total_chunks, file_size, checksum,
                           error_message, processing_time, metadata_json
                    FROM documents
                '''
                
                conditions = []
                params = []
                
                if status:
                    conditions.append('processing_status = ?')
                    params.append(status.value)
                
                if file_type:
                    conditions.append('file_type = ?')
                    params.append(file_type)
                
                if conditions:
                    query += ' WHERE ' + ' AND '.join(conditions)
                
                query += ' ORDER BY upload_timestamp DESC LIMIT ?'
                params.append(limit)
                
                cursor.execute(query, params)
                rows = cursor.fetchall()
                
                documents = []
                for row in rows:
                    documents.append(DocumentMetadata(
                        document_id=row[0],
                        filename=row[1],
                        file_path=row[2],
                        file_type=row[3],
                        upload_timestamp=datetime.fromisoformat(row[4]),
                        processing_status=ProcessingStatus(row[5]),
                        total_chunks=row[6],
                        file_size=row[7],
                        checksum=row[8],
                        error_message=row[9],
                        processing_time=row[10],
                        metadata_json=row[11]
                    ))
                
                return documents
                
        except Exception as e:
            logger.error(f"Failed to list documents: {e}")
            return []
    
    def delete_document(self, doc_id: str) -> bool:
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                # Delete citations first (foreign key constraint)
                cursor.execute('DELETE FROM citations WHERE document_id = ?', (doc_id,))
                
                # Delete document
                cursor.execute('DELETE FROM documents WHERE document_id = ?', (doc_id,))
                
                conn.commit()
                logger.info(f"Deleted document and citations: {doc_id}")
                return True
                
        except Exception as e:
            logger.error(f"Failed to delete document: {e}")
            return False
    
    def get_statistics(self) -> Dict[str, Any]:
        """
        Get database statistics.
        
        Returns:
            Dictionary with database statistics
        """
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                # Count documents by status
                cursor.execute('''
                    SELECT processing_status, COUNT(*) 
                    FROM documents 
                    GROUP BY processing_status
                ''')
                status_counts = dict(cursor.fetchall())
                
                # Count documents by type
                cursor.execute('''
                    SELECT file_type, COUNT(*) 
                    FROM documents 
                    GROUP BY file_type
                ''')
                type_counts = dict(cursor.fetchall())
                
                # Total statistics
                cursor.execute('SELECT COUNT(*) FROM documents')
                total_documents = cursor.fetchone()[0]
                
                cursor.execute('SELECT COUNT(*) FROM citations')
                total_citations = cursor.fetchone()[0]
                
                cursor.execute('SELECT SUM(total_chunks) FROM documents')
                total_chunks = cursor.fetchone()[0] or 0
                
                cursor.execute('SELECT SUM(file_size) FROM documents')
                total_file_size = cursor.fetchone()[0] or 0
                
                return {
                    'total_documents': total_documents,
                    'total_citations': total_citations,
                    'total_chunks': total_chunks,
                    'total_file_size': total_file_size,
                    'documents_by_status': status_counts,
                    'documents_by_type': type_counts,
                    'database_path': self.db_path
                }
                
        except Exception as e:
            logger.error(f"Failed to get statistics: {e}")
            return {'error': str(e)}
    
    def cleanup_orphaned_citations(self) -> int:
        """
        Clean up citations that reference non-existent documents.
        
        Returns:
            Number of orphaned citations removed
        """
        try:
            with sqlite3.connect(self.db_path) as conn:
                cursor = conn.cursor()
                
                cursor.execute('''
                    DELETE FROM citations 
                    WHERE document_id NOT IN (SELECT document_id FROM documents)
                ''')
                
                removed_count = cursor.rowcount
                conn.commit()
                
                logger.info(f"Cleaned up {removed_count} orphaned citations")
                return removed_count
                
        except Exception as e:
            logger.error(f"Failed to cleanup orphaned citations: {e}")
            return 0



if __name__=="__main__":
    logger.info(f"metadata init ..")