"""
Command-line interface argument parsing for wittrans.

SPDX-FileCopyrightText: 2024 Jiri Grönroos <jiri.gronroos@iki.fi>

SPDX-License-Identifier: LGPL-3.0-or-later
"""

import argparse

from . import __version__


def parse_arguments() -> argparse.Namespace:
    """
    Parse command line arguments.
    Returns:
        Parsed arguments namespace
    """
    parser = argparse.ArgumentParser(
        description="Search for translated strings in Linux distribution locale files.",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Looking for "Monday" in different languages:

  %(prog)s "maanantai" fi            # Finnish (2-letter ISO-639 code)
  %(prog)s "maanantai" fin           # Finnish (3-letter ISO-639 code)
  %(prog)s "maanantai" fi_FI         # Finnish (C locale code)
  %(prog)s "segunda-feira" pt_BR     # Brazilian Portuguese
  %(prog)s "lunes" es                # Spanish
  %(prog)s "星期一" zh_CN            # Simplified Chinese

Additional options:

  %(prog)s "lunes" es -i             # Highlight search results
  %(prog)s "lunes" es --no-flatpak   # Skip Flatpak applications (faster)
  %(prog)s "lunes" es -s             # Search only in source strings
  %(prog)s "lunes" es -t             # Search only in translations
  %(prog)s "Monday" en -w            # Search for whole word only
  %(prog)s "lunes" es -o tsv         # Export to TSV file
  %(prog)s "星期一" zh_CN -o tsv --output-file chinese_monday.tsv
                                        # Custom output filename
  %(prog)s --all fi -o tsv           # Export all Finnish translations to TSV file

Run 'locale -a' in terminal to see all available locale codes on your system.
        """,
    )
    parser.add_argument(
        "search_term",
        nargs="?",
        default="",
        help="Text to search for in quotes (case-insensitive). Not required when using --all.",
    )
    parser.add_argument(
        "language_code",
        help="Language code: ISO-639-1 (2-letter), ISO-639-2/3 (3-letter), or C locale code (e.g., fi, fin, fi_FI, zh_CN)",
    )
    parser.add_argument(
        "-i",
        "--indicate",
        action="store_true",
        help="Indicate/highlight search term matches in output",
    )
    parser.add_argument(
        "-s",
        "--source",
        action="store_true",
        help="Search only in source strings (cannot be used with -t)",
    )
    parser.add_argument(
        "-t",
        "--translation",
        action="store_true",
        help="Search only in translation strings (cannot be used with -s)",
    )
    parser.add_argument(
        "-w",
        "--whole-word",
        action="store_true",
        help="Match whole words only instead of partial matches",
    )
    parser.add_argument(
        "-f",
        "--no-flatpak",
        action="store_true",
        help="Skip searching Flatpak applications (faster search)",
    )
    parser.add_argument(
        "-o",
        "--output",
        metavar="FORMAT",
        choices=["tsv"],
        help="Output format: tsv (tab-separated values file). When specified, results are written to [search_term]_[language_code].tsv",
    )
    parser.add_argument(
        "--output-file",
        metavar="FILE",
        help="Custom output filename (used with -o option). If not specified, auto-generated filename is used.",
    )
    parser.add_argument(
        "-a",
        "--all",
        action="store_true",
        help="Retrieve all translations (requires -o tsv for output)",
    )
    parser.add_argument(
        "--version",
        action="version",
        version=f"%(prog)s {__version__}",
    )

    args = parser.parse_args()

    if args.all:
        # --all requires TSV output
        if args.output != "tsv":
            parser.error(
                "The --all flag requires TSV output (-o tsv). "
                "Example: wittrans --all fi -o tsv"
            )
        # When using --all, no search term is needed and shouldn't be accepted
        args.search_term = ""
    else:
        # Normal search requires a non-empty search term
        if len(args.search_term) == 0:
            parser.error(
                "Search term cannot be empty (use --all to retrieve all translations)"
            )

    if args.source and args.translation:
        parser.error("Options -s/--source and -t/--translation cannot be used together")

    return args
