"""Superpitch — inbound pitch screening agent."""

from __future__ import annotations

import os
import sys

from superpitch import __version__
from superpitch.app_paths import bootstrap_user_files
from superpitch.state import load_config, save_config
from superpitch.terminal_ui import run_interactive_app
from superpitch.updates import get_update_notice

def main():
    if len(sys.argv) > 1:
        _run_file_mode()
        return

    bootstrap_user_files()
    update_notice = get_update_notice()
    config = load_config()

    if update_notice:
        print(f"Update available: {update_notice} (current {__version__}). Type /upgrade inside the app.")

    run_interactive_app(config)


def _run_file_mode():
    config = load_config()
    api_key = config.get("anthropic_api_key", "")
    if not api_key:
        print("No Anthropic API key configured. Run `superpitch` to set up.")
        raise SystemExit(1)

    from superpitch.process import process_file
    from superpitch.state import save_evidence_entry
    from superpitch.terminal_ui import print_packet_summary

    path = sys.argv[1]
    if path == "-":
        import tempfile
        text = sys.stdin.read()
        with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
            f.write(text)
            tmp = f.name
        packet = process_file(tmp, config.get("thesis", ""), api_key)
        os.unlink(tmp)
    else:
        if not os.path.isfile(path):
            print(f"File not found: {path}")
            raise SystemExit(1)
        packet = process_file(path, config.get("thesis", ""), api_key)

    if packet:
        print_packet_summary(packet)
        save_evidence_entry(packet, source_label=path)
    else:
        print("Could not extract evidence from that file.")


if __name__ == "__main__":
    main()
