import csv
import hashlib
import json
import os
import re
import stat
from urllib.request import Request, urlopen
import pythoncom
import win32com.client


KEYWORDS_URL = (
    "https://9564488983.belliniseven.ai/"
    "consultants/caseOrganizer/mSquareConsultingLLC/TXT/keywords.txt"
)


def _parse_keyword_lines(text: str) -> list[str]:
    """Parse non-empty, non-comment keyword lines from text content."""
    keywords: list[str] = []
    for line in text.splitlines():
        cleaned = line.strip()
        if not cleaned or cleaned.startswith("#"):
            continue
        keywords.append(cleaned)
    return keywords


def load_keywords(source: str, fallback_local_path: str | None = None, timeout: int = 15) -> list[str]:
    """Load keywords from an HTTP(S) URL or local file path, with optional local fallback."""
    if source.startswith(("http://", "https://")):
        try:
            req = Request(
                source,
                headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) pstSearch/1.0"},
            )
            with urlopen(req, timeout=timeout) as resp:
                raw_bytes = resp.read()
            decoded = raw_bytes.decode("utf-8-sig", errors="replace")
            return _parse_keyword_lines(decoded)
        except Exception as exc:
            if fallback_local_path and os.path.isfile(fallback_local_path):
                print(f"Warning: Could not fetch '{source}' ({exc}). Falling back to '{fallback_local_path}'.")
                with open(fallback_local_path, "r", encoding="utf-8-sig") as f:
                    return _parse_keyword_lines(f.read())
            raise RuntimeError(f"Failed to load keywords from URL '{source}': {exc}") from exc

    if not os.path.isfile(source):
        raise FileNotFoundError(f"Keywords file not found: {source}")

    with open(source, "r", encoding="utf-8-sig") as f:
        return _parse_keyword_lines(f.read())


def _sanitize_filename(name: str, max_len: int = 80) -> str:
    """Create a safe Windows filename component."""
    sanitized = re.sub(r'[\\/:*?"<>|\r\n]+', "_", name).strip(" .")
    return sanitized[:max_len] or "untitled"


def _find_matched_keywords(haystacks: list[str], keywords: list[str]) -> list[str]:
    """Return case-insensitive keyword matches across provided message fields."""
    combined = "\n".join(h for h in haystacks if h).lower()
    if not combined:
        return []
    return [kw for kw in keywords if kw.lower() in combined]


def _extract_item_text_fields(item) -> tuple[str, str, str, str, str]:
    """Safely extract subject, sender, body, received time, and entry ID."""
    subject = str(getattr(item, "Subject", "") or "")
    sender = str(
        getattr(item, "SenderEmailAddress", "")
        or getattr(item, "SenderName", "")
        or ""
    )
    body = str(getattr(item, "Body", "") or "")
    received = str(getattr(item, "ReceivedTime", "") or "")
    entry_id = str(getattr(item, "EntryID", "") or "")
    return subject, sender, body, received, entry_id


def _validate_pst_file(pst_path: str) -> None:
    """Validate file existence, non-zero size, and clear read-only attribute if set."""
    if not os.path.isfile(pst_path):
        raise FileNotFoundError(f"PST file not found: {pst_path}")

    file_size = os.path.getsize(pst_path)
    if file_size == 0:
        raise ValueError(f"PST file is 0 bytes (empty): {pst_path}")

    file_mode = os.stat(pst_path).st_mode
    if not (file_mode & stat.S_IWRITE):
        os.chmod(pst_path, file_mode | stat.S_IWRITE)


def _find_store_by_path(namespace, pst_path: str):
    """Return (store_id, root_folder) if pst_path is already mounted in namespace.Stores."""
    norm_target = os.path.normcase(os.path.abspath(pst_path))
    try:
        store_count = int(namespace.Stores.Count)
    except Exception:
        return None, None

    for idx in range(1, store_count + 1):
        try:
            store = namespace.Stores.Item(idx)
            raw_path = str(getattr(store, "FilePath", "") or "")
            if raw_path:
                norm_path = os.path.normcase(os.path.abspath(raw_path))
                if norm_path == norm_target:
                    store_id = str(getattr(store, "StoreID", "") or "")
                    root_folder = store.GetRootFolder()
                    if root_folder is not None:
                        return store_id, root_folder
        except Exception:
            continue

    return None, None


def _mount_pst_store(namespace, pst_path: str):
    """Mount pst_path if not already mounted. Returns (store_id, root_folder, mounted_by_script)."""
    existing_id, existing_root = _find_store_by_path(namespace, pst_path)
    if existing_root is not None:
        return existing_id, existing_root, False

    try:
        namespace.AddStore(pst_path)
    except Exception as exc:
        raise RuntimeError(
            f"Outlook refused AddStore for '{pst_path}' ({os.path.getsize(pst_path)} bytes). "
            "Check that Outlook/SCANPST can open the file directly, no other process locks it, "
            "and the file is a valid local PST container."
        ) from exc

    mounted_id, mounted_root = _find_store_by_path(namespace, pst_path)
    if mounted_root is None:
        raise RuntimeError(f"PST appeared to mount but root folder could not be resolved: {pst_path}")

    return mounted_id, mounted_root, True


def _detach_store_safely(namespace, store_id: str | None, mounted_by_script: bool) -> None:
    """Detach the PST store only if this script mounted it."""
    if namespace is None or not store_id or not mounted_by_script:
        return
    try:
        target_store = namespace.GetStoreFromID(store_id)
        if target_store is not None:
            root_folder = target_store.GetRootFolder()
            if root_folder is not None:
                namespace.RemoveStore(root_folder)
    except Exception:
        pass


def _search_folder_recursive(
    folder,
    keywords: list[str],
    export_dir: str,
    folder_path: str,
    matches: list[dict],
    seen_entry_ids: set[str],
) -> None:
    """Recursively scan MAPI folders and export matching messages as .msg files."""
    current_path = f"{folder_path}/{folder.Name}" if folder_path else (folder.Name or "(Root)")

    try:
        items = folder.Items
        item_count = int(items.Count)
    except Exception:
        return

    for idx in range(1, item_count + 1):
        try:
            item = items.Item(idx)
        except Exception:
            continue

        subject, sender, body, received, entry_id = _extract_item_text_fields(item)
        matched_kws = _find_matched_keywords([subject, sender, body], keywords)
        if not matched_kws:
            continue

        dedup_key = entry_id or f"{current_path}|{received}|{subject}"
        if dedup_key in seen_entry_ids:
            continue
        seen_entry_ids.add(dedup_key)

        seq = len(matches) + 1
        kw_tag = _sanitize_filename("_".join(matched_kws), max_len=40)
        subj_tag = _sanitize_filename(subject, max_len=60)
        short_hash = hashlib.sha1(dedup_key.encode("utf-8", errors="replace")).hexdigest()[:8]

        msg_filename = f"{seq:04d}_{kw_tag}_{subj_tag}_{short_hash}.msg"
        msg_path = os.path.join(export_dir, msg_filename)

        exported_msg = False
        try:
            item.SaveAs(msg_path, 3)  # olMSG = 3
            exported_msg = True
        except Exception:
            txt_path = os.path.splitext(msg_path)[0] + ".txt"
            with open(txt_path, "w", encoding="utf-8") as tf:
                tf.write(f"Folder: {current_path}\n")
                tf.write(f"Subject: {subject}\n")
                tf.write(f"Sender: {sender}\n")
                tf.write(f"Received: {received}\n")
                tf.write(f"Matched Keywords: {', '.join(matched_kws)}\n")
                tf.write("-" * 60 + "\n")
                tf.write(body)
            msg_path = txt_path

        matches.append(
            {
                "index": seq,
                "folder": current_path,
                "subject": subject,
                "sender": sender,
                "received": received,
                "matched_keywords": matched_kws,
                "exported_file": msg_path,
                "saved_as_msg": exported_msg,
            }
        )

    try:
        subfolders = folder.Folders
        subfolder_count = int(subfolders.Count)
    except Exception:
        return

    for s_idx in range(1, subfolder_count + 1):
        try:
            sub_folder = subfolders.Item(s_idx)
        except Exception:
            continue
        _search_folder_recursive(
            sub_folder,
            keywords,
            export_dir,
            current_path,
            matches,
            seen_entry_ids,
        )


def search_pst_and_export(
    pst_path: str,
    keywords_source: str,
    export_dir: str,
    fallback_keywords_path: str | None = None,
) -> list[dict]:
    """Attach PST, search all folders for keywords, export hits, and detach PST."""
    _validate_pst_file(pst_path)

    keywords = load_keywords(keywords_source, fallback_local_path=fallback_keywords_path)
    if not keywords:
        raise ValueError(f"No valid keywords found in: {keywords_source}")

    os.makedirs(export_dir, exist_ok=True)

    pythoncom.CoInitialize()
    outlook = None
    namespace = None
    store_id: str | None = None
    mounted_by_script = False
    matches: list[dict] = []
    seen_entry_ids: set[str] = set()

    try:
        outlook = win32com.client.Dispatch("Outlook.Application")
        namespace = outlook.GetNamespace("MAPI")

        store_id, pst_root, mounted_by_script = _mount_pst_store(namespace, pst_path)

        _search_folder_recursive(
            pst_root,
            keywords,
            export_dir,
            "",
            matches,
            seen_entry_ids,
        )
    finally:
        _detach_store_safely(namespace, store_id, mounted_by_script)
        pst_root = None
        namespace = None
        outlook = None
        pythoncom.CoUninitialize()

    return matches


def main() -> None:
    base_dir = r"C:\Temp\projectElectronicDiscovery\PST"
    pst_path = os.path.join(base_dir, "michael@belliniseven.cloud.pst")
    fallback_keywords_path = os.path.join(base_dir, "keywords.txt")
    export_dir = os.path.join(base_dir, "searchHits")

    results = search_pst_and_export(
        pst_path=pst_path,
        keywords_source=KEYWORDS_URL,
        export_dir=export_dir,
        fallback_keywords_path=fallback_keywords_path,
    )

    manifest_json = os.path.join(export_dir, "search_hits_manifest.json")
    manifest_csv = os.path.join(export_dir, "search_hits_manifest.csv")

    with open(manifest_json, "w", encoding="utf-8") as jf:
        json.dump(results, jf, indent=2)

    with open(manifest_csv, "w", newline="", encoding="utf-8") as cf:
        writer = csv.DictWriter(
            cf,
            fieldnames=[
                "index",
                "folder",
                "subject",
                "sender",
                "received",
                "matched_keywords",
                "exported_file",
                "saved_as_msg",
            ],
        )
        writer.writeheader()
        for row in results:
            row_copy = dict(row)
            row_copy["matched_keywords"] = "; ".join(row["matched_keywords"])
            writer.writerow(row_copy)

    print(f"Total matching messages exported: {len(results)}")
    print(f"Export folder: {export_dir}")
    print(f"Manifests saved to:\n  - {manifest_json}\n  - {manifest_csv}")


if __name__ == "__main__":
    main()