import base64
import imaplib
import json
import os
import re
import smtplib
import sqlite3
import ssl
import urllib.error
import urllib.request
from contextlib import contextmanager
from email import policy
from email.message import EmailMessage
from email.parser import BytesParser
from email.utils import formatdate, make_msgid, parseaddr
from html.parser import HTMLParser
from pathlib import Path
from urllib.parse import parse_qs

from cryptography.fernet import Fernet

ROOT = Path(__file__).resolve().parent
PROJECT = "crmmail-bc67f"
API_KEY = "AIzaSyBKZswNTLjATEBv-UNClyq9R2kpxy6og8A"
TLS = ssl.create_default_context()
PAGE_SIZE = 30
MAIL_LIMIT = 8 * 1024 * 1024
ADDRESS = re.compile(r"[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,63}")
HOST = re.compile(r"[a-zA-Z0-9.-]+")
GMAIL = {
    "label": "Gmail / Google Workspace",
    "imap": "imap.gmail.com",
    "smtp": "smtp.gmail.com",
    "sent": "[Gmail]/Sent Mail",
}


class Problem(Exception):
    def __init__(self, status, message):
        self.status = status
        self.message = message


def providers():
    extra = json.loads(os.environ.get("MAIL_CRM_PROVIDERS", "{}"))
    return {**extra, "gmail": GMAIL}


@contextmanager
def database():
    directory = Path(os.environ["MAIL_CRM_DATA_DIR"]).resolve()
    if directory == ROOT or ROOT in directory.parents:
        raise RuntimeError("Place MAIL_CRM_DATA_DIR outside the ZIP extraction directory")
    directory.mkdir(parents=True, exist_ok=True, mode=0o700)
    connection = sqlite3.connect(directory / "mail.sqlite3", timeout=10)
    try:
        with connection:
            connection.execute("""CREATE TABLE IF NOT EXISTS accounts (
                id INTEGER PRIMARY KEY, owner TEXT NOT NULL, address TEXT NOT NULL,
                label TEXT NOT NULL, provider TEXT NOT NULL, secret BLOB NOT NULL,
                UNIQUE(owner, address COLLATE NOCASE))""")
            connection.row_factory = sqlite3.Row
            yield connection
    finally:
        connection.close()


def authenticate(env):
    header = env.get("HTTP_AUTHORIZATION", "")
    token = header.removeprefix("Bearer ")
    if token == header or len(token) > 8000:
        raise Problem("401 Unauthorized", "Sign in to continue.")
    try:
        parts = token.split(".")
        claims = json.loads(base64.urlsafe_b64decode(parts[1] + "=" * (-len(parts[1]) % 4)))
        if claims.get("aud") != PROJECT or claims.get("iss") != f"https://securetoken.google.com/{PROJECT}":
            raise ValueError()
    except (ValueError, IndexError, AttributeError):
        raise Problem("401 Unauthorized", "Invalid session.") from None

    allowed = {uid.strip() for uid in os.environ.get("MAIL_CRM_ADMIN_UIDS", "").split(",") if uid.strip()}
    if not allowed:
        raise Problem("503 Service Unavailable", "The administrator UID is missing from the server configuration.")
    request = urllib.request.Request(
        f"https://identitytoolkit.googleapis.com/v1/accounts:lookup?key={API_KEY}",
        data=json.dumps({"idToken": token}).encode(),
        headers={"Content-Type": "application/json"},
    )
    try:
        with urllib.request.urlopen(request, timeout=8) as response:
            users = json.load(response).get("users", [])
    except urllib.error.HTTPError as exc:
        if exc.code in (400, 401):
            raise Problem("401 Unauthorized", "Your Firebase session expired. Sign in again.") from None
        raise Problem("503 Service Unavailable", "Firebase could not verify this session.") from None
    except (OSError, ValueError):
        raise Problem("503 Service Unavailable", "Firebase authentication is temporarily unavailable.") from None

    if not users or users[0].get("disabled") or users[0].get("localId") != claims.get("sub"):
        raise Problem("401 Unauthorized", "Invalid Firebase session.")
    uid = users[0]["localId"]
    if uid not in allowed:
        raise Problem("403 Forbidden", "This Firebase user has no access to the mail dashboard.")
    return uid


def public_account(row):
    return {key: row[key] for key in ("id", "address", "label", "provider")}


def text_field(data, key, limit, required=True, multiline=False):
    value = data.get(key, "")
    if not isinstance(value, str) or len(value) > limit or "\x00" in value:
        raise Problem("400 Bad Request", f"Invalid {key}.")
    if (required and not value.strip()) or (not multiline and ("\r" in value or "\n" in value)):
        raise Problem("400 Bad Request", f"Invalid {key}.")
    return value


def provider(name):
    entry = providers().get(name)
    if not entry:
        raise Problem("400 Bad Request", "This mail provider is unavailable.")
    if not all(HOST.fullmatch(entry[key]) for key in ("imap", "smtp")):
        raise RuntimeError("Invalid server-side provider configuration")
    return entry


@contextmanager
def inbox(account, password, folder="inbox"):
    entry = provider(account["provider"])
    with imaplib.IMAP4_SSL(entry["imap"], 993, ssl_context=TLS, timeout=15) as client:
        client.login(account["address"], password)
        mailbox = "INBOX" if folder == "inbox" else entry.get("sent", "Sent")
        if not mailbox or any(c in mailbox for c in '\r\n"\\'):
            raise RuntimeError("Invalid mailbox configuration")
        status, rows = client.select(f'"{mailbox}"', readonly=True)
        if status != "OK":
            raise Problem("502 Bad Gateway", "The provider could not open that mailbox.")
        validity = client.response("UIDVALIDITY")[1][0]
        if not validity or not validity.isdigit():
            raise Problem("502 Bad Gateway", "The provider did not identify this mailbox.")
        yield client, int(rows[0]), validity.decode()


def metadata(message):
    return {
        "subject": str(message.get("Subject", "")) or "(No subject)",
        "from": str(message.get("From", "")),
        "to": str(message.get("To", "")),
        "date": str(message.get("Date", "")),
    }


def list_messages(client, count, page, validity):
    end = count - page * PAGE_SIZE
    if end <= 0:
        return {"messages": [], "more": False}
    start = max(1, end - PAGE_SIZE + 1)
    status, rows = client.fetch(f"{start}:{end}", "(UID BODY.PEEK[HEADER.FIELDS (FROM TO SUBJECT DATE)])")
    if status != "OK":
        raise Problem("502 Bad Gateway", "Could not load the message list.")
    messages = []
    for index, row in enumerate(rows):
        if isinstance(row, tuple):
            trailing = rows[index + 1] if index + 1 < len(rows) else None
            attributes = row[0] + (trailing if isinstance(trailing, bytes) else b"")
            uid = re.search(rb"\bUID (\d+)\b", attributes)
            if uid:
                message = BytesParser(policy=policy.default).parsebytes(row[1])
                messages.append({**metadata(message), "uid": uid[1].decode(), "validity": validity})
    return {"messages": messages[::-1], "more": start > 1}


class PlainText(HTMLParser):
    hidden = {"script", "style", "head", "title"}
    blocks = {"p", "div", "br", "li", "tr", "blockquote", "h1", "h2", "h3"}

    def __init__(self):
        super().__init__()
        self.fragments = []
        self.hidden_tags = []

    def handle_starttag(self, tag, attrs):
        if tag in self.hidden:
            self.hidden_tags.append(tag)
        elif tag in self.blocks and not self.hidden_tags:
            self.fragments.append("\n")

    def handle_endtag(self, tag):
        if self.hidden_tags and self.hidden_tags[-1] == tag:
            self.hidden_tags.pop()
        elif tag in self.blocks and not self.hidden_tags:
            self.fragments.append("\n")

    def handle_data(self, data):
        if not self.hidden_tags:
            self.fragments.append(re.sub(r"\s+", " ", data))


def read_message(client, uid):
    status, rows = client.uid("FETCH", uid, "(RFC822.SIZE)")
    sizes = [re.search(rb"RFC822.SIZE (\d+)", row) for row in rows if isinstance(row, bytes)]
    size = next((int(match[1]) for match in sizes if match), None)
    if status != "OK" or size is None:
        raise Problem("404 Not Found", "This message is no longer available.")
    if size > MAIL_LIMIT:
        raise Problem("413 Content Too Large", "Open this message in your provider’s webmail. It exceeds 8 MB.")
    status, rows = client.uid("FETCH", uid, "(BODY.PEEK[])")
    raw = next((row[1] for row in rows if isinstance(row, tuple)), None)
    if status != "OK" or raw is None:
        raise Problem("404 Not Found", "This message is no longer available.")
    message = BytesParser(policy=policy.default).parsebytes(raw)
    part = message.get_body(preferencelist=("plain", "html"))
    body = ""
    if part:
        payload = part.get_payload(decode=True) or b""
        try:
            body = payload.decode(part.get_content_charset() or "utf-8", errors="replace")
        except LookupError:
            body = payload.decode("utf-8", errors="replace")
        if part.get_content_type() == "text/html":
            converter = PlainText()
            converter.feed(body)
            converter.close()
            body = "".join(converter.fragments).strip()
    return {
        **metadata(message),
        "body": body[:500000],
        "replyTo": parseaddr(str(message.get("Reply-To") or message.get("From") or ""))[1],
        "messageId": str(message.get("Message-ID", "")),
        "attachments": [part.get_filename() or "Attachment" for part in message.iter_attachments()],
    }


def send_message(account, password, data):
    recipient = text_field(data, "to", 254).strip()
    if not ADDRESS.fullmatch(recipient):
        raise Problem("400 Bad Request", "Enter one valid recipient email address.")
    message = EmailMessage()
    message["From"] = account["address"]
    message["To"] = recipient
    message["Subject"] = text_field(data, "subject", 300, required=False)
    message["Date"] = formatdate(localtime=False)
    message["Message-ID"] = make_msgid(domain=account["address"].split("@")[1])
    reference = text_field(data, "inReplyTo", 500, required=False)
    if re.fullmatch(r"<[^<>\s]+@[^<>\s]+>", reference):
        message["In-Reply-To"] = reference
        message["References"] = reference
    message.set_content(text_field(data, "body", 100000, multiline=True))
    settings = provider(account["provider"])
    port = int(settings.get("smtp_port", 465))
    if port not in (465, 587):
        raise RuntimeError("Use SMTP port 465 or 587")
    if port == 465:
        connection = smtplib.SMTP_SSL(settings["smtp"], port, timeout=15, context=TLS)
    else:
        connection = smtplib.SMTP(settings["smtp"], port, timeout=15)
    try:
        if port == 587:
            connection.starttls(context=TLS)
        connection.login(account["address"], password)
        connection.send_message(message)
    finally:
        connection.close()


def request_data(env):
    if env.get("CONTENT_TYPE", "").split(";")[0] != "application/json":
        raise Problem("415 Unsupported Media Type", "Use a JSON request.")
    try:
        length = int(env.get("CONTENT_LENGTH") or "0")
        if not 0 < length <= 700000:
            raise ValueError()
        data = json.loads(env["wsgi.input"].read(length))
        if not isinstance(data, dict):
            raise ValueError()
    except ValueError:
        raise Problem("400 Bad Request", "Invalid request data.") from None
    return data


def api(env, path):
    method = env["REQUEST_METHOD"]
    owner = authenticate(env)
    if path == "/api/providers" and method == "GET":
        return {key: value["label"] for key, value in providers().items()}
    if path == "/api/accounts" and method == "GET":
        with database() as db:
            rows = db.execute("SELECT id, address, label, provider FROM accounts WHERE owner = ? ORDER BY label COLLATE NOCASE, id", (owner,))
            return [public_account(row) for row in rows]
    if path == "/api/accounts" and method == "POST":
        data = request_data(env)
        address = text_field(data, "address", 254).strip()
        label = text_field(data, "label", 80, required=False).strip() or address
        name = text_field(data, "provider", 50)
        provider(name)
        password = text_field(data, "password", 500)
        if name == "gmail":
            password = "".join(password.split())
        if not ADDRESS.fullmatch(address) or (name == "gmail" and not re.fullmatch(r"[A-Za-z]{16}", password)):
            raise Problem("400 Bad Request", "Check the mailbox address and credential. Gmail requires a 16-letter app password.")
        cipher = Fernet(os.environ["MAIL_CRM_KEY"])
        account = {"address": address, "provider": name}
        with inbox(account, password):
            pass
        with database() as db:
            db.execute("""INSERT INTO accounts (owner, address, label, provider, secret)
                VALUES (?, ?, ?, ?, ?) ON CONFLICT(owner, address) DO UPDATE
                SET label=excluded.label, provider=excluded.provider, secret=excluded.secret""",
                (owner, address, label, name, cipher.encrypt(password.encode())))
        return {"ok": True}

    match = re.fullmatch(r"/api/accounts/(\d{1,15})(?:/(messages(?:/\d{1,15})?|send))?", path)
    if match:
        account_id, action = match.groups()
        with database() as db:
            account = db.execute("SELECT * FROM accounts WHERE owner = ? AND id = ?", (owner, account_id)).fetchone()
            if not account:
                raise Problem("404 Not Found", "Mailbox not found.")
            if not action and method == "DELETE":
                db.execute("DELETE FROM accounts WHERE owner = ? AND id = ?", (owner, account_id))
                return {"ok": True}
        password = Fernet(os.environ["MAIL_CRM_KEY"]).decrypt(account["secret"]).decode()
        if action == "send" and method == "POST":
            send_message(account, password, request_data(env))
            return {"ok": True}
        if action and action.startswith("messages") and method == "GET":
            query = parse_qs(env.get("QUERY_STRING", ""))
            folder = query.get("folder", ["inbox"])[0]
            page = query.get("page", ["0"])[0]
            if folder not in {"inbox", "sent"} or not re.fullmatch(r"[0-9]{1,8}", page):
                raise Problem("400 Bad Request", "Invalid mailbox folder or page.")
            with inbox(account, password, folder) as (client, count, validity):
                if action == "messages":
                    return list_messages(client, count, int(page), validity)
                if query.get("validity", [""])[0] != validity:
                    raise Problem("409 Conflict", "The mailbox changed. Refresh the message list.")
                return read_message(client, action.split("/")[1])
    raise Problem("404 Not Found", "Page not found.")


STATIC = {
    "/": ("index.html", "text/html"),
    "/app.js": ("app.js", "text/javascript"),
    "/style.css": ("style.css", "text/css"),
}
CSP = (
    "default-src 'self'; script-src 'self' https://www.gstatic.com; "
    "style-src 'self'; connect-src 'self' https://identitytoolkit.googleapis.com https://securetoken.googleapis.com; "
    "frame-src https://crmmail-bc67f.firebaseapp.com; frame-ancestors 'none'; base-uri 'none'; form-action 'self'"
)


def application(env, start_response):
    path = env.get("PATH_INFO", "")
    status = "200 OK"
    mime = "application/json"
    try:
        if path in STATIC and env["REQUEST_METHOD"] == "GET":
            filename, mime = STATIC[path]
            body = (ROOT / "static" / filename).read_bytes()
        elif path.startswith("/api/"):
            body = json.dumps(api(env, path)).encode()
        else:
            raise Problem("404 Not Found", "Page not found.")
    except (imaplib.IMAP4.error, smtplib.SMTPException, TimeoutError, ConnectionError, ssl.SSLError) as exc:
        error = "The mail provider rejected the request or could not be reached."
        if path.endswith("/send"):
            error = "Mail delivery could not be confirmed. Check the recipient or Sent before retrying."
        status, mime, body = "502 Bad Gateway", "application/json", json.dumps({"error": error}).encode()
    except Problem as exc:
        status, mime, body = exc.status, "application/json", json.dumps({"error": exc.message}).encode()
    except Exception as exc:
        import socket
        import sys

        if isinstance(exc, socket.gaierror):
            status, error = "502 Bad Gateway", "The mail server hostname could not be resolved."
        else:
            status, error = "500 Internal Server Error", "The server could not complete this request. Check its configuration."
            print(f"Mail CRM internal error ({type(exc).__name__})", file=sys.stderr)
        mime, body = "application/json", json.dumps({"error": error}).encode()
    start_response(status, [
        ("Content-Type", f"{mime}; charset=utf-8"),
        ("Content-Length", str(len(body))),
        ("Cache-Control", "no-store"),
        ("Content-Security-Policy", CSP),
        ("X-Content-Type-Options", "nosniff"),
        ("Referrer-Policy", "no-referrer"),
    ])
    return [body]
