"""Command line argument parsing."""

import argparse
import sys

from porterminal import __version__


def parse_args() -> argparse.Namespace:
    """Parse command line arguments.

    Returns:
        Parsed arguments namespace with:
        - path: Starting directory for the shell (optional)
        - no_tunnel: Whether to skip Cloudflare tunnel
        - verbose: Whether to show detailed logs
        - update: Whether to update to latest version
        - check_update: Whether to check for updates
    """
    parser = argparse.ArgumentParser(
        description="Porterminal - Web terminal via Cloudflare Tunnel",
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        "-V",
        "--version",
        action="version",
        version=f"%(prog)s {__version__}",
    )
    parser.add_argument(
        "path",
        nargs="?",
        default=None,
        help="Starting directory for the shell (default: current directory)",
    )
    parser.add_argument(
        "--no-tunnel",
        action="store_true",
        help="Start server only, without Cloudflare tunnel",
    )
    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="Show detailed startup logs",
    )
    parser.add_argument(
        "-U",
        "--update",
        action="store_true",
        help="Update to the latest version",
    )
    parser.add_argument(
        "--check-update",
        action="store_true",
        help="Check if a newer version is available",
    )
    parser.add_argument(
        "-b",
        "--background",
        action="store_true",
        help="Run in background and return immediately",
    )
    parser.add_argument(
        "--init",
        action="store_true",
        help="Create .ptn/ptn.yaml config file in current directory",
    )
    parser.add_argument(
        "-p",
        "--password",
        action="store_true",
        help="Prompt for password to protect terminal access",
    )
    parser.add_argument(
        "-dp",
        "--default-password",
        action="store_true",
        help="Toggle password requirement in config (on/off)",
    )
    # Internal argument for background mode communication
    parser.add_argument(
        "--_url-file",
        dest="url_file",
        help=argparse.SUPPRESS,
    )

    args = parser.parse_args()

    # Handle update commands early (before main app starts)
    if args.check_update:
        from porterminal.updater import check_for_updates, get_upgrade_command

        has_update, latest = check_for_updates(use_cache=False)
        if has_update:
            print(f"Update available: {__version__} → {latest}")
            print(f"Run: {get_upgrade_command()}")
        else:
            print(f"Already at latest version ({__version__})")
        sys.exit(0)

    if args.update:
        from porterminal.updater import update_package

        success = update_package()
        sys.exit(0 if success else 1)

    if args.init:
        _init_config()
        sys.exit(0)

    if args.default_password:
        _toggle_password_requirement()
        sys.exit(0)

    return args


DEFAULT_CONFIG = """\
# ptn configuration file
# Docs: https://github.com/lyehe/porterminal/blob/master/docs/configuration.md

# Custom buttons (appear in third toolbar row)
buttons:
  - label: "git"
    send: "git status\\r"
  - label: "build"
    send: "npm run build\\r"
  # Multi-step button with delays (ms):
  # - label: "deploy"
  #   send:
  #     - "npm run build"
  #     - 100
  #     - "\\r"

# Terminal settings (optional)
# terminal:
#   default_shell: bash
#   cols: 120
#   rows: 30
"""


def _init_config() -> None:
    """Create .ptn/ptn.yaml in current directory."""
    from pathlib import Path

    config_dir = Path.cwd() / ".ptn"
    config_file = config_dir / "ptn.yaml"

    if config_file.exists():
        print(f"Config already exists: {config_file}")
        return

    config_dir.mkdir(exist_ok=True)
    config_file.write_text(DEFAULT_CONFIG)
    print(f"Created: {config_file}")


def _toggle_password_requirement() -> None:
    """Toggle security.require_password in config file."""
    from pathlib import Path

    import yaml

    from porterminal.config import find_config_file

    # Find existing config or use default location
    config_path = find_config_file()
    if config_path is None:
        config_dir = Path.cwd() / ".ptn"
        config_path = config_dir / "ptn.yaml"
        config_dir.mkdir(exist_ok=True)

    # Read existing config or create empty
    if config_path.exists():
        with open(config_path, encoding="utf-8") as f:
            data = yaml.safe_load(f) or {}
    else:
        data = {}

    # Toggle the value
    if "security" not in data:
        data["security"] = {}
    current = data["security"].get("require_password", False)
    data["security"]["require_password"] = not current

    # Write back
    with open(config_path, "w", encoding="utf-8") as f:
        yaml.safe_dump(data, f, default_flow_style=False, sort_keys=False)

    new_value = data["security"]["require_password"]
    status = "enabled" if new_value else "disabled"
    print(f"Password requirement {status} in {config_path}")
