#!/bin/bash

# Script to convert .txt files to .rs Leptos components
# Usage: ./convert_txt_to_rs.sh

set -e

# Directory paths
TXT_DIR="LEPTOS_LUCIDE_TXT"
RS_DIR="src/leptos_full"

# Function to convert snake_case to PascalCase
snake_to_pascal() {
    local snake_case="$1"
    # Remove .txt extension
    local name="${snake_case%.txt}"
    # Split by underscore and capitalize each part
    echo "$name" | sed 's/_/ /g' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))} 1' | sed 's/ //g'
}

# Function to check if a name is a Rust keyword
is_rust_keyword() {
    local name="$1"
    case "$name" in
        "move"|"box"|"type"|"as"|"break"|"const"|"continue"|"crate"|"else"|"enum"|"extern"|"false"|"fn"|"for"|"if"|"impl"|"in"|"let"|"loop"|"match"|"mod"|"mut"|"pub"|"ref"|"return"|"self"|"Self"|"static"|"struct"|"super"|"trait"|"true"|"unsafe"|"use"|"where"|"while"|"async"|"await"|"dyn"|"abstract"|"become"|"do"|"final"|"macro"|"override"|"priv"|"typeof"|"unsized"|"virtual"|"yield"|"try")
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

# Function to get safe module name (with r# prefix if needed)
get_safe_module_name() {
    local name="$1"
    if is_rust_keyword "$name"; then
        echo "r#${name}"
    else
        echo "$name"
    fi
}

# Function to generate Rust component
generate_component() {
    local txt_file="$1"
    local component_name="$2"
    local rs_file="$3"
    
    # Read SVG paths from txt file
    local svg_paths
    svg_paths=$(cat "$txt_file")
    
    # Generate the Rust component
    cat > "$rs_file" << EOF
use leptos::prelude::*;

use crate::_svg_icon::SvgIcon;

#[component]
pub fn ${component_name}(#[prop(into, optional)] class: Signal<String>) -> impl IntoView {
    view! {
        <SvgIcon class=class>
            <title>"Rust UI Icons - ${component_name}"</title>

${svg_paths}
        </SvgIcon>
    }
}
EOF
}

# Function to generate mod.rs file
generate_mod_rs() {
    local mod_file="$RS_DIR/mod.rs"
    
    echo "Generating mod.rs file..."
    
    # Start with the base SvgIcon module
    cat > "$mod_file" << EOF
pub mod _svg_icon_full;
EOF
    
    # Add module declarations for all components
    for txt_file in "$TXT_DIR"/*.txt; do
        if [ ! -f "$txt_file" ]; then
            continue
        fi
        
        filename=$(basename "$txt_file")
        module_name="${filename%.txt}"
        safe_module_name=$(get_safe_module_name "$module_name")
        
        echo "pub mod ${safe_module_name};" >> "$mod_file"
    done
    
    # Add empty line and re-exports
    echo "" >> "$mod_file"
    echo "pub use _svg_icon_full::SvgIcon;" >> "$mod_file"
    
    # Add re-exports for all components
    for txt_file in "$TXT_DIR"/*.txt; do
        if [ ! -f "$txt_file" ]; then
            continue
        fi
        
        filename=$(basename "$txt_file")
        module_name="${filename%.txt}"
        safe_module_name=$(get_safe_module_name "$module_name")
        component_name=$(snake_to_pascal "$filename")
        
        echo "pub use ${safe_module_name}::${component_name};" >> "$mod_file"
    done
}

# Main conversion loop
echo "Converting .txt files to .rs components..."

if [ ! -d "$TXT_DIR" ]; then
    echo "Error: Directory $TXT_DIR not found!"
    exit 1
fi

if [ ! -d "$RS_DIR" ]; then
    echo "Creating directory $RS_DIR..."
    mkdir -p "$RS_DIR"
fi

# Process each .txt file
for txt_file in "$TXT_DIR"/*.txt; do
    if [ ! -f "$txt_file" ]; then
        echo "No .txt files found in $TXT_DIR"
        continue
    fi
    
    # Extract filename without path and extension
    filename=$(basename "$txt_file")
    
    # Convert to PascalCase component name
    component_name=$(snake_to_pascal "$filename")
    
    # Generate output .rs filename (keep snake_case for file)
    rs_filename="${filename%.txt}.rs"
    rs_file="$RS_DIR/$rs_filename"
    
    echo "Converting: $filename -> $rs_filename (component: $component_name)"
    
    # Generate the component
    generate_component "$txt_file" "$component_name" "$rs_file"
done

# Generate mod.rs file
generate_mod_rs

echo "Conversion complete!"
echo "Generated components in: $RS_DIR"
echo "Updated mod.rs with all module declarations and re-exports"