#include <filesystem>
#include <iostream>
#include <string>

#include "core/resources/resource_pack.hpp"

namespace {

void showHelp() {
    std::cout << "AEE - Resource Packer\n";
    std::cout << "=====================\n";
    std::cout << "Usage: pack_resources [options] [input_dir] [output_file]\n\n";
    std::cout << "Options:\n";
    std::cout << "  --help     Show this help message\n";
    std::cout << "  --list     List contents of an existing pack file\n\n";
    std::cout << "Arguments:\n";
    std::cout << "  input_dir   Directory to pack (default: data)\n";
    std::cout << "  output_file Pack file name (default: resources.pack)\n\n";
    std::cout << "Examples:\n";
    std::cout << "  pack_resources                    # Pack 'data' to 'resources.pack'\n";
    std::cout << "  pack_resources mydata mypack.pack # Pack 'mydata' to 'mypack.pack'\n";
    std::cout << "  pack_resources --list my.pack     # List contents of 'my.pack'\n";
}

void listPackContents(const std::string& pack_file) {
    ResourcePack pack;
    if (!pack.loadPack(pack_file)) {
        std::cerr << "Error: cannot open pack file: " << pack_file << '\n';
        return;
    }
    auto resources = pack.getResourceList();
    std::cout << "Pack file: " << pack_file << '\n';
    std::cout << "Resources: " << resources.size() << '\n';
    for (const auto& r : resources) std::cout << "  " << r << '\n';
}

}  // namespace

int main(int argc, char* argv[]) {
    std::string data_dir = "data";
    std::string output_file = "resources.pack";
    bool list_mode = false;
    bool data_dir_set = false;

    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        if (arg == "--help" || arg == "-h") {
            showHelp();
            return 0;
        }
        if (arg == "--list") {
            list_mode = true;
            if (i + 1 < argc) output_file = argv[++i];
            continue;
        }
        if (!arg.empty() && arg[0] != '-') {
            if (!data_dir_set) {
                data_dir = arg;
                data_dir_set = true;
            } else {
                output_file = arg;
            }
        }
    }

    if (list_mode) {
        listPackContents(output_file);
        return 0;
    }

    std::cout << "AEE - Resource Packer\n=====================\n";
    std::cout << "Input directory: " << data_dir << '\n';
    std::cout << "Output file: " << output_file << '\n';

    if (!std::filesystem::exists(data_dir)) {
        std::cerr << "Error: input directory does not exist: " << data_dir << '\n';
        return 1;
    }

    ResourcePack pack;
    std::cout << "Scanning and packing resources...\n";
    if (!pack.addDirectory(data_dir)) {
        std::cerr << "Error: failed to add directory to pack\n";
        return 1;
    }
    std::cout << "Found " << pack.getResourceCount() << " resources\n";

    std::cout << "Saving pack file...\n";
    if (!pack.savePack(output_file)) {
        std::cerr << "Error: failed to save pack file\n";
        return 1;
    }

    auto file_size = std::filesystem::file_size(std::filesystem::path(output_file));
    std::cout << "Pack file created: " << output_file << " ("
              << (static_cast<double>(file_size) / 1024.0 / 1024.0) << " MB)\n";
    return 0;
}
