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
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
#!/usr/bin/env python3
"""

Legal Dashboard FastAPI Main Application (Simplified)

====================================================



Simplified FastAPI application for testing API structure.

"""

import os
import logging
from pathlib import Path
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application lifespan manager"""
    try:
        logger.info("🚀 Starting Legal Dashboard (Simplified)...")

        # Create required directories (Windows compatible)
        uploads_dir = Path.cwd() / "uploads"
        data_dir = Path.cwd() / "data"
        os.makedirs(uploads_dir, exist_ok=True)
        os.makedirs(data_dir, exist_ok=True)

        logger.info("🎉 Services initialized successfully!")

        yield  # Application runs here

    except Exception as e:
        logger.error(f"❌ Initialization failed: {e}")
        raise
    finally:
        logger.info("🔄 Shutting down Legal Dashboard...")

# Create FastAPI application
app = FastAPI(
    title="Legal Dashboard API",
    description="AI-powered Persian legal document processing system",
    version="1.0.0",
    docs_url="/api/docs",
    redoc_url="/api/redoc",
    lifespan=lifespan
)

# Add middlewares
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Configure properly in production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Serve static files (Frontend)
frontend_dir = Path(__file__).parent.parent / "frontend"
if frontend_dir.exists():
    app.mount("/static", StaticFiles(directory=str(frontend_dir)), name="static")
    logger.info(f"📁 Static files mounted from: {frontend_dir}")
else:
    logger.warning("⚠️ Frontend directory not found")

# Root route - serve main dashboard


@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def read_root():
    """Serve main dashboard page"""
    try:
        html_file = frontend_dir / "index.html"
        if html_file.exists():
            return FileResponse(html_file, media_type="text/html")
        else:
            return HTMLResponse("""

            <html>

                <head><title>Legal Dashboard</title></head>

                <body>

                    <h1>🏛️ Legal Dashboard API</h1>

                    <p>Backend is running! Frontend files not found.</p>

                    <p><a href="/api/docs">📖 API Documentation</a></p>

                </body>

            </html>

            """)
    except Exception as e:
        logger.error(f"Error serving root: {e}")
        raise HTTPException(status_code=500, detail="Error serving homepage")

# Health check endpoint


@app.get("/api/health")
async def health_check():
    """System health check"""
    return {
        "status": "healthy",
        "services": {
            "database": "healthy",
            "ocr": "healthy",
            "ai": "healthy"
        },
        "version": "1.0.0"
    }

# Dashboard endpoints


@app.get("/api/dashboard/summary")
async def dashboard_summary():
    """Dashboard summary data"""
    return {
        "total_documents": 6,
        "processed_documents": 4,
        "error_documents": 1,
        "average_quality": 8.1,
        "recent_activity": [
            {"date": "2024-12-01", "count": 2},
            {"date": "2024-12-02", "count": 3},
            {"date": "2024-12-03", "count": 1}
        ]
    }


@app.get("/api/dashboard/charts-data")
async def charts_data():
    """Charts data for dashboard"""
    return {
        "category_distribution": {
            "قراردادها": 1,
            "دادخواست‌ها": 1,
            "احکام قضایی": 1,
            "آرای دیوان": 1,
            "سایر": 2
        },
        "processing_trends": [
            {"date": "2024-12-01", "processed": 2, "uploaded": 3},
            {"date": "2024-12-02", "processed": 3, "uploaded": 4},
            {"date": "2024-12-03", "processed": 1, "uploaded": 2}
        ]
    }


@app.get("/api/dashboard/ai-suggestions")
async def ai_suggestions():
    """AI suggestions for dashboard"""
    return {
        "suggestions": [
            {
                "title": "بهبود کیفیت OCR",
                "description": "پیشنهاد می‌شود از تصاویر با کیفیت بالاتر استفاده کنید",
                "score": 0.85
            },
            {
                "title": "دسته‌بندی خودکار",
                "description": "سیستم می‌تواند اسناد را به صورت خودکار دسته‌بندی کند",
                "score": 0.92
            }
        ]
    }


@app.post("/api/dashboard/ai-feedback")
async def ai_feedback():
    """AI feedback endpoint"""
    return {"status": "success", "message": "Feedback received"}


@app.get("/api/dashboard/performance-metrics")
async def performance_metrics():
    """Performance metrics"""
    return {
        "ocr_accuracy": 0.92,
        "processing_speed": 15.3,
        "error_rate": 0.08
    }


@app.get("/api/dashboard/trends")
async def dashboard_trends():
    """Dashboard trends"""
    return {
        "document_growth": 15.2,
        "quality_improvement": 2.1,
        "processing_efficiency": 8.3
    }

# Documents endpoints


@app.get("/api/documents")
async def get_documents():
    """Get all documents"""
    return {
        "documents": [
            {"id": 1, "title": "قرارداد اجاره",
                "status": "processed", "quality": 8.5},
            {"id": 2, "title": "دادخواست حقوقی",
                "status": "processed", "quality": 7.8},
            {"id": 3, "title": "حکم قضایی", "status": "error", "quality": 0.0}
        ]
    }


@app.get("/api/documents/search/")
async def search_documents():
    """Search documents"""
    return {"results": [], "total": 0}


@app.get("/api/documents/categories/")
async def get_categories():
    """Get document categories"""
    return {
        "categories": ["قراردادها", "دادخواست‌ها", "احکام قضایی", "آرای دیوان", "سایر"]
    }


@app.get("/api/documents/sources/")
async def get_sources():
    """Get document sources"""
    return {
        "sources": ["آپلود دستی", "اسکن خودکار", "ایمیل", "وب‌سایت"]
    }

# OCR endpoints


@app.post("/api/ocr/process")
async def process_ocr():
    """Process OCR"""
    return {"status": "success", "text": "متن استخراج شده"}


@app.post("/api/ocr/process-and-save")
async def process_and_save_ocr():
    """Process OCR and save"""
    return {"status": "success", "document_id": 1}


@app.post("/api/ocr/batch-process")
async def batch_process_ocr():
    """Batch process OCR"""
    return {"status": "success", "processed": 5}


@app.get("/api/ocr/quality-metrics")
async def ocr_quality_metrics():
    """OCR quality metrics"""
    return {
        "average_accuracy": 0.92,
        "confidence_threshold": 0.8,
        "error_rate": 0.08
    }


@app.get("/api/ocr/models")
async def ocr_models():
    """Available OCR models"""
    return {
        "models": ["persian_ocr_v1", "persian_ocr_v2", "multilingual_ocr"]
    }


@app.get("/api/ocr/status")
async def ocr_status():
    """OCR service status"""
    return {"status": "healthy", "active_models": 2}

# Analytics endpoints


@app.get("/api/analytics/overview")
async def analytics_overview():
    """Analytics overview"""
    return {
        "total_documents": 6,
        "processing_rate": 85.7,
        "average_quality": 8.1
    }


@app.get("/api/analytics/trends")
async def analytics_trends():
    """Analytics trends"""
    return {
        "daily_processing": [2, 3, 1, 4, 2, 3, 1],
        "quality_trend": [7.5, 8.1, 8.3, 8.0, 8.2, 8.1, 8.4]
    }


@app.get("/api/analytics/similarity")
async def analytics_similarity():
    """Document similarity analysis"""
    return {
        "similarity_matrix": [],
        "clusters": []
    }


@app.get("/api/analytics/performance")
async def analytics_performance():
    """Performance analytics"""
    return {
        "processing_time": 15.3,
        "accuracy_rate": 92.0,
        "throughput": 4.2
    }


@app.get("/api/analytics/entities")
async def analytics_entities():
    """Entity extraction analytics"""
    return {
        "entities_found": 45,
        "entity_types": ["نام", "تاریخ", "مبلغ", "آدرس"]
    }


@app.get("/api/analytics/quality-analysis")
async def analytics_quality():
    """Quality analysis"""
    return {
        "quality_distribution": {
            "excellent": 2,
            "good": 3,
            "poor": 1
        }
    }

# Scraping endpoints


@app.post("/api/scraping/scrape")
async def start_scraping():
    """Start web scraping"""
    return {"status": "started", "job_id": "scrape_001"}


@app.get("/api/scraping/status")
async def scraping_status():
    """Scraping status"""
    return {"status": "idle", "last_run": "2024-12-01"}


@app.get("/api/scraping/items")
async def scraping_items():
    """Scraped items"""
    return {
        "items": [
            {"url": "https://example.com/1", "title": "مطلب اول"},
            {"url": "https://example.com/2", "title": "مطلب دوم"}
        ]
    }


@app.get("/api/scraping/statistics")
async def scraping_statistics():
    """Scraping statistics"""
    return {
        "total_scraped": 150,
        "success_rate": 95.2,
        "average_speed": 2.3
    }


@app.get("/api/scraping/rating/summary")
async def scraping_rating_summary():
    """Scraping rating summary"""
    return {
        "average_rating": 4.2,
        "total_ratings": 25,
        "rating_distribution": {"5": 10, "4": 8, "3": 4, "2": 2, "1": 1}
    }

# Error handlers


@app.exception_handler(404)
async def not_found_handler(request, exc):
    """Custom 404 handler"""
    return HTMLResponse("""

    <html>

        <head><title>404 - صفحه یافت نشد</title></head>

        <body style="font-family: 'Tahoma', sans-serif; text-align: center; padding: 50px;">

            <h1>🔍 صفحه یافت نشد</h1>

            <p>صفحه مورد نظر شما وجود ندارد.</p>

            <a href="/">🏠 بازگشت به صفحه اصلی</a>

        </body>

    </html>

    """, status_code=404)


@app.exception_handler(500)
async def internal_error_handler(request, exc):
    """Custom 500 handler"""
    logger.error(f"Internal server error: {exc}")
    return HTMLResponse("""

    <html>

        <head><title>500 - خطای سرور</title></head>

        <body style="font-family: 'Tahoma', sans-serif; text-align: center; padding: 50px;">

            <h1>⚠️ خطای سرور</h1>

            <p>متأسفانه خطایی در سرور رخ داده است.</p>

            <a href="/">🏠 بازگشت به صفحه اصلی</a>

        </body>

    </html>

    """, status_code=500)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)