"""Mortgage Banker Portal - core application: config, schema, identity, security."""
import hmac
import logging
import os
import secrets
import sqlite3
import time
from datetime import datetime, timedelta, timezone
from functools import wraps
from logging.handlers import RotatingFileHandler

from flask import Flask, request, session, redirect, url_for, abort, g
from werkzeug.security import generate_password_hash

__version__ = "2.2.2"

# The public showcase is deliberately isolated from the private working database.
DEMO_MODE = os.environ.get("MORTGAGE_DEMO", "1").lower() not in ("0", "false", "no")

# Configuration
DATA_DIR = os.environ.get("MORTGAGE_DATA_DIR", os.path.join(os.path.dirname(__file__), "data"))
OUTPUT_DIR = os.environ.get("MORTGAGE_OUTPUT_DIR", os.path.join(DATA_DIR, "output"))
LOG_DIR = os.environ.get("MORTGAGE_LOG_DIR", os.path.join(DATA_DIR, "logs"))

os.makedirs(DATA_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
os.makedirs(LOG_DIR, exist_ok=True)

DB_PATH = os.path.join(DATA_DIR, "mortgage_demo.db" if DEMO_MODE else "mortgage.db")

# Security policy (overridable through environment variables).
IDLE_TIMEOUT_MINUTES = int(os.environ.get("MORTGAGE_IDLE_MINUTES", "30"))
SESSION_MAX_HOURS = int(os.environ.get("MORTGAGE_SESSION_HOURS", "8"))
LOGIN_MAX_FAILURES = int(os.environ.get("MORTGAGE_LOGIN_MAX_FAILURES", "5"))
LOGIN_WINDOW_MINUTES = int(os.environ.get("MORTGAGE_LOGIN_WINDOW_MINUTES", "15"))
LOGIN_MAX_FAILURES_PER_ADDRESS = int(os.environ.get("MORTGAGE_LOGIN_MAX_ADDRESS_FAILURES", "20"))
BACKUP_KEEP = int(os.environ.get("MORTGAGE_BACKUP_KEEP", "14"))

logger = logging.getLogger("mortgage_banker_portal")
if not logger.handlers:
    _handler = RotatingFileHandler(os.path.join(LOG_DIR, "app.log"), maxBytes=2_000_000, backupCount=5, encoding="utf-8")
    _handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s"))
    logger.addHandler(_handler)
    logger.setLevel(logging.INFO)


def utc_now_text():
    """UTC timestamp in the same text format SQLite uses for CURRENT_TIMESTAMP."""
    return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")


def get_db():
    """Get database connection."""
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    conn.execute("PRAGMA foreign_keys=ON")
    conn.execute("PRAGMA journal_mode=WAL")
    return conn


def init_db():
    """Bring the schema to the latest tracked migration version.

    Existing private databases are backed up automatically before any pending
    migration runs. See services/migrations.py for the migration rules.
    """
    try:
        from services import migrations, backup
    except ImportError:  # package import
        from .services import migrations, backup
    conn = sqlite3.connect(DB_PATH)
    try:
        needs_backup = (not DEMO_MODE and migrations.has_user_data(conn) and bool(migrations.pending(conn)))
    finally:
        conn.close()
    if needs_backup:
        manifest = backup.create_backup(DB_PATH, DATA_DIR, reason='pre-migration', app_version=__version__)
        logger.info('Pre-migration backup created: %s', manifest['file'])
    applied = migrations.apply_migrations(DB_PATH)
    if applied:
        logger.info('Applied schema migrations: %s', applied)
    return applied


def create_default_user():
    """Create a private administrator only with a supplied first-run password."""
    if DEMO_MODE:
        return
    conn = get_db()
    users = conn.execute("SELECT COUNT(*) FROM users").fetchone()[0]
    if users == 0:
        password = os.environ.get("MORTGAGE_ADMIN_PASSWORD")
        if not password or len(password) < 12:
            conn.close()
            raise RuntimeError("Set MORTGAGE_ADMIN_PASSWORD (12+ characters) before first private run")
        password_hash = generate_password_hash(password)
        conn.execute(
            "INSERT INTO users (username, password_hash, role, display_name) VALUES (?, ?, ?, ?)",
            (os.environ.get("MORTGAGE_ADMIN_USER", "admin"), password_hash, "admin", "Workspace Owner")
        )
        conn.commit()
    
    conn.close()


def current_user():
    """Look up the user on every request so role/status changes take effect."""
    if 'current_user_record' not in g:
        g.current_user_record = None
        if session.get('user_id') is not None and not DEMO_MODE:
            conn = get_db()
            user = conn.execute("SELECT id, username, role, display_name, email, job_title, timezone, is_active FROM users WHERE id = ?", (session['user_id'],)).fetchone()
            conn.close()
            if user and user['is_active']:
                g.current_user_record = dict(user)
    return g.current_user_record


def audit_event(action, category, subject='', details='', conn=None, actor_user_id=None):
    """Append a hash-chained audit event.

    Each row stores ``prev_hash`` and ``row_hash`` (SHA-256 over the previous hash
    and this row's fields), so any later edit, deletion or reordering is
    detectable with ``verify_audit_chain``. Database triggers also reject
    UPDATE/DELETE on the table. Callers may pass their transaction connection.
    """
    try:
        from services.migrations import audit_row_hash
    except ImportError:
        from .services.migrations import audit_row_hash
    own_connection = conn is None
    db = conn if conn is not None else get_db()
    if not db.in_transaction:
        # Take the write lock before reading the chain tip so two writers can't fork it.
        db.execute('BEGIN IMMEDIATE')
    if actor_user_id is None:
        try:
            actor_user_id = session.get('user_id')
        except RuntimeError:  # outside a request (CLI)
            actor_user_id = None
    tip = db.execute('SELECT row_hash FROM audit_events ORDER BY id DESC LIMIT 1').fetchone()
    prev_hash = tip[0] if tip and tip[0] else ''
    record = {'actor_user_id': actor_user_id, 'action': action, 'category': category,
              'subject': str(subject or ''), 'details': str(details or ''), 'created_at': utc_now_text()}
    db.execute('INSERT INTO audit_events (actor_user_id, action, category, subject, details, created_at, prev_hash, row_hash) '
               'VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
               (record['actor_user_id'], action, category, record['subject'], record['details'],
                record['created_at'], prev_hash, audit_row_hash(prev_hash, record)))
    if own_connection:
        db.commit()
        db.close()


def verify_audit_chain(conn=None):
    """Recompute the full audit chain. Returns a summary with the first broken row, if any."""
    try:
        from services.migrations import audit_row_hash
    except ImportError:
        from .services.migrations import audit_row_hash
    own_connection = conn is None
    db = conn if conn is not None else get_db()
    prev, checked, broken = '', 0, None
    for row in db.execute('SELECT id, actor_user_id, action, category, subject, details, created_at, '
                          'prev_hash, row_hash FROM audit_events ORDER BY id'):
        expected = audit_row_hash(prev, dict(row))
        if row['prev_hash'] != prev or row['row_hash'] != expected:
            broken = {'id': row['id'], 'reason': 'previous-hash mismatch' if row['prev_hash'] != prev else 'row content changed'}
            break
        prev = row['row_hash']
        checked += 1
    if own_connection:
        db.close()
    return {'valid': broken is None, 'events_checked': checked, 'first_broken': broken, 'chain_tip': prev}


def login_required(f):
    """Decorator to require login."""
    @wraps(f)
    def decorated(*args, **kwargs):
        # Public demo: only approved read endpoints, never private settings/imports.
        safe_endpoints = {
            'loans.list_loans', 'loans.api_list_loans', 'loans.api_get_loan',
            'brokers.list_brokers', 'brokers.api_list_brokers', 'brokers.api_get_broker',
            'import_export.export_loans', 'analytics.index',
            'reporting.index', 'reporting.dashboard_csv', 'reporting.analyst_csv',
            'integrations.index', 'tools.index', 'tools.api_capabilities',
            'health.index', 'health.api_health'
        }
        if DEMO_MODE and request.method in ('GET', 'HEAD') and (
            request.endpoint in safe_endpoints
            or request.endpoint.startswith('dashboard.')
            or request.endpoint.startswith('analytics.api_')
        ):
            return f(*args, **kwargs)
        if current_user() is None:
            return redirect(url_for('auth.login'))
        return f(*args, **kwargs)
    return decorated


def roles_required(*allowed_roles):
    """Restrict private routes to the listed roles; demo never satisfies RBAC."""
    def wrapper(f):
        @wraps(f)
        @login_required
        def decorated(*args, **kwargs):
            user = current_user()
            if user is None or user['role'] not in allowed_roles:
                abort(403)
            return f(*args, **kwargs)
        return decorated
    return wrapper


def create_app():
    """Create and configure Flask app."""
    app = Flask(__name__)
    secret = os.environ.get("MORTGAGE_SECRET_KEY", "")
    if not DEMO_MODE and len(secret) < 24:
        # A per-process random key silently logs everyone out on restart and hides
        # misconfiguration; private mode must be explicitly configured.
        raise RuntimeError("Set MORTGAGE_SECRET_KEY (24+ random characters) before starting private mode. "
                           "Generate one with: python -c \"import secrets; print(secrets.token_urlsafe(48))\"")
    app.secret_key = secret or os.urandom(32)
    if os.environ.get("MORTGAGE_TRUST_PROXY", "0") == "1":
        # Behind the host's NGINX/Apache proxy: take client IP and scheme from one proxy hop
        # so sign-in throttling and audit records see the real visitor address.
        from werkzeug.middleware.proxy_fix import ProxyFix
        app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
    app.config.update(
        SESSION_COOKIE_HTTPONLY=True,
        SESSION_COOKIE_SAMESITE="Lax",
        SESSION_COOKIE_NAME="mbp_session",
        SESSION_COOKIE_SECURE=os.environ.get("MORTGAGE_COOKIE_SECURE", "0") == "1",
        PERMANENT_SESSION_LIFETIME=timedelta(hours=SESSION_MAX_HOURS),
        MAX_CONTENT_LENGTH=8 * 1024 * 1024,
    )

    # Initialize database
    init_db()
    if DEMO_MODE:
        try:
            from .demo_data import seed_demo_data
        except ImportError:
            from demo_data import seed_demo_data
        seed_demo_data(get_db)

    @app.before_request
    def enforce_session_policy():
        """Absolute lifetime comes from the permanent cookie; idle timeout is enforced here."""
        if DEMO_MODE or session.get('user_id') is None:
            return None
        now = time.time()
        last_seen = session.get('_last_seen', now)
        started = session.get('_started', now)
        if now - last_seen > IDLE_TIMEOUT_MINUTES * 60 or now - started > SESSION_MAX_HOURS * 3600:
            audit_event('session.expired', 'identity', session.get('username', ''),
                        'idle' if now - last_seen > IDLE_TIMEOUT_MINUTES * 60 else 'max-lifetime')
            session.clear()
            if request.path.startswith('/api/'):
                abort(401, description='Session expired')
            return redirect(url_for('auth.login', expired=1))
        session['_last_seen'] = now
        return None

    @app.before_request
    def protect_demo():
        if DEMO_MODE and request.method not in ('GET', 'HEAD', 'OPTIONS'):
            abort(403, description="The portfolio demo is read-only")
        if not DEMO_MODE and request.method not in ('GET', 'HEAD', 'OPTIONS'):
            supplied = request.headers.get('X-CSRF-Token') or request.form.get('csrf_token', '')
            expected = session.get('_csrf_token', '')
            if not expected or not hmac.compare_digest(expected, supplied):
                abort(400, description='Invalid or missing CSRF token')

    @app.after_request
    def security_headers(response):
        response.headers['X-Content-Type-Options'] = 'nosniff'
        response.headers['Referrer-Policy'] = 'strict-origin-when-cross-origin'
        response.headers['X-Frame-Options'] = 'DENY'
        response.headers['Cache-Control'] = 'no-store'
        response.headers['Permissions-Policy'] = 'camera=(), microphone=(), geolocation=(), payment=()'
        response.headers['Cross-Origin-Opener-Policy'] = 'same-origin'
        # All scripts are external files under /static; inline style attributes are still allowed.
        response.headers['Content-Security-Policy'] = (
            "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; "
            "font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; "
            "form-action 'self'; frame-ancestors 'none'")
        return response

    @app.errorhandler(500)
    def internal_error(error):
        logger.exception('Unhandled server error on %s %s', request.method, request.path)
        if request.path.startswith('/api/'):
            return {'error': 'Internal server error. The event was logged for the administrator.'}, 500
        return 'Internal server error. The event was logged for the administrator.', 500

    @app.context_processor
    def layout_context():
        if not DEMO_MODE and '_csrf_token' not in session:
            session['_csrf_token'] = secrets.token_urlsafe(32)
        return {'demo_mode': DEMO_MODE, 'app_version': __version__,
                'csrf_token': session.get('_csrf_token', ''), 'current_account': current_user()}

    @app.template_filter('money')
    def money_format(value):
        return "${:,.0f}".format(float(value or 0))

    # Register routes - handle both package import and direct script execution
    try:
        from . import web as web_pkg
        route_modules = [web_pkg.auth, web_pkg.dashboard, web_pkg.loans, web_pkg.brokers,
                         web_pkg.analytics, web_pkg.import_export, web_pkg.settings,
                         web_pkg.users, web_pkg.reporting, web_pkg.integrations, web_pkg.tools,
                         web_pkg.health]
    except ImportError:
        # Running as script, not package
        import sys
        sys.path.insert(0, os.path.dirname(__file__))
        from web import (auth, dashboard, loans, brokers, analytics, import_export, settings, users,
                         reporting, integrations, tools, health)
        route_modules = [auth, dashboard, loans, brokers, analytics, import_export, settings, users,
                         reporting, integrations, tools, health]
    
    for route_module in route_modules:
        app.register_blueprint(route_module.bp)
    
    # In private mode, bootstrap only from explicitly configured credentials.
    create_default_user()

    return app


if __name__ == "__main__":
    import waitress
    app = create_app()
    port = int(os.environ.get("MORTGAGE_PORT", 8080))
    print(f"Mortgage Banker Portal v{__version__} starting on http://127.0.0.1:{port}")
    waitress.serve(app, host="127.0.0.1", port=port)
