import Foundation

/// Generates a friendly session name as three hyphenated dictionary words
/// (e.g. `amber-quiet-otter`), used as both the display title and the worktree
/// branch slug when the user creates a chat without naming it.
public enum RandomName {
    private static let adjectives = [
        "amber", "brisk", "calm", "dapper", "eager", "fuzzy", "gentle", "hazy",
        "ivory", "jolly", "keen", "lucid", "mellow", "nimble", "olive", "plucky",
        "quiet", "rustic", "sleek", "tidy", "upbeat", "vivid", "warm", "zesty",
        "bold", "clever", "dusky", "frosty", "golden", "humble",
    ]
    private static let textures = [
        "ancient", "breezy", "coral", "dewy", "ember", "feather", "glass", "harbor",
        "iris", "jade", "kelp", "lunar", "maple", "north", "opal", "pebble",
        "quartz", "river", "spruce", "thistle", "umber", "velvet", "willow", "yarn",
        "cedar", "drift", "fern", "grove", "meadow", "slate",
    ]
    private static let animals = [
        "otter", "falcon", "lynx", "heron", "bison", "marten", "raven", "shrew",
        "tapir", "vole", "wren", "yak", "badger", "civet", "dingo", "egret",
        "ferret", "gecko", "ibis", "koala", "lemur", "newt", "panda", "quail",
        "robin", "seal", "toad", "urchin", "viper", "weasel",
    ]

    public static func generate() -> String {
        let a = adjectives.randomElement() ?? "amber"
        let b = textures.randomElement() ?? "quiet"
        let c = animals.randomElement() ?? "otter"
        return "\(a)-\(b)-\(c)"
    }

    /// Whether a title looks like one this generator produced — three lowercase
    /// hyphenated words — i.e. an unnamed chat, still eligible to be auto-renamed
    /// from its first message (an explicit title isn't). Structural rather than exact
    /// word-list membership so it's robust to the lists evolving.
    public static func isGenerated(_ title: String) -> Bool {
        let parts = title.split(separator: "-", omittingEmptySubsequences: false)
        guard parts.count == 3 else { return false }
        return parts.allSatisfy { part in
            !part.isEmpty && part.allSatisfy { ("a"..."z").contains($0) }
        }
    }
}
