#include "argparse.hpp"

#include <stdexcept>

static const std::string HELP_TEXT = R"(
Converts existing files to C++ source code which can be used to recreate the original files.

Usage: dehydrate [OPTIONS] SOURCE DEST

Options:
  -s         Silent mode (no output)
  -u         Update dehydrate to the latest version
  -v         Show version only

Examples:
  dehydrate file.txt output/    Creates _file.txt.cpp and _file.txt.hpp in output/
  dehydrate src/ output/        Creates _src.cpp and _src.hpp in output/
  dehydrate -u                  Updates dehydrate to the latest version
  dehydrate -v                  Shows version number
  dehydrate version             Shows version number
)";

Args parse_args(int argc, char* argv[]) {
    Args args;
    int idx = 1;
    
    // Check for "version" as first argument (no dash)
    if (argc > 1 && std::string(argv[1]) == "version") {
        args.version = true;
        return args;
    }
    
    // Parse flags
    while (idx < argc && argv[idx][0] == '-') {
        std::string flag = argv[idx];
        
        if (flag == "-s") {
            args.silent = true;
        } else if (flag == "-u") {
            args.update = true;
        } else if (flag == "-v") {
            args.version = true;
        } else {
            throw std::runtime_error("Unknown flag: " + flag + "\n\n" + HELP_TEXT);
        }
        
        idx++;
    }
    
    // If update or version flag is set, return early
    if (args.update || args.version) {
        return args;
    }
    
    // Require source and dest parameters for normal operation
    if (argc - idx != 2) {
        throw std::runtime_error(HELP_TEXT);
    }
    
    args.source = argv[idx];
    args.dest = argv[idx + 1];
    return args;
} 