"""
FastAPI Application Factory

Creates and configures the FastAPI application.
"""

from __future__ import annotations

import logging
from contextlib import asynccontextmanager
from pathlib import Path
from typing import TYPE_CHECKING

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse

from .config import AppConfig, default_config
from backend.web.services import InferenceService
from backend.web.services.storage_helpers import get_storage_helper

# Public API imports
from backend.web.api.v1.public import (
    create_inference_router,
    create_async_router,
    set_async_service,
    create_labeling_router,
)

# Async processing imports
from backend.data.async_request_db import AsyncRequestDB
from backend.web.workers.async_queue import AsyncTaskQueue
from backend.web.services.async_processing import AsyncProcessingService
from backend.web.dependencies import init_dependencies
from backend.web.core.rate_limiter import RateLimiter

# Admin API imports
from backend.web.api.v1.admin import (
    create_annotation_router,
    create_augmentation_router,
    create_auth_router,
    create_documents_router,
    create_locks_router,
    create_training_router,
)
from backend.web.api.v1.admin.dashboard import create_dashboard_router
from backend.web.core.scheduler import start_scheduler, stop_scheduler
from backend.web.core.autolabel_scheduler import start_autolabel_scheduler, stop_autolabel_scheduler

# Batch upload imports
from backend.web.api.v1.batch.routes import router as batch_upload_router
from backend.web.workers.batch_queue import init_batch_queue, shutdown_batch_queue
from backend.web.services.batch_upload import BatchUploadService
from backend.data.repositories import ModelVersionRepository

if TYPE_CHECKING:
    from collections.abc import AsyncGenerator

logger = logging.getLogger(__name__)


def create_app(config: AppConfig | None = None) -> FastAPI:
    """
    Create and configure FastAPI application.

    Args:
        config: Application configuration. Uses default if not provided.

    Returns:
        Configured FastAPI application
    """
    config = config or default_config

    # Create model path resolver that reads from database
    def get_active_model_path():
        """Resolve active model path from database."""
        try:
            model_repo = ModelVersionRepository()
            active_model = model_repo.get_active()
            if active_model and active_model.model_path:
                return active_model.model_path
        except Exception as e:
            logger.warning(f"Failed to get active model from database: {e}")
        return None

    # Create inference service with database model resolver
    inference_service = InferenceService(
        model_config=config.model,
        storage_config=config.storage,
        model_path_resolver=get_active_model_path,
    )

    # Create async processing components
    async_db = AsyncRequestDB()
    rate_limiter = RateLimiter(async_db)
    task_queue = AsyncTaskQueue(
        max_size=config.async_processing.queue_max_size,
        worker_count=config.async_processing.worker_count,
    )
    async_service = AsyncProcessingService(
        inference_service=inference_service,
        db=async_db,
        queue=task_queue,
        rate_limiter=rate_limiter,
        async_config=config.async_processing,
        storage_config=config.storage,
    )

    # Initialize dependencies for FastAPI
    init_dependencies(async_db, rate_limiter)
    set_async_service(async_service)

    @asynccontextmanager
    async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
        """Application lifespan manager."""
        logger.info("Starting Invoice Inference API...")

        # Initialize async request database tables
        try:
            async_db.create_tables()
            logger.info("Async database tables ready")
        except Exception as e:
            logger.error(f"Failed to initialize async database: {e}")

        # Initialize admin database tables (admin_tokens, admin_documents, training_tasks, etc.)
        try:
            from backend.data.database import create_db_and_tables
            create_db_and_tables()
            logger.info("Admin database tables ready")
        except Exception as e:
            logger.error(f"Failed to initialize admin database: {e}")

        # Initialize inference service on startup
        try:
            inference_service.initialize()
            logger.info("Inference service ready")
        except Exception as e:
            logger.error(f"Failed to initialize inference service: {e}")
            # Continue anyway - service will retry on first request

        # Start async processing service
        try:
            async_service.start()
            logger.info("Async processing service started")
        except Exception as e:
            logger.error(f"Failed to start async processing: {e}")

        # Start batch upload queue
        try:
            batch_service = BatchUploadService()
            init_batch_queue(batch_service)
            logger.info("Batch upload queue started")
        except Exception as e:
            logger.error(f"Failed to start batch upload queue: {e}")

        # Start training scheduler
        try:
            start_scheduler()
            logger.info("Training scheduler started")
        except Exception as e:
            logger.error(f"Failed to start training scheduler: {e}")

        # Start auto-label scheduler
        try:
            start_autolabel_scheduler()
            logger.info("AutoLabel scheduler started")
        except Exception as e:
            logger.error(f"Failed to start autolabel scheduler: {e}")

        yield

        logger.info("Shutting down Invoice Inference API...")

        # Stop auto-label scheduler
        try:
            stop_autolabel_scheduler()
            logger.info("AutoLabel scheduler stopped")
        except Exception as e:
            logger.error(f"Error stopping autolabel scheduler: {e}")

        # Stop training scheduler
        try:
            stop_scheduler()
            logger.info("Training scheduler stopped")
        except Exception as e:
            logger.error(f"Error stopping training scheduler: {e}")

        # Stop batch upload queue
        try:
            shutdown_batch_queue()
            logger.info("Batch upload queue stopped")
        except Exception as e:
            logger.error(f"Error stopping batch upload queue: {e}")

        # Stop async processing service
        try:
            async_service.stop(timeout=30.0)
            logger.info("Async processing service stopped")
        except Exception as e:
            logger.error(f"Error stopping async service: {e}")

        # Close database connection
        try:
            async_db.close()
            logger.info("Database connection closed")
        except Exception as e:
            logger.error(f"Error closing database: {e}")

    # Create FastAPI app
    # Store inference service for access by routes (e.g., model reload)
    # This will be set after app creation

    app = FastAPI(
        title="Invoice Field Extraction API",
        description="""
        REST API for extracting fields from Swedish invoices.

        ## Features
        - YOLO-based field detection
        - OCR text extraction
        - Field normalization and validation
        - Visualization of detections

        ## Supported Fields
        - InvoiceNumber
        - InvoiceDate
        - InvoiceDueDate
        - OCR (reference number)
        - Bankgiro
        - Plusgiro
        - Amount
        - supplier_org_number (Swedish organization number)
        - customer_number
        - payment_line (machine-readable payment code)
        """,
        version="1.0.0",
        lifespan=lifespan,
    )

    # Add CORS middleware
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # Mount static files for results using StorageHelper
    storage = get_storage_helper()
    results_dir = storage.get_results_base_path()
    if results_dir:
        app.mount(
            "/static/results",
            StaticFiles(directory=str(results_dir)),
            name="results",
        )
    else:
        logger.warning("Could not mount static results directory: local storage not available")

    # Include public API routes
    inference_router = create_inference_router(inference_service, config.storage)
    app.include_router(inference_router)

    async_router = create_async_router(config.storage.allowed_extensions)
    app.include_router(async_router, prefix="/api/v1")

    labeling_router = create_labeling_router(inference_service, config.storage)
    app.include_router(labeling_router)

    # Include admin API routes
    auth_router = create_auth_router()
    app.include_router(auth_router, prefix="/api/v1")

    documents_router = create_documents_router(config.storage)
    app.include_router(documents_router, prefix="/api/v1")

    locks_router = create_locks_router()
    app.include_router(locks_router, prefix="/api/v1")

    annotation_router = create_annotation_router()
    app.include_router(annotation_router, prefix="/api/v1")

    training_router = create_training_router()
    app.include_router(training_router, prefix="/api/v1")

    augmentation_router = create_augmentation_router()
    app.include_router(augmentation_router, prefix="/api/v1/admin")

    # Include dashboard routes
    dashboard_router = create_dashboard_router()
    app.include_router(dashboard_router, prefix="/api/v1")

    # Include batch upload routes
    app.include_router(batch_upload_router)

    # Store inference service in app state for access by routes
    app.state.inference_service = inference_service

    # Root endpoint - serve HTML UI
    @app.get("/", response_class=HTMLResponse)
    async def root() -> str:
        """Serve the web UI."""
        return get_html_ui()

    return app


def get_html_ui() -> str:
    """Generate HTML UI for the web application."""
    return """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice Field Extraction</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            color: white;
            margin-bottom: 30px;
        }

        header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }

        header p {
            opacity: 0.9;
            font-size: 1.1rem;
        }

        .main-content {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .card {
            background: white;
            border-radius: 16px;
            padding: 24px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }

        .card h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .upload-card {
            display: flex;
            align-items: center;
            gap: 20px;
            flex-wrap: wrap;
        }

        .upload-card h2 {
            margin-bottom: 0;
            white-space: nowrap;
        }

        .upload-area {
            border: 2px dashed #ddd;
            border-radius: 10px;
            padding: 15px 25px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
            background: #fafafa;
            flex: 1;
            min-width: 200px;
        }

        .upload-area:hover, .upload-area.dragover {
            border-color: #667eea;
            background: #f0f4ff;
        }

        .upload-area.has-file {
            border-color: #10b981;
            background: #ecfdf5;
        }

        .upload-icon {
            font-size: 24px;
            display: inline;
            margin-right: 8px;
        }

        .upload-area p {
            color: #666;
            margin: 0;
            display: inline;
        }

        .upload-area small {
            color: #999;
            display: block;
            margin-top: 5px;
        }

        #file-input {
            display: none;
        }

        .file-name {
            margin-top: 15px;
            padding: 10px 15px;
            background: #e0f2fe;
            border-radius: 8px;
            color: #0369a1;
            font-weight: 500;
        }

        .btn {
            display: inline-block;
            padding: 12px 24px;
            border: none;
            border-radius: 10px;
            font-size: 0.9rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
        }

        .btn-primary:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .loading {
            display: none;
            align-items: center;
            gap: 10px;
        }

        .loading.active {
            display: flex;
        }

        .spinner {
            width: 24px;
            height: 24px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .results {
            display: none;
        }

        .results.active {
            display: block;
        }

        .result-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }

        .result-status {
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
        }

        .result-status.success {
            background: #dcfce7;
            color: #166534;
        }

        .result-status.partial {
            background: #fef3c7;
            color: #92400e;
        }

        .result-status.error {
            background: #fee2e2;
            color: #991b1b;
        }

        .fields-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 12px;
        }

        .field-item {
            padding: 12px;
            background: #f8fafc;
            border-radius: 10px;
            border-left: 4px solid #667eea;
        }

        .field-item label {
            display: block;
            font-size: 0.75rem;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 4px;
        }

        .field-item .value {
            font-size: 1.1rem;
            font-weight: 600;
            color: #1e293b;
        }

        .field-item .confidence {
            font-size: 0.75rem;
            color: #10b981;
            margin-top: 2px;
        }

        .visualization {
            margin-top: 20px;
        }

        .visualization img {
            width: 100%;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
        }

        .processing-time {
            text-align: center;
            color: #64748b;
            font-size: 0.9rem;
            margin-top: 15px;
        }

        .cross-validation {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 10px;
            padding: 15px;
            margin-top: 20px;
        }

        .cross-validation h3 {
            margin: 0 0 10px 0;
            color: #334155;
            font-size: 1rem;
        }

        .cv-status {
            font-weight: 600;
            padding: 8px 12px;
            border-radius: 6px;
            margin-bottom: 10px;
            display: inline-block;
        }

        .cv-status.valid {
            background: #dcfce7;
            color: #166534;
        }

        .cv-status.invalid {
            background: #fef3c7;
            color: #92400e;
        }

        .cv-details {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            margin-top: 10px;
        }

        .cv-item {
            background: white;
            border: 1px solid #e2e8f0;
            border-radius: 6px;
            padding: 6px 12px;
            font-size: 0.85rem;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .cv-item.match {
            border-color: #86efac;
            background: #f0fdf4;
        }

        .cv-item.mismatch {
            border-color: #fca5a5;
            background: #fef2f2;
        }

        .cv-icon {
            font-weight: bold;
        }

        .cv-item.match .cv-icon {
            color: #16a34a;
        }

        .cv-item.mismatch .cv-icon {
            color: #dc2626;
        }

        .cv-summary {
            margin-top: 10px;
            font-size: 0.8rem;
            color: #64748b;
        }

        .error-message {
            background: #fee2e2;
            color: #991b1b;
            padding: 15px;
            border-radius: 10px;
            margin-top: 15px;
        }

        footer {
            text-align: center;
            color: white;
            opacity: 0.8;
            margin-top: 30px;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>📄 Invoice Field Extraction</h1>
            <p>Upload a Swedish invoice (PDF or image) to extract fields automatically</p>
        </header>

        <div class="main-content">
            <!-- Upload Section - Compact -->
            <div class="card upload-card">
                <h2>📤 Upload</h2>

                <div class="upload-area" id="upload-area">
                    <span class="upload-icon">📁</span>
                    <p>Drag & drop or <strong>click to browse</strong></p>
                    <small>PDF, PNG, JPG (max 50MB)</small>
                    <input type="file" id="file-input" accept=".pdf,.png,.jpg,.jpeg">
                </div>

                <div class="file-name" id="file-name" style="display: none;"></div>

                <button class="btn btn-primary" id="submit-btn" disabled>
                    🚀 Extract
                </button>

                <div class="loading" id="loading">
                    <div class="spinner"></div>
                    <p>Processing...</p>
                </div>
            </div>

            <!-- Results Section - Full Width -->
            <div class="card">
                <h2>📊 Extraction Results</h2>

                <div id="placeholder" style="text-align: center; padding: 30px; color: #999;">
                    <div style="font-size: 48px; margin-bottom: 10px;">🔍</div>
                    <p>Upload a document to see extraction results</p>
                </div>

                <div class="results" id="results">
                    <div class="result-header">
                        <span>Document: <strong id="doc-id"></strong></span>
                        <span class="result-status" id="result-status"></span>
                    </div>

                    <div class="fields-grid" id="fields-grid"></div>

                    <div class="processing-time" id="processing-time"></div>

                    <div class="cross-validation" id="cross-validation" style="display: none;"></div>

                    <div class="error-message" id="error-message" style="display: none;"></div>

                    <div class="visualization" id="visualization" style="display: none;">
                        <h3 style="margin-bottom: 10px; color: #333;">🎯 Detection Visualization</h3>
                        <img id="viz-image" src="" alt="Detection visualization">
                    </div>
                </div>
            </div>
        </div>

        <footer>
            <p>Powered by ColaCoder</p>
        </footer>
    </div>

    <script>
        const uploadArea = document.getElementById('upload-area');
        const fileInput = document.getElementById('file-input');
        const fileName = document.getElementById('file-name');
        const submitBtn = document.getElementById('submit-btn');
        const loading = document.getElementById('loading');
        const placeholder = document.getElementById('placeholder');
        const results = document.getElementById('results');

        let selectedFile = null;

        // Drag and drop handlers
        uploadArea.addEventListener('click', () => fileInput.click());

        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });

        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });

        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                handleFile(files[0]);
            }
        });

        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                handleFile(e.target.files[0]);
            }
        });

        function handleFile(file) {
            const validTypes = ['.pdf', '.png', '.jpg', '.jpeg'];
            const ext = '.' + file.name.split('.').pop().toLowerCase();

            if (!validTypes.includes(ext)) {
                alert('Please upload a PDF, PNG, or JPG file.');
                return;
            }

            selectedFile = file;
            fileName.textContent = `📎 ${file.name}`;
            fileName.style.display = 'block';
            uploadArea.classList.add('has-file');
            submitBtn.disabled = false;
        }

        submitBtn.addEventListener('click', async () => {
            if (!selectedFile) return;

            // Show loading
            submitBtn.disabled = true;
            loading.classList.add('active');
            placeholder.style.display = 'none';
            results.classList.remove('active');

            try {
                const formData = new FormData();
                formData.append('file', selectedFile);

                const response = await fetch('/api/v1/infer', {
                    method: 'POST',
                    body: formData,
                });

                const data = await response.json();

                if (!response.ok) {
                    throw new Error(data.detail || 'Processing failed');
                }

                displayResults(data);

            } catch (error) {
                console.error('Error:', error);
                document.getElementById('error-message').textContent = error.message;
                document.getElementById('error-message').style.display = 'block';
                results.classList.add('active');
            } finally {
                loading.classList.remove('active');
                submitBtn.disabled = false;
            }
        });

        function displayResults(data) {
            const result = data.result;

            // Document ID
            document.getElementById('doc-id').textContent = result.document_id;

            // Status
            const statusEl = document.getElementById('result-status');
            statusEl.textContent = result.success ? 'Success' : 'Partial';
            statusEl.className = 'result-status ' + (result.success ? 'success' : 'partial');

            // Fields
            const fieldsGrid = document.getElementById('fields-grid');
            fieldsGrid.innerHTML = '';

            const fieldOrder = [
                'InvoiceNumber', 'InvoiceDate', 'InvoiceDueDate', 'OCR',
                'Amount', 'Bankgiro', 'Plusgiro',
                'supplier_org_number', 'customer_number', 'payment_line'
            ];

            fieldOrder.forEach(field => {
                const value = result.fields[field];
                const confidence = result.confidence[field];

                if (value !== null && value !== undefined) {
                    const fieldDiv = document.createElement('div');
                    fieldDiv.className = 'field-item';
                    fieldDiv.innerHTML = `
                        <label>${formatFieldName(field)}</label>
                        <div class="value">${value}</div>
                        ${confidence ? `<div class="confidence">✓ ${(confidence * 100).toFixed(1)}% confident</div>` : ''}
                    `;
                    fieldsGrid.appendChild(fieldDiv);
                }
            });

            // Processing time
            document.getElementById('processing-time').textContent =
                `⏱️ Processed in ${result.processing_time_ms.toFixed(0)}ms`;

            // Cross-validation results
            const cvDiv = document.getElementById('cross-validation');
            if (result.cross_validation) {
                const cv = result.cross_validation;
                let cvHtml = '<h3>🔍 Cross-Validation (Payment Line)</h3>';
                cvHtml += `<div class="cv-status ${cv.is_valid ? 'valid' : 'invalid'}">`;
                cvHtml += cv.is_valid ? '✅ Valid' : '⚠️ Mismatch Detected';
                cvHtml += '</div>';

                cvHtml += '<div class="cv-details">';
                if (cv.payment_line_ocr) {
                    const matchIcon = cv.ocr_match === true ? '✓' : (cv.ocr_match === false ? '✗' : '—');
                    cvHtml += `<div class="cv-item ${cv.ocr_match === true ? 'match' : (cv.ocr_match === false ? 'mismatch' : '')}">`;
                    cvHtml += `<span class="cv-icon">${matchIcon}</span> OCR: ${cv.payment_line_ocr}</div>`;
                }
                if (cv.payment_line_amount) {
                    const matchIcon = cv.amount_match === true ? '✓' : (cv.amount_match === false ? '✗' : '—');
                    cvHtml += `<div class="cv-item ${cv.amount_match === true ? 'match' : (cv.amount_match === false ? 'mismatch' : '')}">`;
                    cvHtml += `<span class="cv-icon">${matchIcon}</span> Amount: ${cv.payment_line_amount}</div>`;
                }
                if (cv.payment_line_account) {
                    const accountType = cv.payment_line_account_type === 'bankgiro' ? 'Bankgiro' : 'Plusgiro';
                    const matchField = cv.payment_line_account_type === 'bankgiro' ? cv.bankgiro_match : cv.plusgiro_match;
                    const matchIcon = matchField === true ? '✓' : (matchField === false ? '✗' : '—');
                    cvHtml += `<div class="cv-item ${matchField === true ? 'match' : (matchField === false ? 'mismatch' : '')}">`;
                    cvHtml += `<span class="cv-icon">${matchIcon}</span> ${accountType}: ${cv.payment_line_account}</div>`;
                }
                cvHtml += '</div>';

                if (cv.details && cv.details.length > 0) {
                    cvHtml += '<div class="cv-summary">' + cv.details[cv.details.length - 1] + '</div>';
                }

                cvDiv.innerHTML = cvHtml;
                cvDiv.style.display = 'block';
            } else {
                cvDiv.style.display = 'none';
            }

            // Visualization
            if (result.visualization_url) {
                const vizDiv = document.getElementById('visualization');
                const vizImg = document.getElementById('viz-image');
                vizImg.src = result.visualization_url;
                vizDiv.style.display = 'block';
            }

            // Errors
            if (result.errors && result.errors.length > 0) {
                document.getElementById('error-message').textContent = result.errors.join(', ');
                document.getElementById('error-message').style.display = 'block';
            } else {
                document.getElementById('error-message').style.display = 'none';
            }

            results.classList.add('active');
        }

        function formatFieldName(name) {
            const nameMap = {
                'InvoiceNumber': 'Invoice Number',
                'InvoiceDate': 'Invoice Date',
                'InvoiceDueDate': 'Due Date',
                'OCR': 'OCR Reference',
                'Amount': 'Amount',
                'Bankgiro': 'Bankgiro',
                'Plusgiro': 'Plusgiro',
                'supplier_org_number': 'Supplier Org Number',
                'customer_number': 'Customer Number',
                'payment_line': 'Payment Line'
            };
            return nameMap[name] || name.replace(/([A-Z])/g, ' $1').replace(/_/g, ' ').trim();
        }
    </script>
</body>
</html>
"""
