#include "db_manager.h"
#include <iostream>
#include <string>
#include <vector>

void printUsage(const char* program_name) {
    std::cout << "DBManager - SCAR Chat Database Management Tool\n"
              << "Usage: " << program_name << " [OPTIONS] COMMAND [ARGS...]\n"
              << "\nOptions:\n"
              << "  --db <path>        Path to database file (default: scarchat.db)\n"
              << "\nCommands:\n"
              << "  adduser <username> <password> [avatar]\n"
              << "      Create a new user with optional avatar image\n"
              << "\n"
              << "  deleteuser <username>\n"
              << "      Delete a user from the database\n"
              << "\n"
              << "  modifypass <username> <newpassword>\n"
              << "      Change user's password\n"
              << "\n"
              << "  modifyavatar <username> <avatar_file>\n"
              << "      Update user's avatar image\n"
              << "\n"
              << "  modifyemail <username> <email>\n"
              << "      Update user's email address\n"
              << "\n"
              << "  modifyrole <username> <role>\n"
              << "      Update user's role (e.g., admin, moderator, user)\n"
              << "\n"
              << "  fetch <username>\n"
              << "      Display detailed information for a user\n"
              << "\n"
              << "  search <field> <value>\n"
              << "      Search users by field (username, email, role)\n"
              << "\n"
              << "  list\n"
              << "      List all users in the database\n"
              << "\nExamples:\n"
              << "  " << program_name << " adduser alice password123\n"
              << "  " << program_name << " adduser bob secret456 /path/to/avatar.png\n"
              << "  " << program_name << " modifypass alice newpassword\n"
              << "  " << program_name << " modifyrole alice admin\n"
              << "  " << program_name << " search role admin\n"
              << "  " << program_name << " list\n"
              << "  " << program_name << " --db /custom/path/db.db list\n"
              << std::endl;
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printUsage(argv[0]);
        return 1;
    }
    
    std::string db_path;
    int command_start = 1;
    
    // Parse options
    while (command_start < argc && argv[command_start][0] == '-') {
        std::string opt = argv[command_start];
        
        if (opt == "--db" && command_start + 1 < argc) {
            db_path = argv[command_start + 1];
            command_start += 2;
        } else if (opt == "--help" || opt == "-h") {
            printUsage(argv[0]);
            return 0;
        } else {
            std::cerr << "Unknown option: " << opt << std::endl;
            printUsage(argv[0]);
            return 1;
        }
    }
    
    if (command_start >= argc) {
        std::cerr << "Error: No command specified" << std::endl;
        printUsage(argv[0]);
        return 1;
    }
    
    // Find database
    db_path = scar::DBManager::findDatabase(db_path);
    std::cout << "Using database: " << db_path << std::endl << std::endl;
    
    try {
        scar::DBManager manager(db_path);
        
        std::string command = argv[command_start];
        std::vector<std::string> args;
        for (int i = command_start + 1; i < argc; ++i) {
            args.push_back(argv[i]);
        }
        
        // Execute command
        if (command == "adduser") {
            if (args.size() < 2) {
                std::cerr << "Usage: adduser <username> <password> [avatar]" << std::endl;
                return 1;
            }
            std::string avatar = (args.size() >= 3) ? args[2] : "";
            return manager.addUser(args[0], args[1], avatar) ? 0 : 1;
            
        } else if (command == "deleteuser") {
            if (args.size() < 1) {
                std::cerr << "Usage: deleteuser <username>" << std::endl;
                return 1;
            }
            return manager.deleteUser(args[0]) ? 0 : 1;
            
        } else if (command == "modifypass") {
            if (args.size() < 2) {
                std::cerr << "Usage: modifypass <username> <newpassword>" << std::endl;
                return 1;
            }
            return manager.modifyPassword(args[0], args[1]) ? 0 : 1;
            
        } else if (command == "modifyavatar") {
            if (args.size() < 2) {
                std::cerr << "Usage: modifyavatar <username> <avatar_file>" << std::endl;
                return 1;
            }
            return manager.modifyAvatar(args[0], args[1]) ? 0 : 1;
            
        } else if (command == "modifyemail") {
            if (args.size() < 2) {
                std::cerr << "Usage: modifyemail <username> <email>" << std::endl;
                return 1;
            }
            return manager.modifyEmail(args[0], args[1]) ? 0 : 1;
            
        } else if (command == "modifyrole") {
            if (args.size() < 2) {
                std::cerr << "Usage: modifyrole <username> <role>" << std::endl;
                return 1;
            }
            return manager.modifyRole(args[0], args[1]) ? 0 : 1;
            
        } else if (command == "fetch") {
            if (args.size() < 1) {
                std::cerr << "Usage: fetch <username>" << std::endl;
                return 1;
            }
            manager.fetchUser(args[0]);
            return 0;
            
        } else if (command == "search") {
            if (args.size() < 2) {
                std::cerr << "Usage: search <field> <value>" << std::endl;
                std::cerr << "Fields: username, email, role" << std::endl;
                return 1;
            }
            manager.searchUsers(args[0], args[1]);
            return 0;
            
        } else if (command == "list") {
            manager.listAllUsers();
            return 0;
            
        } else {
            std::cerr << "Unknown command: " << command << std::endl;
            printUsage(argv[0]);
            return 1;
        }
        
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}
