package main

import (
    "bufio"
    "fmt"
    "math/rand"
    "os"
    "strconv"
    "strings"
    "time"
)

const filename = "CharacterData.txt"                                                                      // The file where Character data will be stored

func main() {                                                                                             // Create a variable to capture user pressing Enter
    var input string
    fmt.Println("Press Enter to generate a random NPC...")
    for {                                                                                                 // Infinite loop allowing repeated NPC generation
        fmt.Scanln(&input)                                                                                // Wait for user to press Enter
        npc := generateNPC()                                                                              // Generate and output the NPC
        npcOutput := formatOutput(npc)
        fmt.Println("Generated NPC Output:", npcOutput)
        appendToFile(npcOutput)                                                                           // Append the NPC output to the text file
    }
}

func getNextID() int {                                                                                    // Function to read existing data from the file and find the next ID
    file, err := os.Open(filename)
    if err != nil {
        return 1                                                                                          // If the file doesn't exist, return ID 1
    }
    defer file.Close()
    scanner := bufio.NewScanner(file)
    maxID := 0
    for scanner.Scan() {
        line := scanner.Text()
        parts := strings.Split(line, ",")
        if len(parts) > 0 {
            idStr := strings.Trim(parts[0], "[c/")                                                        // Extract the ID (first part of the line)
            if id, err := strconv.Atoi(idStr); err == nil && id > maxID {
                maxID = id
            }
        }
    }
    return maxID + 1                                                                                      // Next ID is maxID + 1
}

func generateNPC() []int {                                                                                // Function to generate a random NPC
    rand.Seed(time.Now().UnixNano())                                                                      // Seed rand with the current time
    playerID := getNextID()                                                                               // Get the next sequential NPC ID
	firstsexgene1 := rand.Intn(2) + 1                                                                     // Gender selection logic, firstsexgene1 can be 1 or 2
	firstsexgene2 := rand.Intn(2) + 1                                                                     // Gender selection logic, firstsexgene2 can be 1 or 2
    secondsexgene1 := rand.Intn(3) + 1                                                                    // Gender selection logic, secondsexgene1 can be 1, 2, or 3
	secondsexgene2 := rand.Intn(3) + 1                                                                    // Gender selection logic, secondsexgene2 can be 1, 2, or 3
	gender := []int{firstsexgene1, firstsexgene2, secondsexgene1, secondsexgene2}                         // Represent the gender using a composite representation, store in an array
    skinColors := []int{                                                                                  // Random skin colors (RGB between 0-255)
        rand.Intn(57) + 200,                                                                              // R between 200 and 255 (inclusive)
        rand.Intn(77) + 150,                                                                              // G between 150 and 226 (inclusive)
        rand.Intn(81) + 120,                                                                              // B between 120 and 200 (inclusive)
    }
    eyeColors := []int{                                                                                   // Random eye colors (RGB between 0-255)
        rand.Intn(256),                                                                                   // R between 0 and 255 (inclusive)
        rand.Intn(256),                                                                                   // G between 0 and 255 (inclusive)
        rand.Intn(256),                                                                                   // B between 0 and 255 (inclusive)
    }
	output := []int{playerID, gender}                                                                     // Combine created NPC attributes into a single slice
	output = append(output, skinColors...)
	output = append(output, eyeColors...)
	return output
}

func intSliceToStringSlice(ints []int) []string {                                                         // Function to convert []int to []string
	strSlice := make([]string, len(ints))
	for i, v := range ints {
	    strSlice[i] = fmt.Sprintf("%d", v)
    }
    return strSlice
}	

func formatOutput(output []int) string {                                                                  // Function to format output as expected
    // Convert the slice of integers to strings
    stringOutput := intSliceToStringSlice(output)
    return fmt.Sprintf("[c/%s]", strings.Join(stringOutput, ","))
}

func appendToFile(data string) {                                                                          // Function to append the generated NPC output to the text file
    file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file.Close()

    if _, err := file.WriteString(data + "\n"); err != nil {
        fmt.Println("Error writing to file:", err)
    }
}
What's wrong?