"""Logs — view and download recent audit log files.

Reached from the sticky-footer Help popover (no sidebar entry — the
nav link is hidden via CSS in ``hide_streamlit_chrome``). URL path
is ``/logs``. Lists every ``datatools-*.jsonl`` file in
``audit_log_dir()`` so users can download today's session or a
prior day's file for support requests.

The 7-day retention sweep in ``src/audit.py`` runs on the writer
thread, so this page is read-only — no delete buttons. Users
wanting a longer retention can copy files out of the folder.
"""

from __future__ import annotations

import sys
from datetime import datetime
from pathlib import Path

import streamlit as st

_project_root = Path(__file__).resolve().parent.parent.parent.parent
if str(_project_root) not in sys.path:
    sys.path.insert(0, str(_project_root))

from src.audit import _RETENTION_DAYS, audit_log_dir, audit_log_path
from src.gui.components import (
    hide_streamlit_chrome,
    html_download_button,
    render_sticky_footer,
)

_ICON_PATH = str(Path(__file__).parent.parent / "assets" / "datatools_icon_256.png")
st.set_page_config(
    page_title="Logs · DataTools",
    page_icon=_ICON_PATH,
    layout="wide",
)

hide_streamlit_chrome(gate_license=False)
render_sticky_footer()


st.markdown("# Logs")
st.caption(
    f"Activity logs are written to disk one file per day and kept "
    f"for {_RETENTION_DAYS} days. Event timestamps inside each file "
    f"are UTC; filenames use your local date."
)

log_dir = audit_log_dir()
today_path = audit_log_path()

st.markdown("### Today")
st.code(str(today_path), language="text")
if today_path.exists():
    size = today_path.stat().st_size
    st.caption(f"{size:,} bytes")
    try:
        data = today_path.read_bytes()
        html_download_button(
            f"Download {today_path.name}",
            data,
            file_name=today_path.name,
            mime="application/x-ndjson",
        )
    except Exception as e:
        st.warning(f"Could not read today's log: {type(e).__name__}: {e}")
else:
    st.caption("No events written yet this session.")

st.markdown("### Older sessions")
if not log_dir.exists():
    st.caption("No logs folder yet.")
else:
    others = sorted(
        (p for p in log_dir.glob("datatools-*.jsonl") if p != today_path),
        key=lambda p: p.stat().st_mtime,
        reverse=True,
    )
    if not others:
        st.caption("No older log files.")
    else:
        for p in others:
            try:
                st_mtime = datetime.fromtimestamp(p.stat().st_mtime)
                size = p.stat().st_size
                cols = st.columns([4, 2, 2])
                cols[0].markdown(f"`{p.name}`")
                cols[1].caption(
                    f"{st_mtime:%Y-%m-%d %H:%M} · {size:,} bytes"
                )
                with cols[2]:
                    html_download_button(
                        "Download",
                        p.read_bytes(),
                        file_name=p.name,
                        mime="application/x-ndjson",
                    )
            except Exception as e:
                st.caption(
                    f"`{p.name}` — could not read: "
                    f"{type(e).__name__}: {e}"
                )

st.markdown("### Folder")
st.code(str(log_dir), language="text")
st.caption(
    "Copy this path into Explorer / Finder / your file manager to "
    "open the folder directly."
)
