// Scyla Dataset Notebook — CLI Mode
//
// Interactive tool for creating, editing, and managing
// text pair datasets for AI training.
//
// Usage: scylac run examples/notebook.sy
//
// Aislinn McCubbins, April 2026

use dataset

fn main() {
    println("")
    println("  ╔════════════════════════════════════╗")
    println("  ║     Scyla Dataset Notebook         ║")
    println("  ║     Text Pair Training Data        ║")
    println("  ╚════════════════════════════════════╝")
    println("")

    let data = dataset_new()
    let current_file = ""

    let running = true
    while running {
        println("")
        println("  Dataset: " + int_to_str(dataset_len(data)) + " pairs")
        if str_len(current_file) > 0 {
            println("  File: " + current_file)
        }
        println("")
        println("  Commands:")
        println("    load <file>    — Load CSV, JSONL, TXT, or JSON")
        println("    save <file>    — Save dataset")
        println("    add            — Add a new text pair")
        println("    view           — Preview dataset")
        println("    view all       — Show all pairs")
        println("    edit <n>       — Edit pair at index")
        println("    delete <n>     — Delete pair at index")
        println("    merge <file>   — Merge another dataset into this one")
        println("    shuffle        — Randomize order")
        println("    clear          — Clear all pairs")
        println("    export <file>  — Export (auto-detects format from extension)")
        println("    quit           — Exit")
        println("")

        let cmd = input("  notebook> ")
        let trimmed = str_trim(cmd)

        if str_eq(trimmed, "quit") {
            running = false
        }

        if str_eq(trimmed, "q") {
            running = false
        }

        if starts_with(trimmed, "load ") {
            let path = str_trim(str_slice(trimmed, 5, str_len(trimmed)))
            println("  Loading " + path + "...")
            data = dataset_load(path)
            current_file = path
            println("  Loaded " + int_to_str(dataset_len(data)) + " pairs.")
        }

        if starts_with(trimmed, "save ") {
            let path = str_trim(str_slice(trimmed, 5, str_len(trimmed)))
            let result = dataset_save(data, path)
            current_file = path
            println("  Saved to " + path)
        }

        if starts_with(trimmed, "export ") {
            let path = str_trim(str_slice(trimmed, 7, str_len(trimmed)))
            let result = dataset_save(data, path)
            println("  Exported to " + path)
        }

        if str_eq(trimmed, "add") {
            println("  Enter prompt (then press Enter):")
            let prompt = input("  [P] ")
            println("  Enter response (then press Enter):")
            let response = input("  [R] ")
            data = dataset_add(data, prompt, response)
            println("  Added pair #" + int_to_str(dataset_len(data)))
        }

        if str_eq(trimmed, "view") {
            println("")
            println(dataset_preview(data, 10))
        }

        if str_eq(trimmed, "view all") {
            println("")
            println(dataset_preview(data, 9999))
        }

        if starts_with(trimmed, "edit ") {
            let idx_str = str_trim(str_slice(trimmed, 5, str_len(trimmed)))
            // Simple number parse
            let idx = 0
            let di = 0
            while di < str_len(idx_str) {
                let d = str_char_at(idx_str, di)
                idx = idx * 10
                if str_eq(d, "1") { idx = idx + 1 }
                if str_eq(d, "2") { idx = idx + 2 }
                if str_eq(d, "3") { idx = idx + 3 }
                if str_eq(d, "4") { idx = idx + 4 }
                if str_eq(d, "5") { idx = idx + 5 }
                if str_eq(d, "6") { idx = idx + 6 }
                if str_eq(d, "7") { idx = idx + 7 }
                if str_eq(d, "8") { idx = idx + 8 }
                if str_eq(d, "9") { idx = idx + 9 }
                di = di + 1
            }
            idx = idx - 1
            if idx >= 0 {
                if idx < dataset_len(data) {
                    let old = dataset_get(data, idx)
                    println("  Current prompt:   " + array_get(old, 0))
                    println("  Current response: " + array_get(old, 1))
                    println("  New prompt (Enter to keep):")
                    let new_prompt = input("  [P] ")
                    if str_len(str_trim(new_prompt)) == 0 {
                        new_prompt = array_get(old, 0)
                    }
                    println("  New response (Enter to keep):")
                    let new_response = input("  [R] ")
                    if str_len(str_trim(new_response)) == 0 {
                        new_response = array_get(old, 1)
                    }
                    data = dataset_set(data, idx, new_prompt, new_response)
                    println("  Updated pair #" + int_to_str(idx + 1))
                } else {
                    println("  Index out of range.")
                }
            }
        }

        if starts_with(trimmed, "delete ") {
            let idx_str = str_trim(str_slice(trimmed, 7, str_len(trimmed)))
            let idx = 0
            let di = 0
            while di < str_len(idx_str) {
                let d = str_char_at(idx_str, di)
                idx = idx * 10
                if str_eq(d, "1") { idx = idx + 1 }
                if str_eq(d, "2") { idx = idx + 2 }
                if str_eq(d, "3") { idx = idx + 3 }
                if str_eq(d, "4") { idx = idx + 4 }
                if str_eq(d, "5") { idx = idx + 5 }
                if str_eq(d, "6") { idx = idx + 6 }
                if str_eq(d, "7") { idx = idx + 7 }
                if str_eq(d, "8") { idx = idx + 8 }
                if str_eq(d, "9") { idx = idx + 9 }
                di = di + 1
            }
            idx = idx - 1
            if idx >= 0 {
                if idx < dataset_len(data) {
                    data = dataset_remove(data, idx)
                    println("  Deleted pair #" + int_to_str(idx + 1))
                }
            }
        }

        if starts_with(trimmed, "merge ") {
            let path = str_trim(str_slice(trimmed, 6, str_len(trimmed)))
            let other = dataset_load(path)
            data = dataset_merge(data, other)
            println("  Merged " + int_to_str(dataset_len(other)) + " pairs. Total: " + int_to_str(dataset_len(data)))
        }

        if str_eq(trimmed, "shuffle") {
            data = dataset_shuffle(data)
            println("  Shuffled.")
        }

        if str_eq(trimmed, "clear") {
            data = dataset_new()
            current_file = ""
            println("  Cleared.")
        }
    }

    println("  Goodbye!")
}
