const std = @import("std");

pub const Allocator = std.mem.Allocator;

pub const ManEntry = struct {
    name: []const u8,
    section: []const u8,
    description: []const u8,
    path: ?[]const u8 = null,

    pub fn getSectionMarker(self: ManEntry) []const u8 {
        if (self.section.len == 0) return "[?] Unknown";
        // Fast path for common sections
        return switch (self.section[0]) {
            '1' => "[*] Command",
            '2' => "[S] Syscall",
            '3' => blk: {
                // Optimized library detection with early returns
                if (self.section.len >= 4) {
                    if (std.mem.startsWith(u8, self.section, "3ssl")) break :blk "[L] OpenSSL";
                    if (std.mem.startsWith(u8, self.section, "3tcl")) break :blk "[L] Tcl API";
                    if (std.mem.startsWith(u8, self.section, "3pm")) break :blk "[L] Perl";
                    if (std.mem.startsWith(u8, self.section, "3p")) break :blk "[L] POSIX";
                    if (std.mem.startsWith(u8, self.section, "3x")) break :blk "[L] X11/ncurses";
                }
                break :blk "[L] C Library";
            },
            '4' => "[D] Device",
            '5' => "[F] Format",
            '6' => "[G] Game",
            '7' => "[M] Misc",
            '8' => "[A] Admin",
            '9' => "[K] Kernel",
            else => "[?] Unknown",
        };
    }

    pub fn matchesSection(self: ManEntry, target_sections: ?[]const []const u8) bool {
        if (target_sections == null) return true;
        for (target_sections.?) |section| {
            if (self.section.len == 0) continue;
            if (std.mem.eql(u8, self.section, section)) return true;
            if (section.len == 1 and std.mem.startsWith(u8, self.section, section)) return true;
        }
        return false;
    }
};

// Use modern ArrayList instead of custom implementation
pub const ManEntryList = std.ArrayList(ManEntry);

pub const SearchConfig = struct {
    keyword: []const u8,
    target_sections: ?[]const []const u8,
    show_paths: bool,
    verbose: bool,
};
