File size: 14,917 Bytes
60f8dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced18a1
60f8dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced18a1
 
60f8dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2a749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b679a05
fa2a749
ced18a1
 
fa2a749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60f8dbd
b679a05
 
ced18a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b679a05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced18a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datetime import datetime
import bcrypt
import re
from bson.objectid import ObjectId

class User:
    def __init__(self, db):
        self.collection = db['users']
        # Create unique index on email
        self.collection.create_index("email", unique=True)
    
    def validate_email(self, email):
        """Validate email format"""
        pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
        return re.match(pattern, email) is not None
    
    def validate_phone(self, phone):
        """Validate phone number (10 digits)"""
        pattern = r'^[0-9]{10}$'
        return re.match(pattern, phone.replace('-', '').replace(' ', '')) is not None
    
    def hash_password(self, password):
        """Hash password using bcrypt"""
        salt = bcrypt.gensalt()
        hashed = bcrypt.hashpw(password.encode('utf-8'), salt)
        return hashed
    
    def verify_password(self, password, hashed_password):
        """Verify password against hash"""
        return bcrypt.checkpw(password.encode('utf-8'), hashed_password)
    
    def create_user(self, name, email, phone, password, is_admin=False):
        """Create a new user"""
        try:
            # Validate inputs
            if not name or len(name.strip()) < 2:
                return {"success": False, "message": "Name must be at least 2 characters long"}
            
            if not self.validate_email(email):
                return {"success": False, "message": "Invalid email format"}
            
            if not self.validate_phone(phone):
                return {"success": False, "message": "Phone number must be 10 digits"}
            
            if not password or len(password) < 6:
                return {"success": False, "message": "Password must be at least 6 characters long"}
            
            # Check if user already exists
            if self.collection.find_one({"email": email.lower()}):
                return {"success": False, "message": "Email already registered"}
            
            # Create user document
            user_doc = {
                "name": name.strip(),
                "email": email.lower(),
                "phone": phone.replace('-', '').replace(' ', ''),
                "password": self.hash_password(password),
                "created_at": datetime.utcnow(),
                "updated_at": datetime.utcnow(),
                "is_active": True,
                "is_admin": is_admin
            }
            
            # Insert into database
            result = self.collection.insert_one(user_doc)
            
            return {
                "success": True,
                "message": "User registered successfully",
                "user_id": str(result.inserted_id)
            }
        
        except Exception as e:
            return {"success": False, "message": f"Error creating user: {str(e)}"}
    
    def get_user_by_email(self, email):
        """Retrieve user by email"""
        return self.collection.find_one({"email": email.lower()})
    
    def get_user_by_id(self, user_id):
        """Retrieve user by ID"""
        try:
            return self.collection.find_one({"_id": ObjectId(user_id)})
        except:
            return None

class Document:
    def __init__(self, db):
        self.collection = db['documents']
        # Create index on user_id for faster queries
        self.collection.create_index("user_id")
    
    def create_document(self, user_id, title, content, doc_type='text', original_filename=None):
        """Create a new document entry"""
        try:
            doc = {
                "user_id": ObjectId(user_id),
                "title": title,
                "content": content,
                "type": doc_type,  # 'text' or 'file'
                "original_filename": original_filename,
                "simplified_content": None,
                "summary": None,
                "created_at": datetime.utcnow(),
                "status": "original",  # original, simplified
                "admin_corrected": False
            }
            
            result = self.collection.insert_one(doc)
            
            return {
                "success": True,
                "message": "Document saved successfully",
                "document_id": str(result.inserted_id)
            }
        except Exception as e:
            return {"success": False, "message": f"Error saving document: {str(e)}"}
    
    def get_user_documents(self, user_id):
        """Get all documents for a specific user"""
        try:
            documents = list(self.collection.find({"user_id": ObjectId(user_id)}).sort("created_at", -1))
            for doc in documents:
                doc['_id'] = str(doc['_id'])
                doc['user_id'] = str(doc['user_id'])
            return documents
        except Exception as e:
            print(f"Error fetching documents: {e}")
            return []

    def get_document_by_id(self, doc_id):
        """Get a specific document by ID"""
        try:
            doc = self.collection.find_one({"_id": ObjectId(doc_id)})
            if doc:
                doc['_id'] = str(doc['_id'])
                doc['user_id'] = str(doc['user_id'])
            return doc
        except:
            return None

    def get_all_documents(self, page=1, per_page=20):
        """Get all documents (admin use) with pagination"""
        try:
            skip = (page - 1) * per_page
            documents = list(
                self.collection.find({})
                .sort("created_at", -1)
                .skip(skip)
                .limit(per_page)
            )
            for doc in documents:
                doc['_id'] = str(doc['_id'])
                doc['user_id'] = str(doc['user_id'])
                # Truncate content for listing
                if doc.get('content'):
                    doc['content_preview'] = doc['content'][:200] + ('...' if len(doc['content']) > 200 else '')
                else:
                    doc['content_preview'] = ''
                if doc.get('simplified_content'):
                    doc['simplified_preview'] = doc['simplified_content'][:200] + (
                        '...' if len(doc['simplified_content']) > 200 else '')
                else:
                    doc['simplified_preview'] = ''
            total = self.collection.count_documents({})
            return {"items": documents, "total": total, "page": page, "per_page": per_page}
        except Exception as e:
            print(f"Error fetching all documents: {e}")
            return {"items": [], "total": 0, "page": page, "per_page": per_page}

    def update_document_simplified(self, doc_id, simplified_content):
        """Update document with simplified content"""
        try:
            result = self.collection.update_one(
                {"_id": ObjectId(doc_id)},
                {"$set": {
                    "simplified_content": simplified_content,
                    "status": "simplified"
                }}
            )
            return result.modified_count > 0
        except Exception as e:
            print(f"Error updating simplified content: {e}")
            return False

    def update_document_summary(self, doc_id, summary_content):
        """Update document with summary"""
        try:
            result = self.collection.update_one(
                {"_id": ObjectId(doc_id)},
                {"$set": {
                    "summary": summary_content
                }}
            )
            return result.modified_count > 0
        except Exception as e:
            print(f"Error updating summary: {e}")
            return False


class SimplificationLog:
    """Tracks every simplification request for admin monitoring."""

    def __init__(self, db):
        self.collection = db['simplification_logs']
        self.collection.create_index("created_at")
        self.collection.create_index("user_id")

    def create_log(self, user_id, doc_id, doc_title, mode, level,
                   processing_time, original_grade, simplified_grade,
                   original_words, simplified_words):
        """Insert a new log entry."""
        try:
            log = {
                "user_id": str(user_id),
                "doc_id": str(doc_id),
                "doc_title": doc_title,
                "mode": mode,
                "level": level,
                "processing_time": processing_time,
                "original_grade": original_grade,
                "simplified_grade": simplified_grade,
                "grade_reduction": round(original_grade - simplified_grade, 1),
                "original_words": original_words,
                "simplified_words": simplified_words,
                "created_at": datetime.utcnow()
            }
            result = self.collection.insert_one(log)
            return {"success": True, "log_id": str(result.inserted_id)}
        except Exception as e:
            print(f"Error creating log: {e}")
            return {"success": False}

    def get_recent_logs(self, page=1, per_page=20):
        """Fetch paginated recent logs."""
        try:
            skip = (page - 1) * per_page
            logs = list(
                self.collection.find({})
                .sort("created_at", -1)
                .skip(skip)
                .limit(per_page)
            )
            for log in logs:
                log['_id'] = str(log['_id'])
                if log.get('created_at'):
                    log['created_at'] = log['created_at'].strftime('%Y-%m-%d %H:%M:%S')
            total = self.collection.count_documents({})
            return {"items": logs, "total": total, "page": page, "per_page": per_page}
        except Exception as e:
            print(f"Error fetching logs: {e}")
            return {"items": [], "total": 0, "page": page, "per_page": per_page}

    def get_stats(self):
        """Return aggregated statistics."""
        try:
            total = self.collection.count_documents({})
            if total == 0:
                return {
                    "total_requests": 0,
                    "avg_processing_time": 0,
                    "avg_grade_reduction": 0,
                    "requests_by_mode": {"basic": 0, "intermediate": 0, "advanced": 0},
                    "recent_activity": []
                }

            # Aggregation pipeline for stats
            agg_result = list(self.collection.aggregate([
                {"$group": {
                    "_id": None,
                    "avg_time": {"$avg": "$processing_time"},
                    "avg_reduction": {"$avg": "$grade_reduction"},
                    "total": {"$sum": 1}
                }}
            ]))

            # Requests by mode
            mode_agg = list(self.collection.aggregate([
                {"$group": {"_id": "$mode", "count": {"$sum": 1}}}
            ]))
            mode_counts = {"basic": 0, "intermediate": 0, "advanced": 0}
            for item in mode_agg:
                if item['_id'] in mode_counts:
                    mode_counts[item['_id']] = item['count']

            # Recent activity (last 7 days, by day)
            from datetime import timedelta
            seven_days_ago = datetime.utcnow() - timedelta(days=7)
            daily_agg = list(self.collection.aggregate([
                {"$match": {"created_at": {"$gte": seven_days_ago}}},
                {"$group": {
                    "_id": {"$dateToString": {"format": "%Y-%m-%d", "date": "$created_at"}},
                    "count": {"$sum": 1},
                    "avg_time": {"$avg": "$processing_time"}
                }},
                {"$sort": {"_id": 1}}
            ]))

            stats = agg_result[0] if agg_result else {}
            return {
                "total_requests": total,
                "avg_processing_time": round(stats.get("avg_time", 0), 2),
                "avg_grade_reduction": round(stats.get("avg_reduction", 0), 2),
                "requests_by_mode": mode_counts,
                "recent_activity": [{"date": d["_id"], "count": d["count"], "avg_time": round(d["avg_time"], 2)} for d in daily_agg]
            }
        except Exception as e:
            print(f"Error computing stats: {e}")
            return {"total_requests": 0, "avg_processing_time": 0, "avg_grade_reduction": 0,
                    "requests_by_mode": {}, "recent_activity": []}


class GlossaryTerm:
    """Manages custom legal term definitions (admin-managed)."""

    def __init__(self, db):
        self.collection = db['glossary_terms']
        self.collection.create_index("term", unique=True)

    def get_all_terms(self):
        """Return all custom glossary terms."""
        try:
            terms = list(self.collection.find({}).sort("term", 1))
            for t in terms:
                t['_id'] = str(t['_id'])
                if t.get('created_at'):
                    t['created_at'] = t['created_at'].strftime('%Y-%m-%d %H:%M')
            return terms
        except Exception as e:
            print(f"Error fetching glossary: {e}")
            return []

    def add_term(self, term, definition, created_by):
        """Add a new custom glossary term."""
        try:
            doc = {
                "term": term.lower().strip(),
                "display_term": term.strip().title(),
                "definition": definition.strip(),
                "created_by": str(created_by),
                "created_at": datetime.utcnow()
            }
            result = self.collection.insert_one(doc)
            return {"success": True, "term_id": str(result.inserted_id)}
        except Exception as e:
            if "duplicate" in str(e).lower():
                return {"success": False, "message": "Term already exists in glossary"}
            return {"success": False, "message": str(e)}

    def delete_term(self, term_id):
        """Delete a glossary term by ID."""
        try:
            result = self.collection.delete_one({"_id": ObjectId(term_id)})
            return {"success": result.deleted_count > 0,
                    "message": "Term deleted" if result.deleted_count > 0 else "Term not found"}
        except Exception as e:
            return {"success": False, "message": str(e)}

    def update_term(self, term_id, term, definition):
        """Update an existing glossary term."""
        try:
            result = self.collection.update_one(
                {"_id": ObjectId(term_id)},
                {"$set": {
                    "term": term.lower().strip(),
                    "display_term": term.strip().title(),
                    "definition": definition.strip()
                }}
            )
            return {"success": result.modified_count > 0,
                    "message": "Term updated" if result.modified_count > 0 else "Nothing changed"}
        except Exception as e:
            return {"success": False, "message": str(e)}