import os
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import TemplateView
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from rest_framework.routers import DefaultRouter
from rest_framework_simplejwt.views import TokenRefreshView

from core.sso import sso_enter
from core.views import (CustomTokenObtainPairView, UserViewSet, RoleViewSet,
                        ClientInviteView, ClientInviteVerifyView, ClientInviteRegisterView,
                        ClientLoginLogsView, ClientEmailReportsView, SystemConfigView, SystemConfigTestView)
from clients.views import (ClientViewSet, ClientContactViewSet, ClientDepartmentViewSet,
                           TicketCategoryViewSet, TicketSubCategoryViewSet, EscalationMatrixViewSet,
                           ClientConfigView, PinkToolStatsView, ClientDatabaseViewSet,
                           ClientEmailAccountViewSet, OutboundEmailViewSet, ClientNetworkViewSet, NetworkStatusView)
from tickets.views import TicketViewSet
from sla.views import SLAPolicyViewSet, SLABreachLogViewSet, BusinessHourViewSet, SLAPauseViewSet
from workflows.views import WorkflowViewSet, WorkflowStepViewSet
from approvals.views import ApprovalRequestViewSet
from audit.views import AuditLogViewSet
from notifications.views import NotificationViewSet, NotificationTemplateViewSet
from knowledge.views import KnowledgeArticleViewSet, ReplyTemplateViewSet
from reports.views import DashboardReportView, SLAReportView, AgentPerformanceReportView
from ai_services.views import (AIReplyAssistantView, AIReplyLibraryView, AIClassifyTicketView,
                                AIQualityReviewView, AIReportSummaryView,
                                AIAssistantView, AIOutboundDraftView)
from integrations.views import (VICIDialStatusView, VICIDialCallPopView, VICIDialDispositionView,
                                 VICIDialRecordingView, VICIDialConfigView,
                                 WebhookInboundEmailView, WebhookSocialMediaView)


_MIME = {
    '.html': 'text/html; charset=utf-8',
    '.txt': 'text/plain; charset=utf-8',
    '.js': 'application/javascript',
    '.css': 'text/css',
    '.png': 'image/png',
    '.jpg': 'image/jpeg',
    '.svg': 'image/svg+xml',
    '.ico': 'image/x-icon',
    '.woff': 'font/woff',
    '.woff2': 'font/woff2',
    '.ttf': 'font/ttf',
    '.json': 'application/json',
}

DIST_DIR = os.path.join(str(settings.BASE_DIR), 'dist')


def serve_dist_file(request, path):
    full_path = os.path.realpath(os.path.join(DIST_DIR, path))
    # Security: prevent directory traversal
    if not full_path.startswith(os.path.realpath(DIST_DIR)):
        from django.http import Http404
        raise Http404
    if os.path.exists(full_path) and os.path.isfile(full_path):
        ext = os.path.splitext(full_path)[1].lower()
        mime = _MIME.get(ext, 'application/octet-stream')
        with open(full_path, 'rb') as f:
            return HttpResponse(f.read(), content_type=mime)
    from django.http import Http404
    raise Http404


def react_app(request, path=''):
    index_path = os.path.join(DIST_DIR, 'index.html')
    if os.path.exists(index_path):
        with open(index_path, 'rb') as f:
            return HttpResponse(f.read(), content_type='text/html')
    return TemplateView.as_view(template_name='login.html')(request)


def _deny_unless_ops_secret(request):
    """Guard for operational / diagnostic endpoints.

    Returns an HttpResponse to short-circuit with, or None when authorised.
    These endpoints run migrations, DDL and diagnostics, so they must never be
    reachable anonymously. Fails CLOSED: if EMAIL_WEBHOOK_SECRET is not
    configured the endpoint is denied rather than left open, and the comparison
    is constant-time.
    """
    import hmac
    import json
    expected = (os.environ.get('EMAIL_WEBHOOK_SECRET', '')
                or getattr(settings, 'EMAIL_WEBHOOK_SECRET', '') or '')
    provided = request.GET.get('secret', '')
    if not expected or not hmac.compare_digest(str(provided), str(expected)):
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403,
                            content_type='application/json')
    return None


def debug_paths(request):
    denied = _deny_unless_ops_secret(request)
    if denied:
        return denied
    index_path = os.path.join(DIST_DIR, 'index.html')
    try:
        contents = str(os.listdir(DIST_DIR) if os.path.exists(DIST_DIR) else os.listdir(str(settings.BASE_DIR)))
    except Exception as e:
        contents = str(e)
    html = f"<pre>BASE_DIR={settings.BASE_DIR}\nDIST={DIST_DIR}\nINDEX_EXISTS={os.path.exists(index_path)}\nCONTENTS={contents}</pre>"
    return HttpResponse(html)


def create_client_user(request):
    """REMOVED — was an unauthenticated account-creation endpoint.

    It created/reset the user 'rapid' with a hardcoded password and the
    client_admin role, so anyone who knew the URL could mint a working login
    (and reset it again after any password change). It was a one-off seeding
    helper; create client users from Users & Audit or the Client Portal instead.
    """
    import json
    return HttpResponse(json.dumps({'error': 'Gone'}), status=410,
                        content_type='application/json')


def run_pinktool_setup(request):
    """REMOVED — was package installation and database migration over HTTP.

    It ran `pip install` and `migrate` in the live environment for anyone who
    knew the URL and the ops secret. That is remote code execution by design:
    one leaked secret, in a log or a shell history, and the server runs whatever
    the next release of a package contains. It existed to set the environment up
    once, and the environment is set up. Migrations belong in a shell, where the
    person running them can see what happened.
    """
    import json
    return HttpResponse(json.dumps({'error': 'Gone'}), status=410,
                        content_type='application/json')


def backfill_channel_source(request):
    import json
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403, content_type='application/json')
    from clients.models import ClientEmailAccount
    from tickets.models import Ticket
    updated = 0
    # For each client that has email accounts, assign the first active account name
    for acct in ClientEmailAccount.objects.filter(is_active=True).order_by('id'):
        count = Ticket.objects.filter(
            client=acct.client, channel='email', channel_source=''
        ).update(channel_source=acct.name)
        updated += count
    return HttpResponse(json.dumps({'ok': True, 'updated': updated}), content_type='application/json')


def auto_fetch_emails(request):
    import json, os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403, content_type='application/json')
    from clients.management.commands.fetch_emails import fetch_account
    from clients.models import ClientEmailAccount
    # One-time backlog recovery: ?reset=1 rewinds the UID high-water mark so the
    # next fetch re-scans the recent window. Message-ID dedup prevents duplicate
    # tickets/comments, so this is safe to run.
    if request.GET.get('reset') == '1':
        ClientEmailAccount.objects.filter(is_active=True).update(last_uid=0, imap_uidvalidity='')
    from core.jobs import record_job
    results = []
    with record_job('fetch_emails', every_minutes=5) as job:
        for acct in ClientEmailAccount.objects.filter(is_active=True):
            r = fetch_account(acct)
            results.append({'account': acct.email, **r})
        job.detail = f'{len(results)} mailbox(es) polled'

    # Send AI auto-replies whose human-like delay has elapsed. On a server with a
    # long-running watcher this sweep is done by `watch_emails`; on cPanel this
    # cron URL is the only thing that runs periodically, so it must sweep too --
    # otherwise replies queue up as "pending" forever and nothing is ever sent.
    sent = 0
    sweep_error = None
    try:
        from ai_services.auto_reply import send_due_replies
        with record_job('ai_reply_sweep', every_minutes=5) as job:
            sent = send_due_replies()
            job.detail = f'{sent} reply(ies) sent'
    except Exception as e:
        sweep_error = str(e)
    payload = {'ok': True, 'results': results, 'auto_replies_sent': sent}
    if sweep_error:
        payload['auto_reply_error'] = sweep_error
    return HttpResponse(json.dumps(payload, default=str), content_type='application/json')


def fix_charset(request):
    """One-time: convert all tables to utf8mb4 so emoji / special Unicode in
    emails can be stored (fixes MySQL 1366 'Incorrect string value')."""
    import json, os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403, content_type='application/json')
    from django.db import connection
    converted, failed = [], []
    with connection.cursor() as cur:
        cur.execute("ALTER DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
                    % connection.settings_dict['NAME'])
        cur.execute("SELECT table_name FROM information_schema.tables "
                    "WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE'")
        tables = [r[0] for r in cur.fetchall()]
        for t in tables:
            try:
                cur.execute("ALTER TABLE `%s` CONVERT TO CHARACTER SET utf8mb4 "
                            "COLLATE utf8mb4_unicode_ci" % t)
                converted.append(t)
            except Exception as e:
                failed.append({'table': t, 'error': str(e)})
    return HttpResponse(json.dumps({'ok': True, 'converted': converted, 'failed': failed}, default=str),
                        content_type='application/json')


def _is_public_ip(host):
    import ipaddress
    try:
        return ipaddress.ip_address(host).is_global
    except ValueError:
        return False


def monitor_networks(request):
    """Cron target: ping each configured network's PUBLIC targets from the CRM
    and open a ticket on a down/recovery state change. Private (192.168.x etc.)
    and the generic internet check are skipped — the cloud can't see private IPs,
    so only a public WAN IP is meaningful here."""
    import json, os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403, content_type='application/json')
    from clients.models import ClientNetwork, NetworkAlert
    from clients.views import _ping_network
    from django.utils import timezone
    results = []
    # Switched-off connections are skipped: no pings, no alerts. That is the
    # point of the toggle — planned work shouldn't page anyone.
    for net in ClientNetwork.objects.select_related('client').filter(is_active=True):
        targets = _ping_network(net)
        last = net.last_status or {}
        changes = []
        for t in targets:
            host = t.get('host')
            if t.get('type') == 'internet' or not _is_public_ip(host):
                continue  # skip the generic internet check and unreachable private IPs
            cur = 'up' if t.get('up') else 'down'
            prev = last.get(host)
            if prev is not None and prev != cur:
                if cur == 'down':
                    subject = f"[NETWORK DOWN] {net.name} — {t['label']} ({host}) is DOWN"
                    kind = NetworkAlert.DOWN
                else:
                    subject = f"[NETWORK UP] {net.name} — {t['label']} ({host}) RECOVERED"
                    kind = NetworkAlert.UP
                # Recorded against the network, not as a ticket — see NetworkAlert.
                NetworkAlert.objects.create(
                    client=net.client, network=net, kind=kind,
                    gateway=t.get('label') or host, message=subject[:500],
                )
                if kind == NetworkAlert.UP:
                    NetworkAlert.objects.filter(
                        network=net, gateway=t.get('label') or host,
                        kind=NetworkAlert.DOWN, resolved_at__isnull=True
                    ).update(resolved_at=timezone.now())
                changes.append(subject)
            last[host] = cur
        net.last_status = last
        net.save(update_fields=['last_status'])
        results.append({'network': net.name, 'changes': changes})
    return HttpResponse(json.dumps({'ok': True, 'results': results}, default=str),
                        content_type='application/json')


def pfsense_script(request):
    """Serve the pfSense gateway-monitor script with the CRM URL + secret baked
    in, so installing on a client's pfSense is a single curl command.

    ``?key=<monitor_key>`` binds the script to ONE ClientNetwork, so the alerts
    that pfSense raises open tickets for that client only. Fetch the URL shown on
    the network in Configuration -> Networks; each network has its own.
    """
    import os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse('Unauthorized', status=403, content_type='text/plain')

    from clients.models import ClientNetwork
    key = (request.GET.get('key') or '').strip()
    if not key:
        return HttpResponse(
            '# ERROR: missing ?key=<monitor key>\n'
            '# Every pfSense must identify which client network it belongs to,\n'
            '# otherwise its alerts cannot be routed. Copy the install command\n'
            '# from Configuration -> Networks -> (your network).\n',
            status=400, content_type='text/plain')
    net = ClientNetwork.objects.select_related('client').filter(monitor_key=key).first()
    if not net:
        return HttpResponse('# ERROR: unknown monitor key\n', status=404, content_type='text/plain')

    domain = os.environ.get('DOMAIN', 'altriacallcenter.com')
    webhook = f"https://crm.{domain}/api/webhooks/email/" if not domain.startswith('crm.') else f"https://{domain}/api/webhooks/email/"
    webhook = webhook.replace('crm.crm.', 'crm.')
    script = (PFSENSE_SCRIPT_TEMPLATE
              .replace('__WEBHOOK__', webhook)
              .replace('__SECRET__', expected)
              .replace('__CLIENT_KEY__', net.monitor_key)
              .replace('__NETWORK__', f'{net.client.name} / {net.name}'))
    return HttpResponse(script, content_type='text/plain')


PFSENSE_SCRIPT_TEMPLATE = r'''#!/bin/sh
# pfSense Gateway Monitor -> CRM (auto-generated). Detects gateway up/down
# changes and posts a ticket to the CRM.
#
# BOUND TO: __NETWORK__
# Alerts from this pfSense open tickets for that client only. Do NOT copy this
# script to another site -- fetch that site's own copy from
# Configuration -> Networks, or its alerts will be filed under the wrong client.
#
# Install:
#   curl -s "https://crm.altriacallcenter.com/pfsense-script/?secret=YOURSECRET&key=YOURKEY" -o /usr/local/bin/crm_gateway_check.sh
#   chmod +x /usr/local/bin/crm_gateway_check.sh
# then add to pfSense Cron: * * * * * root /usr/local/bin/crm_gateway_check.sh
CRM_WEBHOOK="__WEBHOOK__"
CRM_SECRET="__SECRET__"
CRM_CLIENT_KEY="__CLIENT_KEY__"
STATE_FILE="/tmp/crm_gw_state"
LOG="/var/log/crm_notify.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S UTC')
if [ -f "$STATE_FILE" ]; then PREV_STATE=$(cat "$STATE_FILE"); else PREV_STATE=""; fi
GW_STATUS=$(/usr/local/bin/php -r "
require_once('/etc/inc/gwlb.inc');
require_once('/etc/inc/util.inc');
\$gateways = return_gateways_status(true);
foreach (\$gateways as \$name => \$gw) { echo \$name . '=' . strtolower(\$gw['status'] ?? 'none') . PHP_EOL; }
" 2>/dev/null)
CURRENT_STATE="$GW_STATUS"
echo "$GW_STATUS" | while IFS='=' read -r GW_NAME GW_STAT; do
    [ -z "$GW_NAME" ] && continue
    PREV=$(echo "$PREV_STATE" | grep "^${GW_NAME}=" | cut -d= -f2)
    if [ "$GW_STAT" != "$PREV" ]; then
        case "$GW_STAT" in
            *down*|*loss*|*delay*|*alarm*)
                PRIORITY="critical"; SUBJECT="[NETWORK DOWN] Gateway $GW_NAME is DOWN"
                BODY="Gateway: $GW_NAME\nStatus: DOWN\nPrevious: $PREV\nTime: $TIMESTAMP" ;;
            *online*|*none*|*up*)
                if echo "$PREV" | grep -qiE 'down|loss|delay|alarm'; then
                    PRIORITY="medium"; SUBJECT="[NETWORK UP] Gateway $GW_NAME RECOVERED"
                    sleep 3
                    SPD_BPS=$(curl -o /dev/null -s -w '%{speed_download}' --max-time 25 "https://speed.cloudflare.com/__down?bytes=50000000" 2>/dev/null)
                    SPD=$(echo "$SPD_BPS" | awk '{if($1>0) printf "%.1f Mbps", $1*8/1000000; else printf "unavailable (test failed)"}')
                    BODY="Gateway: $GW_NAME\nStatus: RECOVERED\nPrevious: $PREV\nTime: $TIMESTAMP\nDownload speed on recovery: ${SPD}"
                else continue; fi ;;
            *) continue ;;
        esac
        SUBJECT_ESC=$(printf '%s' "$SUBJECT" | sed 's/"/\\"/g')
        BODY_ESC=$(printf '%s' "$BODY" | sed 's/"/\\"/g' | tr -d '\n')
        JSON="{\"subject\":\"$SUBJECT_ESC\",\"from\":\"pfsense@altria.local\",\"from_name\":\"pfSense Firewall\",\"body\":\"$BODY_ESC\",\"email_id\":\"pfsense-$(date +%s)-$GW_NAME\",\"source\":\"email\",\"client_key\":\"$CRM_CLIENT_KEY\",\"_ai_action\":\"ticket\",\"_ai_priority\":\"$PRIORITY\",\"_ai_sentiment\":\"neutral\",\"_ai_category\":\"network\",\"_ai_summary\":\"$SUBJECT_ESC\"}"
        curl -s -o /dev/null -X POST "$CRM_WEBHOOK" -H "Content-Type: application/json" -H "X-Webhook-Secret: $CRM_SECRET" -H "X-Monitor-Key: $CRM_CLIENT_KEY" -d "$JSON" --max-time 10
        echo "[$TIMESTAMP] $SUBJECT" >> $LOG
    fi
done
echo "$CURRENT_STATE" > "$STATE_FILE"
'''


def _speedtest_email_html(subject, body):
    """Render the plain speed-test summary into a professional HTML email."""
    import re
    from django.utils.html import escape
    ts = ''
    rows = ''
    for ln in (body or '').split('\n'):
        ln = ln.strip()
        m = re.match(r'^Time:\s*(.+)$', ln)
        if m:
            ts = m.group(1)
            continue
        g = re.match(r'^(\S+)\s*\(([^)]+)\):\s*(.+)$', ln)
        if not g:
            continue
        gw, iface, spd = escape(g.group(1)), escape(g.group(2)), escape(g.group(3))
        low = spd.lower()
        if 'down' in low:
            color, badge = '#9ca3af', '#f3f4f6'
        elif 'unavail' in low:
            color, badge = '#dc2626', '#fef2f2'
        else:
            color, badge = '#16a34a', '#f0fdf4'
        rows += (
            '<tr>'
            f'<td style="padding:12px 16px;border-bottom:1px solid #eef0f4;font-weight:600;color:#111827">{gw}</td>'
            f'<td style="padding:12px 16px;border-bottom:1px solid #eef0f4;color:#6b7280;font-size:13px">{iface}</td>'
            f'<td style="padding:12px 16px;border-bottom:1px solid #eef0f4;text-align:right">'
            f'<span style="background:{badge};color:{color};font-weight:700;padding:4px 12px;border-radius:20px;font-size:13px">{spd}</span>'
            '</td></tr>'
        )
    return (
        '<div style="background:#f4f5f7;padding:24px 0;font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif">'
        '<div style="max-width:600px;margin:0 auto;background:#fff;border-radius:14px;overflow:hidden;box-shadow:0 1px 4px rgba(0,0,0,.08)">'
        '<div style="background:#4f46e5;padding:22px 28px">'
        '<div style="color:#fff;font-size:18px;font-weight:800;letter-spacing:.3px">Altria Call Center</div>'
        '<div style="color:#c7d2fe;font-size:13px;margin-top:2px">Daily Network Speed Report</div>'
        '</div>'
        f'<div style="padding:20px 28px 6px"><div style="color:#6b7280;font-size:13px">Report generated</div>'
        f'<div style="color:#111827;font-size:15px;font-weight:600">{escape(ts)}</div></div>'
        '<div style="padding:12px 28px 24px">'
        '<table style="width:100%;border-collapse:collapse;margin-top:8px">'
        '<thead><tr style="text-align:left">'
        '<th style="padding:8px 16px;color:#9ca3af;font-size:11px;text-transform:uppercase;letter-spacing:.5px;border-bottom:2px solid #eef0f4">Gateway</th>'
        '<th style="padding:8px 16px;color:#9ca3af;font-size:11px;text-transform:uppercase;letter-spacing:.5px;border-bottom:2px solid #eef0f4">Interface</th>'
        '<th style="padding:8px 16px;color:#9ca3af;font-size:11px;text-transform:uppercase;letter-spacing:.5px;border-bottom:2px solid #eef0f4;text-align:right">Download</th>'
        f'</tr></thead><tbody>{rows}</tbody></table>'
        '</div>'
        '<div style="padding:16px 28px;background:#fafafa;border-top:1px solid #eef0f4;color:#9ca3af;font-size:12px;text-align:center">'
        'Automated report from Altria CRM · Network Monitor</div>'
        '</div></div>'
    )


def report_emails(request):
    """Get or set the daily-report recipient list (stored in the CRM, so you
    manage all recipients here instead of in the pfSense script).
    View:  /report-emails/?secret=...
    Set:   /report-emails/?secret=...&emails=a@x.com,b@y.com,c@z.com"""
    import json, os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403, content_type='application/json')
    from core.models import SystemConfig
    cfg = SystemConfig.get()
    if 'emails' in request.GET:
        raw = request.GET.get('emails', '').replace(';', ',')
        clean = ', '.join([e.strip() for e in raw.split(',') if e.strip()])
        cfg.report_emails = clean
        cfg.save(update_fields=['report_emails'])
    current = [e.strip() for e in (cfg.report_emails or '').split(',') if e.strip()]
    return HttpResponse(json.dumps({'ok': True, 'recipients': current}),
                        content_type='application/json')


@csrf_exempt
def network_speedtest_report(request):
    """Receive a daily speed-test summary (from the pfSense speedtest script):
    create ONE ticket and email the summary to the recipients configured in the
    CRM (SystemConfig.report_emails), falling back to any address in the POST."""
    import json, os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403, content_type='application/json')
    try:
        data = json.loads(request.body.decode() or '{}')
    except Exception:
        data = {}
    subject = (data.get('subject') or 'Daily Network Speed Report')[:500]
    body = data.get('body') or ''

    # Route to the specific client this pfSense belongs to (its id is baked into
    # the installed script). Recipients come from that client's configuration.
    from clients.models import Client, ClientConfiguration
    from tickets.models import Ticket
    client = None
    client_id = data.get('client')
    if client_id:
        client = Client.objects.filter(pk=client_id).first()
    if not client:
        client = Client.objects.filter(is_active=True).first()
    recipients = []
    if client:
        cfg = ClientConfiguration.objects.filter(client=client).first()
        src = ((cfg.report_emails if cfg else '') or (data.get('email') or ''))
        recipients = [e.strip() for e in src.replace(';', ',').split(',') if e.strip()]

    # Store one row per gateway measured, rather than a ticket. This is what
    # makes the speed history chartable and keeps the queue clean.
    from clients.models import NetworkSpeedTest, network_for_gateway
    from core.jobs import record_job
    from django.utils import timezone
    import re as _re
    # Expected once a day; flag it overdue after ~2 days of silence.
    with record_job('speed_report', every_minutes=1440) as _job:
        _job.detail = subject[:200]
    stored = 0
    for line in (body or '').splitlines():
        # Lines look like:  WANPLDTGW (igb1): 92.4 Mbps
        m = _re.match(r'\s*([^\s(]+)\s*\([^)]*\):\s*(.+?)\s*$', line)
        if not m:
            continue
        gw, val = m.group(1), m.group(2)
        speed = _re.match(r'([\d.]+)\s*Mbps', val)
        # Each gateway lands on its own network when one claims that name.
        NetworkSpeedTest.objects.create(
            client=client, network=network_for_gateway(client.pk, gw), gateway=gw[:120],
            mbps=float(speed.group(1)) if speed else None,
            raw=val[:200], tested_at=timezone.now(),
        )
        stored += 1
    emailed, email_error = False, ''
    if recipients:
        try:
            from django.core.mail import EmailMultiAlternatives
            from django.conf import settings
            html = _speedtest_email_html(subject, body)
            msg = EmailMultiAlternatives(
                subject, body, getattr(settings, 'DEFAULT_FROM_EMAIL', None), recipients)
            msg.attach_alternative(html, 'text/html')
            msg.send(fail_silently=False)
            emailed = True
        except Exception as e:
            email_error = str(e)
    return HttpResponse(json.dumps({'ok': True, 'gateways_recorded': stored,
                                    'emailed': emailed, 'email_error': email_error}),
                        content_type='application/json')


def job_health(request):
    """Health of every scheduled job — the thing that was missing when the AI
    reply sweep and the speed report each failed silently for days."""
    import json
    denied = _deny_unless_ops_secret(request)
    if denied:
        return denied
    from core.models import JobRun
    jobs = [{
        'name': j.name,
        'status': j.status,
        'overdue': j.is_overdue,
        'last_run_at': j.last_run_at.isoformat() if j.last_run_at else None,
        'last_ok_at': j.last_ok_at.isoformat() if j.last_ok_at else None,
        'expected_every_minutes': j.expected_every_minutes,
        'runs': j.run_count,
        'failures': j.fail_count,
        'detail': j.detail,
        'error': j.error[:300],
    } for j in JobRun.objects.all()]
    unhealthy = [j['name'] for j in jobs if j['overdue'] or j['status'] != 'ok']
    return HttpResponse(
        json.dumps({'ok': not unhealthy, 'unhealthy': unhealthy, 'jobs': jobs}, indent=2),
        content_type='application/json',
        status=200 if not unhealthy else 503)


def speedtest_pending(request):
    """pfSense polls this every minute. Returns whether an on-demand "Speed Test
    Now" was requested, and clears the flag so it runs only once."""
    import json, os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse(json.dumps({'error': 'Unauthorized'}), status=403, content_type='application/json')
    # Per-client flag: the installed pfSense script polls with its own client id.
    from clients.models import ClientConfiguration
    from django.utils import timezone
    client_id = request.GET.get('client')
    cfg = ClientConfiguration.objects.filter(client_id=client_id).first() if client_id else None
    pending = bool(cfg.speedtest_requested) if cfg else False
    if cfg:
        # This poll is the only proof a probe exists for this client. Recording
        # it lets the UI say whether a requested test can actually run.
        fields = ['probe_last_seen']
        cfg.probe_last_seen = timezone.now()
        if pending:
            cfg.speedtest_requested = False
            fields.append('speedtest_requested')
        cfg.save(update_fields=fields)
    return HttpResponse(json.dumps({'pending': pending}), content_type='application/json')


def pfsense_speedtest_script(request):
    """Serve the per-gateway speed-test script for pfSense, with the CRM URLs,
    secret and CLIENT id baked in. Runs every minute: fires at 8 PM PHT daily,
    or on demand when that client's "Speed Test Now" button sets the flag."""
    import os
    secret = request.GET.get('secret', '')
    expected = os.environ.get('EMAIL_WEBHOOK_SECRET', '')
    if not expected or secret != expected:
        return HttpResponse('Unauthorized', status=403, content_type='text/plain')
    email = request.GET.get('email', '')
    client_id = request.GET.get('client', '')
    report_url = f"https://crm.altriacallcenter.com/network-speedtest-report/?secret={expected}"
    pending_url = f"https://crm.altriacallcenter.com/speedtest-pending/?secret={expected}&client={client_id}"
    script = (PFSENSE_SPEEDTEST_TEMPLATE
              .replace('__REPORT_URL__', report_url)
              .replace('__PENDING_URL__', pending_url)
              .replace('__CLIENT__', client_id)
              .replace('__EMAIL__', email))
    return HttpResponse(script, content_type='text/plain')


PFSENSE_SPEEDTEST_TEMPLATE = r'''#!/bin/sh
# Daily per-gateway speed test -> CRM ticket + email summary (auto-generated).
# Runs HOURLY via cron but only fires at 8 PM Manila time — so you never have to
# match the pfSense system timezone. Run with argument "now" to test on demand.
# Install:
#   curl -s "https://crm.altriacallcenter.com/pfsense-speedtest-script/?secret=YOURSECRET&email=you@example.com" -o /usr/local/bin/crm_speedtest_daily.sh
#   chmod +x /usr/local/bin/crm_speedtest_daily.sh
# pfSense Cron (every minute):  * * * * *  root  /usr/local/bin/crm_speedtest_daily.sh
REPORT_URL="__REPORT_URL__"
PENDING_URL="__PENDING_URL__"
REPORT_EMAIL="__EMAIL__"
CLIENT_ID="__CLIENT__"
STREAMS=6                                              # parallel downloads per gateway (Ookla-style)
TEST_FILE="https://speed.cloudflare.com/__down?bytes=25000000"   # per stream

# Decide whether to run this minute:
#   - "now" argument           -> always (manual test)
#   - the 20:00 Manila hour    -> the scheduled daily run, once per day
#   - on-demand button request -> the CRM flag is set (checked every minute)
#
# The daily run is gated on the DATE, not on the clock reading exactly "20:00".
# Matching a single minute meant one missed tick — a reboot, a busy box, an
# overlapping run, a moment of cron skew — silently skipped that whole day's
# report with no error and no retry. Now any minute in the 20:00 hour will fire
# it if today's report hasn't gone out yet, and it still fires only once.
STATE_DIR="/var/db"
[ -d "$STATE_DIR" ] || STATE_DIR="/tmp"
LAST_RUN_FILE="$STATE_DIR/crm_speedtest_lastrun"
TODAY=$(TZ='Asia/Manila' date '+%Y-%m-%d')
HOUR=$(TZ='Asia/Manila' date '+%H')
if [ -f "$LAST_RUN_FILE" ]; then LAST_RUN=$(cat "$LAST_RUN_FILE"); else LAST_RUN=""; fi

RUN=0
[ "$1" = "now" ] && RUN=1
if [ "$HOUR" = "20" ] && [ "$LAST_RUN" != "$TODAY" ]; then
    RUN=1
    SCHEDULED=1
fi
if [ "$RUN" = "0" ]; then
    echo "$(curl -s --max-time 8 "$PENDING_URL")" | grep -qi '"pending":[[:space:]]*true' && RUN=1
fi
[ "$RUN" = "1" ] || exit 0

# Claim the day BEFORE testing: a speed test takes ~25s, so two ticks could
# otherwise start overlapping runs and post two reports.
[ "$SCHEDULED" = "1" ] && echo "$TODAY" > "$LAST_RUN_FILE"
TIMESTAMP=$(TZ='Asia/Manila' date '+%Y-%m-%d %H:%M PHT')
SUMFILE=$(mktemp)

# gateway name | real interface | status
GW_INFO=$(/usr/local/bin/php -r "
require_once('/etc/inc/gwlb.inc');
require_once('/etc/inc/interfaces.inc');
\$gws = return_gateways_array();
\$st  = return_gateways_status(true);
foreach (\$gws as \$name => \$gw) {
    \$rif = get_real_interface(\$gw['interface']);
    \$status = strtolower(\$st[\$name]['status'] ?? 'none');
    echo \$name . '|' . \$rif . '|' . \$status . PHP_EOL;
}
" 2>/dev/null)

echo "$GW_INFO" | while IFS='|' read -r GW RIF STATUS; do
    [ -z "$GW" ] && continue
    if echo "$STATUS" | grep -qiE 'down|loss|delay|alarm'; then
        SPD="DOWN (skipped)"
    else
        # Parallel streams (like Ookla) — run $STREAMS downloads at once through
        # this WAN and SUM their speeds, so we measure the link's real capacity
        # instead of a single-connection reading.
        TMPD=$(mktemp -d)
        n=0
        while [ $n -lt "$STREAMS" ]; do
            curl --interface "$RIF" -o /dev/null -s -w '%{speed_download}\n' --max-time 25 "$TEST_FILE" > "$TMPD/$n" 2>/dev/null &
            n=$((n + 1))
        done
        wait
        BPS=$(cat "$TMPD"/* 2>/dev/null | awk '{s += $1} END {printf "%.0f", s + 0}')
        rm -rf "$TMPD"
        SPD=$(echo "$BPS" | awk '{if($1>0) printf "%.1f Mbps", $1*8/1000000; else printf "unavailable"}')
    fi
    printf '%s (%s): %s\n' "$GW" "$RIF" "$SPD" >> "$SUMFILE"
done

RESULTS=$(cat "$SUMFILE"); rm -f "$SUMFILE"
BODY="Daily Network Speed Report\nTime: $TIMESTAMP\n\n$RESULTS"
BODY_ESC=$(printf '%s' "$BODY" | sed 's/"/\\"/g' | awk '{printf "%s\\n", $0}' | tr -d '\n')
SUBJECT="Daily Network Speed Report - $TIMESTAMP"
JSON="{\"subject\":\"$SUBJECT\",\"body\":\"$BODY_ESC\",\"email\":\"$REPORT_EMAIL\",\"client\":\"$CLIENT_ID\"}"
HTTP=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$REPORT_URL" \
       -H "Content-Type: application/json" -d "$JSON" --max-time 60)
echo "[$TIMESTAMP] scheduled=$SCHEDULED http=$HTTP $RESULTS" >> /var/log/crm_speedtest.log

# If the CRM rejected or was unreachable, release the day's claim so a later
# tick in the 20:00 hour retries instead of losing the report entirely.
if [ "$SCHEDULED" = "1" ] && [ "$HTTP" != "200" ]; then
    rm -f "$LAST_RUN_FILE"
fi
'''


def diag_clientconfig(request):
    denied = _deny_unless_ops_secret(request)
    if denied:
        return denied
    import json
    result = {}
    try:
        from django.db import connection
        with connection.cursor() as cur:
            cur.execute("SHOW COLUMNS FROM clients_clientconfiguration")
            result['columns'] = [row[0] for row in cur.fetchall()]
    except Exception as e:
        result['columns_error'] = str(e)
    try:
        from clients.models import ClientConfiguration
        from clients.serializers import ClientConfigurationSerializer
        cfg = ClientConfiguration.objects.first()
        if cfg:
            result['serializer_ok'] = True
            result['client_id'] = cfg.client_id
        else:
            result['serializer_ok'] = 'no records'
    except Exception as e:
        result['serializer_error'] = str(e)
    try:
        from clients.views import ClientConfigView
        result['views_import'] = 'OK'
    except Exception as e:
        result['views_import_error'] = str(e)
    return HttpResponse(f'<pre>{json.dumps(result, indent=2)}</pre>')


def debug_full(request):
    import sys, json, subprocess
    result = {}

    # Python & environment
    result['python_version'] = sys.version
    result['python_executable'] = sys.executable
    result['sys_path'] = sys.path

    # Django settings
    result['BASE_DIR'] = str(settings.BASE_DIR)
    result['DEBUG'] = settings.DEBUG
    result['ALLOWED_HOSTS'] = getattr(settings, 'ALLOWED_HOSTS', [])
    result['INSTALLED_APPS'] = list(getattr(settings, 'INSTALLED_APPS', []))

    # Env vars (safe ones only)
    import os as _os
    result['env'] = {
        k: v for k, v in _os.environ.items()
        if k in ('DJANGO_SETTINGS_MODULE', 'PYTHONPATH', 'HOME', 'PWD', 'PATH',
                  'PASSENGER_APP_ENV', 'PORT', 'WSGI_MODULE')
    }

    # Database check
    try:
        from django.db import connection
        with connection.cursor() as cursor:
            cursor.execute("SELECT 1")
        result['db'] = 'OK'
        result['db_engine'] = settings.DATABASES['default']['ENGINE']
        result['db_name'] = settings.DATABASES['default'].get('NAME', '')
    except Exception as e:
        result['db'] = f'ERROR: {e}'

    # Installed packages
    try:
        import pkg_resources
        pkgs = {p.project_name: p.version for p in pkg_resources.working_set}
        key_packages = ['Django', 'djangorestframework', 'djangorestframework-simplejwt',
                        'mysqlclient', 'PyMySQL', 'Pillow', 'celery', 'redis',
                        'django-cors-headers', 'django-filter', 'openai', 'requests']
        result['packages'] = {k: pkgs.get(k, 'NOT INSTALLED') for k in key_packages}
        result['all_packages_count'] = len(pkgs)
    except Exception as e:
        result['packages'] = f'ERROR: {e}'

    # File system checks
    dist_dir = DIST_DIR
    dist2_dir = os.path.join(str(settings.BASE_DIR), 'dist2')
    result['dist_exists'] = os.path.exists(dist_dir)
    result['dist2_exists'] = os.path.exists(dist2_dir)
    result['dist_index'] = os.path.exists(os.path.join(dist_dir, 'index.html'))
    result['dist2_index'] = os.path.exists(os.path.join(dist2_dir, 'index.html'))
    try:
        result['dist_files'] = os.listdir(dist_dir) if os.path.exists(dist_dir) else []
        result['dist2_files'] = os.listdir(dist2_dir) if os.path.exists(dist2_dir) else []
    except Exception as e:
        result['dist_files'] = str(e)

    # passenger_wsgi.py check
    wsgi_path = os.path.join(str(settings.BASE_DIR), 'passenger_wsgi.py')
    result['passenger_wsgi_exists'] = os.path.exists(wsgi_path)
    if os.path.exists(wsgi_path):
        try:
            with open(wsgi_path) as f:
                result['passenger_wsgi_content'] = f.read()
        except Exception as e:
            result['passenger_wsgi_content'] = str(e)

    # requirements.txt check
    req_path = os.path.join(str(settings.BASE_DIR), 'requirements.txt')
    result['requirements_exists'] = os.path.exists(req_path)

    # .env file check (existence only, not contents)
    env_path = os.path.join(str(settings.BASE_DIR), '.env')
    result['dotenv_exists'] = os.path.exists(env_path)

    # Django system check
    try:
        from django.core.management import call_command
        from io import StringIO
        out = StringIO()
        call_command('check', stdout=out, stderr=out)
        result['django_check'] = out.getvalue() or 'OK'
    except Exception as e:
        result['django_check'] = f'ERROR: {e}'

    # Migrations status
    try:
        from io import StringIO
        out = StringIO()
        from django.core.management import call_command
        call_command('showmigrations', '--list', stdout=out, stderr=out)
        lines = out.getvalue().splitlines()
        unapplied = [l for l in lines if '[ ]' in l]
        result['unapplied_migrations'] = unapplied if unapplied else 'All applied'
    except Exception as e:
        result['unapplied_migrations'] = f'ERROR: {e}'

    html = f'<pre style="font-family:monospace;font-size:13px;white-space:pre-wrap">{json.dumps(result, indent=2, default=str)}</pre>'
    return HttpResponse(html)


router = DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'roles', RoleViewSet)
router.register(r'clients', ClientViewSet)
router.register(r'client-contacts', ClientContactViewSet)
router.register(r'client-departments', ClientDepartmentViewSet)
router.register(r'ticket-categories', TicketCategoryViewSet)
router.register(r'ticket-sub-categories', TicketSubCategoryViewSet)
router.register(r'escalation-matrix', EscalationMatrixViewSet)
router.register(r'tickets', TicketViewSet)
router.register(r'sla-policies', SLAPolicyViewSet)
router.register(r'sla-breaches', SLABreachLogViewSet)
router.register(r'sla-pauses', SLAPauseViewSet)
router.register(r'business-hours', BusinessHourViewSet)
router.register(r'workflows', WorkflowViewSet)
router.register(r'workflow-steps', WorkflowStepViewSet)
router.register(r'approvals', ApprovalRequestViewSet)
router.register(r'audit-logs', AuditLogViewSet)
router.register(r'notifications', NotificationViewSet, basename='notification')
router.register(r'notification-templates', NotificationTemplateViewSet)
router.register(r'knowledge', KnowledgeArticleViewSet)
router.register(r'reply-templates', ReplyTemplateViewSet)
router.register(r'client-databases', ClientDatabaseViewSet, basename='clientdatabase')
router.register(r'client-email-accounts', ClientEmailAccountViewSet, basename='clientemailaccount')
router.register(r'client-networks', ClientNetworkViewSet, basename='clientnetwork')
router.register(r'outbound-emails', OutboundEmailViewSet, basename='outboundemail')

urlpatterns = [
    path('', lambda r: HttpResponseRedirect('/login'), name='home'),
    path('debug-paths/', debug_paths, name='debug_paths'),
    path('auto-fetch-emails/', auto_fetch_emails, name='auto_fetch_emails'),
    path('fix-charset/', fix_charset, name='fix_charset'),
    path('monitor-networks/', monitor_networks, name='monitor_networks'),
    path('pfsense-script/', pfsense_script, name='pfsense_script'),
    path('pfsense-speedtest-script/', pfsense_speedtest_script, name='pfsense_speedtest_script'),
    path('network-speedtest-report/', network_speedtest_report, name='network_speedtest_report'),
    path('speedtest-pending/', speedtest_pending, name='speedtest_pending'),
    path('job-health/', job_health, name='job_health'),
    path('report-emails/', report_emails, name='report_emails'),
    path('backfill-channel-source/', backfill_channel_source, name='backfill_channel_source'),
    path('diag-clientconfig/', diag_clientconfig, name='diag_clientconfig'),
    path('create-client-user/', create_client_user, name='create_client_user'),
    # Before the SPA catch-all, or the sign-in link would render the app shell
    # instead of signing anyone in.
    path('sso/', sso_enter, name='sso_enter'),
    path('admin/', admin.site.urls),
    path('api/', include(router.urls)),

    # Auth
    path('api/auth/login/', CustomTokenObtainPairView.as_view(), name='token_obtain'),
    path('api/auth/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('api/client-login-logs/<int:client_id>/', ClientLoginLogsView.as_view(), name='client_login_logs'),
    path('api/client-email-reports/<int:client_id>/', ClientEmailReportsView.as_view(), name='client_email_reports'),
    path('api/auth/client-invite/', ClientInviteView.as_view(), name='client_invite'),
    path('api/auth/client-invite/verify/', ClientInviteVerifyView.as_view(), name='client_invite_verify'),
    path('api/auth/client-register/', ClientInviteRegisterView.as_view(), name='client_register'),

    # Reports
    path('api/reports/dashboard/', DashboardReportView.as_view(), name='dashboard_report'),
    path('api/reports/sla/', SLAReportView.as_view(), name='sla_report'),
    path('api/reports/agent-performance/', AgentPerformanceReportView.as_view(), name='agent_report'),

    # AI Services
    path('api/system-config/', SystemConfigView.as_view(), name='system_config'),
    path('api/system-config/test/', SystemConfigTestView.as_view(), name='system_config_test'),
    path('api/ai/reply-assistant/', AIReplyAssistantView.as_view(), name='ai_reply'),
    # Search past AI replies — a plain DB query, works with AI switched off.
    path('api/ai/reply-library/', AIReplyLibraryView.as_view(), name='ai_reply_library'),
    path('api/ai/classify/', AIClassifyTicketView.as_view(), name='ai_classify'),
    path('api/ai/quality-review/', AIQualityReviewView.as_view(), name='ai_qa'),
    path('api/ai/report-summary/', AIReportSummaryView.as_view(), name='ai_report'),
    path('api/ai/assistant/', AIAssistantView.as_view(), name='ai_assistant'),
    path('api/ai/outbound-draft/', AIOutboundDraftView.as_view(), name='ai_outbound_draft'),

    # VICIdial Integration
    path('api/vicidial/config/', VICIDialConfigView.as_view(), name='vici_config'),
    path('api/vicidial/status/', VICIDialStatusView.as_view(), name='vici_status'),
    path('api/vicidial/call-pop/', VICIDialCallPopView.as_view(), name='vici_callpop'),
    path('api/vicidial/disposition/', VICIDialDispositionView.as_view(), name='vici_disposition'),
    path('api/vicidial/recording/', VICIDialRecordingView.as_view(), name='vici_recording'),

    # Client Configuration
    path('api/client-config/<int:client_id>/', ClientConfigView.as_view(), name='client_config'),
    path('api/client-pinktool/<int:client_id>/', PinkToolStatsView.as_view(), name='client_pinktool'),
    path('api/client-network/<int:client_id>/', NetworkStatusView.as_view(), name='client_network'),

    # Webhooks
    path('api/webhooks/email/', WebhookInboundEmailView.as_view(), name='webhook_email'),
    path('api/webhooks/social/', WebhookSocialMediaView.as_view(), name='webhook_social'),

    # Serve React build static assets (JS, CSS, fonts, images)
    re_path(r'^(?P<path>assets/.+)$', serve_dist_file, name='react_assets'),

    # Self-hosted help pages (dist/help/*.html). Kept in the CRM rather than an
    # external link so the guide can't rot or become unreachable for a client.
    # Must precede the SPA catch-all, which would otherwise swallow it.
    re_path(r'^(?P<path>help/.+\.html)$', serve_dist_file, name='help_pages'),

    # Catch-all: serve React app for any frontend route (must be last)
    re_path(r'^(?!api/|admin/|static/|media/|assets/|setup-admin/|run-migrate/|debug-paths/).*$', react_app, name='spa'),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
