"""
Email -> ticket ingestion (Freshdesk-style).

Design goals (why this looks the way it does):
  * Do NOT rely on the IMAP \\Seen flag. Webmail previews or another IMAP
    client flip \\Seen, which used to make messages get skipped forever. We
    select the mailbox READ-ONLY and never change flags.
  * Track the highest processed IMAP UID per account (``last_uid``) and only
    fetch UIDs above it. UIDVALIDITY changes reset the high-water mark.
  * Idempotent: every message is deduped by its ``Message-ID`` so reprocessing
    the same email can never create a duplicate ticket/comment.
  * Threading priority (like Freshdesk):
        1. an explicit ``[#TKT...]`` / ``TKT...`` token in the subject
        2. In-Reply-To / References headers matching a stored Message-ID
        3. same base subject + same sender with an open ticket
        4. otherwise -> brand new ticket
  * A reply to a resolved/closed ticket reopens it.
"""
import imaplib
import email as email_lib
import re
from email.header import decode_header
from email.utils import parseaddr
from datetime import timedelta
from django.utils import timezone
from django.core.management.base import BaseCommand
from clients.models import ClientEmailAccount
from tickets.models import Ticket, TicketComment


FETCH_LIMIT = 50  # max messages processed per account per run
INITIAL_SYNC_DAYS = 7  # on first connect, only import mail from the last N days
CLOSED_STATUSES = ['closed', 'cancelled', 'resolved']
TICKET_TOKEN_RE = re.compile(r'TKT\d{6,}', re.IGNORECASE)
MSGID_RE = re.compile(r'<[^>]+>')
SUBJECT_PREFIX_RE = re.compile(r'^(Re|Fwd|FW|RE|FWD)\s*:\s*', re.IGNORECASE)

# ── Priority classification from email content ──
# Keyword rules applied to the SUBJECT (concise and intentional). We avoid
# scanning the body because marketing/newsletter footers contain noisy words
# like "legal", "breach", "download" that cause false positives. First matching
# tier (highest first) wins; nothing matched -> 'medium' (previous behaviour).
# Deterministic and needs no API key; an AI classifier could replace this later.
# Multi-word phrases are used where a single word would be ambiguous.
PRIORITY_KEYWORDS = [
    ('critical', [
        'outage', 'service down', 'system down', 'server down', 'site down',
        'data breach', 'security breach', 'data loss', 'hacked', 'ransomware',
        'cannot access', "can't access", 'not working', 'emergency',
    ]),
    ('high', [
        'urgent', 'asap', 'immediately', 'escalate', 'escalation', 'complaint',
        'refund', 'chargeback', 'past due', 'overdue', 'action required',
        'cancel my', 'cancel order', 'cancelled', 'cancellation', 'canceled',
        'deadline', 'time sensitive', 'high priority', 'service issue',
        'not happy', 'unacceptable',
    ]),
    ('low', [
        'newsletter', 'welcome', 'promotion', 'promo', '% off', 'sale',
        'unsubscribe', 'no-reply', 'noreply', 'notification', 'digest',
        'report domain', 'report-id', 'daily network speed report', 'dmarc',
        'aggregate report', 'webinar', 'event', 'invitation', 'verify',
        'single-use code', 'security alert', 'fyi', 'thank you', 'thanks',
    ]),
]


def classify_priority(subject, body=None):
    """Return 'critical' | 'high' | 'medium' | 'low' from the email subject.

    Checks critical -> high -> low in order and returns the first tier whose
    keywords appear in the subject. Defaults to 'medium' when nothing matches.
    ``body`` is accepted for signature compatibility but intentionally unused
    (bodies are too noisy for reliable keyword matching).
    """
    text = (subject or '').lower()
    for level, words in PRIORITY_KEYWORDS:
        if any(w in text for w in words):
            return level
    return 'medium'


def _decode(val):
    if not val:
        return ''
    out = []
    for b, enc in decode_header(val):
        if isinstance(b, bytes):
            out.append(b.decode(enc or 'utf-8', errors='replace'))
        else:
            out.append(b)
    return ''.join(out)


def _base_subject(subject):
    """Strip any number of Re:/Fwd: prefixes for threading comparison."""
    s = subject or ''
    while True:
        stripped = SUBJECT_PREFIX_RE.sub('', s, count=1)
        if stripped == s:
            return s.strip()
        s = stripped


def _extract_body(msg):
    body = ''
    if msg.is_multipart():
        for part in msg.walk():
            ct = part.get_content_type()
            disp = part.get('Content-Disposition')
            if ct == 'text/plain' and not disp:
                payload = part.get_payload(decode=True)
                if payload:
                    body = payload.decode(part.get_content_charset() or 'utf-8', errors='replace')
                    break
            elif ct == 'text/html' and not body and not disp:
                payload = part.get_payload(decode=True)
                if payload:
                    body = payload.decode(part.get_content_charset() or 'utf-8', errors='replace')
    else:
        payload = msg.get_payload(decode=True)
        if payload:
            body = payload.decode(msg.get_content_charset() or 'utf-8', errors='replace')
    return body


# Attachments we refuse to store, whatever the sender calls them. Support mail
# regularly carries these and nothing good comes of keeping them on disk.
BLOCKED_ATTACHMENT_EXT = {
    '.exe', '.dll', '.scr', '.com', '.bat', '.cmd', '.pif', '.vbs', '.vbe',
    '.js', '.jse', '.jar', '.msi', '.ps1', '.sh', '.hta', '.cpl', '.lnk',
}
MAX_ATTACHMENT_BYTES = 15 * 1024 * 1024   # 15 MB per file


def _save_attachments(msg, ticket):
    """Persist a message's real attachments against the ticket.

    Customers routinely send screenshots — and the AI asks for them — but until
    now every attachment was dropped on the floor because the fetcher only
    decoded text parts. Returns the number stored.

    Deliberately conservative: skips inline images used for signatures/logos,
    refuses executable types, caps file size, and never lets a sender-supplied
    filename influence the path on disk.
    """
    import os
    from django.core.files.base import ContentFile
    from tickets.models import TicketAttachment

    if not msg.is_multipart():
        return 0

    saved = 0
    for part in msg.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        disp = (part.get('Content-Disposition') or '')
        # Only real attachments. An inline part with no filename is body content
        # (or a signature logo), not something an agent needs.
        filename = part.get_filename()
        if not filename or 'attachment' not in disp.lower():
            continue

        filename = _decode(filename)
        # Sender-controlled: strip any path component so it can't escape MEDIA_ROOT.
        safe_name = os.path.basename(filename.replace('\\', '/')).strip() or 'attachment'
        ext = os.path.splitext(safe_name)[1].lower()
        if ext in BLOCKED_ATTACHMENT_EXT:
            continue

        try:
            payload = part.get_payload(decode=True)
        except Exception:
            continue
        if not payload or len(payload) > MAX_ATTACHMENT_BYTES:
            continue

        try:
            att = TicketAttachment(
                ticket=ticket, uploaded_by=None, filename=safe_name[:255],
                file_size=len(payload), file_type=(part.get_content_type() or '')[:50],
                tags='inbound-email',
            )
            att.file.save(safe_name[:120], ContentFile(payload), save=False)
            att.save()
            saved += 1
        except Exception:
            # One bad attachment must never cost us the whole email.
            continue
    return saved


def _referenced_ids(msg):
    """All Message-IDs referenced by In-Reply-To + References headers."""
    raw = ' '.join(filter(None, [msg.get('In-Reply-To', ''), msg.get('References', '')]))
    return MSGID_RE.findall(raw)


def _find_parent(acct, msg, subject, from_email):
    """Return the ticket a message belongs to, or None for a new ticket."""
    # 1. Explicit ticket token in the subject line
    tok = TICKET_TOKEN_RE.search(subject or '')
    if tok:
        t = Ticket.objects.filter(client=acct.client,
                                  ticket_number__iexact=tok.group(0)).first()
        if t:
            return t

    # 2. Reply headers -> a stored Message-ID (on a ticket or a comment)
    ref_ids = _referenced_ids(msg)
    if ref_ids:
        t = Ticket.objects.filter(client=acct.client,
                                  source_message_id__in=ref_ids).first()
        if t:
            return t
        c = (TicketComment.objects.filter(ticket__client=acct.client,
                                          source_message_id__in=ref_ids)
             .select_related('ticket').first())
        if c:
            return c.ticket

    # 3. Same base subject + same sender — only when the subject was explicitly a
    #    reply/forward ("Re:"/"Fwd:"). A brand-new email with a plain subject
    #    (even if it matches an open ticket's subject) starts its OWN ticket, so
    #    two separate "Refund" emails are NOT merged. Genuine replies that lost
    #    their headers still thread because they carry a "Re:" prefix.
    if SUBJECT_PREFIX_RE.match(subject or ''):
        base = _base_subject(subject)
        if base:
            return (Ticket.objects.filter(client=acct.client, channel='email',
                                          customer_email__iexact=from_email,
                                          subject__iexact=base)
                    .exclude(status__in=CLOSED_STATUSES)
                    .order_by('-created_at').first())
    return None


def _link_outbound_reply(ticket, ref_ids):
    """Attach a freshly created ticket to the outbound email it answers.

    ``ref_ids`` are the Message-IDs from In-Reply-To/References. Only links a
    row for the same client, and only one that is not already linked, so a long
    thread does not keep re-pointing the original outbound email.
    """
    if not ref_ids:
        return
    from clients.models import OutboundEmail
    ob = OutboundEmail.objects.filter(
        client=ticket.client, status=OutboundEmail.SENT,
        message_id__in=ref_ids, ticket__isnull=True,
    ).first()
    if not ob:
        return
    ob.ticket = ticket
    ob.save(update_fields=['ticket'])
    # Carry the agent who started the conversation onto the ticket, so the reply
    # lands with the person who already has the context.
    if ob.sender_id and not ticket.assigned_agent_id:
        ticket.assigned_agent_id = ob.sender_id
        ticket.save(update_fields=['assigned_agent'])


def process_message(acct, raw, results):
    """Parse one raw RFC822 message and create/append a ticket. Idempotent."""
    msg = email_lib.message_from_bytes(raw)
    message_id = (msg.get('Message-ID', '') or '').strip()

    # Idempotent dedup by Message-ID (safe to reprocess the same email)
    if message_id and (
        Ticket.objects.filter(source_message_id=message_id).exists()
        or TicketComment.objects.filter(source_message_id=message_id).exists()
    ):
        results['duplicates'] += 1
        return

    subject = _decode(msg.get('Subject', '(No Subject)'))[:500]
    from_name, from_email = parseaddr(_decode(msg.get('From', '')))
    from_email = from_email or _decode(msg.get('From', ''))
    body = _extract_body(msg)

    parent = _find_parent(acct, msg, subject, from_email)

    if parent:
        # Fallback dedup when the email had no Message-ID: avoid re-adding the
        # same body as a comment.
        if not message_id and TicketComment.objects.filter(
            ticket=parent, body__icontains=body[:100].strip()
        ).exists():
            results['duplicates'] += 1
            return
        TicketComment.objects.create(
            ticket=parent,
            body=body[:5000],
            comment_type='customer_reply',
            author=None,
            source_message_id=message_id,
        )
        # A reply can carry the screenshot the first mail forgot.
        _save_attachments(msg, parent)
        # Bump activity time so the ticket rises to the top of the list
        # (Freshdesk-style "most recently active first"). 'updated_at' is
        # auto_now, so listing it in update_fields refreshes it.
        update_fields = ['updated_at']
        # Reopen a resolved/closed ticket on customer reply (Freshdesk behavior)
        if parent.status in CLOSED_STATUSES:
            parent.status = 'open'
            parent.reopen_count = (parent.reopen_count or 0) + 1
            update_fields += ['status', 'reopen_count']
        parent.save(update_fields=update_fields)
        results['threaded'] += 1
        # Continue the two-way conversation: let the AI agent reply to this
        # customer follow-up too (not just the first email). Never break ingestion.
        try:
            from ai_services.auto_reply import auto_reply_to_ticket
            auto_reply_to_ticket(parent)
        except Exception:
            pass
    else:
        kwargs = dict(
            client=acct.client,
            subject=subject,
            description=body[:5000],
            customer_name=from_name or from_email,
            customer_email=from_email,
            channel='email',
            channel_source=acct.name,
            status='open',
            priority=classify_priority(subject, body),
            source_message_id=message_id,
        )
        if acct.assigned_agent_id:
            kwargs['assigned_agent'] = acct.assigned_agent
        ticket = Ticket.objects.create(**kwargs)
        _save_attachments(msg, ticket)
        _assign_sla(ticket)
        # If this is the customer answering an email an agent sent them, tie the
        # new ticket to that outbound message. The outbound leg deliberately
        # creates no ticket, so without this link the agent has no way to see
        # that the ticket in front of them is the reply to their own email.
        try:
            _link_outbound_reply(ticket, _referenced_ids(msg))
        except Exception:
            pass
        # AI auto-reply agent: recognise the concern and reply (auto-send when
        # highly confident, else draft for review). Never break ingestion.
        try:
            from ai_services.auto_reply import auto_reply_to_ticket
            auto_reply_to_ticket(ticket)
        except Exception:
            pass
        results['created'] += 1


def _assign_sla(ticket):
    """Attach the best-matching active SLA policy and set the due dates.

    Matches on the ticket's priority (category 'general' for email), falling
    back to any active policy for the client. No-op if the client has none.
    """
    import datetime
    from sla.models import SLAPolicy

    policy = (
        SLAPolicy.objects.filter(client=ticket.client, priority=ticket.priority,
                                 category='general', is_active=True).first()
        or SLAPolicy.objects.filter(client=ticket.client, priority=ticket.priority,
                                    is_active=True).first()
        or SLAPolicy.objects.filter(client=ticket.client, is_active=True).first()
    )
    if not policy:
        return
    now = timezone.now()
    ticket.sla_policy = policy
    ticket.first_response_due = now + datetime.timedelta(minutes=policy.first_response_minutes)
    ticket.resolution_due = now + datetime.timedelta(minutes=policy.resolution_minutes)
    ticket.save(update_fields=['sla_policy', 'first_response_due', 'resolution_due'])


def _uidvalidity(M, folder):
    try:
        typ, data = M.status(folder, '(UIDVALIDITY)')
        if typ == 'OK' and data and data[0]:
            m = re.search(rb'UIDVALIDITY (\d+)', data[0])
            if m:
                return m.group(1).decode()
    except Exception:
        pass
    return ''


def fetch_account(acct, verbose=False):
    results = {'fetched': 0, 'created': 0, 'threaded': 0, 'duplicates': 0, 'errors': []}
    try:
        if acct.imap_ssl:
            M = imaplib.IMAP4_SSL(acct.imap_host, int(acct.imap_port))
        else:
            M = imaplib.IMAP4(acct.imap_host, int(acct.imap_port))
        M.login(acct.imap_user or acct.email, acct.imap_pass)

        # Open READ-ONLY so we never touch the \Seen flag.
        candidates = [acct.imap_folder, f'INBOX.{acct.imap_folder}', 'INBOX']
        folder, typ = acct.imap_folder, 'NO'
        for f in candidates:
            typ, _ = M.select(f, readonly=True)
            if typ == 'OK':
                folder = f
                break
        if typ != 'OK':
            M.logout()
            results['errors'].append(f'Cannot open any folder for {acct.email}')
            return results

        # First sync (or mailbox recreated -> new UIDVALIDITY): prime the
        # high-water mark so we only import a recent window instead of the
        # whole mailbox history. This is how connecting a mailbox behaves in
        # Freshdesk; Message-ID dedup keeps any overlap idempotent.
        uidval = _uidvalidity(M, folder)
        if uidval and uidval != (acct.imap_uidvalidity or ''):
            acct.imap_uidvalidity = uidval
            since = (timezone.now() - timedelta(days=INITIAL_SYNC_DAYS)).strftime('%d-%b-%Y')
            typ_s, d = M.uid('search', None, 'SINCE', since)
            recent = sorted(int(x) for x in d[0].split()) if (typ_s == 'OK' and d and d[0]) else []
            # Start just below the oldest message in the window (or keep the
            # existing mark if the window is empty).
            acct.last_uid = (recent[0] - 1) if recent else (acct.last_uid or 0)

        start = (acct.last_uid or 0) + 1
        # Explicit "UID a:*" form — some IMAP servers reject a bare sequence set.
        typ, data = M.uid('search', None, 'UID', f'{start}:*')
        uids = []
        if typ == 'OK' and data and data[0]:
            # 'UID a:*' always returns at least the highest UID, so filter.
            uids = sorted(u for u in (int(x) for x in data[0].split()) if u > (acct.last_uid or 0))
        uids = uids[:FETCH_LIMIT]
        results['fetched'] = len(uids)

        for uid in uids:
            try:
                typ, msg_data = M.uid('fetch', str(uid), '(RFC822)')
                raw = msg_data[0][1] if typ == 'OK' and msg_data and msg_data[0] else None
                if raw:
                    process_message(acct, raw, results)
            except Exception as e:
                results['errors'].append(f'uid {uid}: {e}')
            # Advance past this message regardless, so a poison message can't
            # wedge the queue (dedup by Message-ID keeps it safe).
            acct.last_uid = uid

        acct.save(update_fields=['last_uid', 'imap_uidvalidity'])
        M.logout()
    except Exception as e:
        results['errors'].append(str(e))
    return results


class Command(BaseCommand):
    help = 'Fetch emails from all active IMAP accounts and create CRM tickets'

    def add_arguments(self, parser):
        parser.add_argument('--client', type=int, help='Only fetch for this client ID')

    def handle(self, *args, **options):
        qs = ClientEmailAccount.objects.filter(is_active=True)
        if options.get('client'):
            qs = qs.filter(client_id=options['client'])

        totals = {'fetched': 0, 'created': 0, 'threaded': 0, 'duplicates': 0}
        for acct in qs:
            r = fetch_account(acct, verbose=True)
            for k in totals:
                totals[k] += r[k]
            if r['errors']:
                self.stderr.write(f'[{acct.email}] ERROR: {r["errors"]}')
            else:
                self.stdout.write(
                    f'[{acct.email}] {r["fetched"]} new msg, '
                    f'{r["created"]} ticket(s), {r["threaded"]} reply(ies), '
                    f'{r["duplicates"]} dup'
                )

        self.stdout.write(self.style.SUCCESS(
            f'Done. {totals["fetched"]} scanned, {totals["created"]} new tickets, '
            f'{totals["threaded"]} replies threaded, {totals["duplicates"]} duplicates.'
        ))
