use crate::shared::state::AppState; use axum::{ extract::{Query, State}, response::{Html, IntoResponse}, routing::{get, post}, Json, Router, }; use chrono::{DateTime, Utc}; use diesel::prelude::*; use serde::{Deserialize, Serialize}; use std::sync::Arc; use uuid::Uuid; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SaveRequest { pub name: Option, pub content: Option, pub nodes: Option, pub connections: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ValidateRequest { pub content: Option, pub nodes: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct FileQuery { pub path: Option, } #[derive(Debug, QueryableByName)] #[diesel(check_for_backend(diesel::pg::Pg))] pub struct DialogRow { #[diesel(sql_type = diesel::sql_types::Text)] pub id: String, #[diesel(sql_type = diesel::sql_types::Text)] pub name: String, #[diesel(sql_type = diesel::sql_types::Text)] pub content: String, #[diesel(sql_type = diesel::sql_types::Timestamptz)] pub updated_at: DateTime, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ValidationResult { pub valid: bool, pub errors: Vec, pub warnings: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ValidationError { pub line: usize, pub column: usize, pub message: String, pub node_id: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ValidationWarning { pub line: usize, pub message: String, pub node_id: Option, } pub fn configure_designer_routes() -> Router> { Router::new() // Match frontend /api/v1/designer/* endpoints .route("/api/v1/designer/files", get(handle_list_files)) .route("/api/v1/designer/load", get(handle_load_file)) .route("/api/v1/designer/save", post(handle_save)) .route("/api/v1/designer/validate", post(handle_validate)) .route("/api/v1/designer/export", get(handle_export)) // Legacy endpoints for compatibility .route( "/api/designer/dialogs", get(handle_list_dialogs).post(handle_create_dialog), ) .route("/api/designer/dialogs/{id}", get(handle_get_dialog)) } /// GET /api/v1/designer/files - List available dialog files pub async fn handle_list_files(State(state): State>) -> impl IntoResponse { let conn = state.conn.clone(); let files = tokio::task::spawn_blocking(move || { let mut db_conn = match conn.get() { Ok(c) => c, Err(e) => { log::error!("DB connection error: {}", e); return get_default_files(); } }; let result: Result, _> = diesel::sql_query( "SELECT id, name, content, updated_at FROM designer_dialogs ORDER BY updated_at DESC LIMIT 50", ) .load(&mut db_conn); match result { Ok(dialogs) if !dialogs.is_empty() => dialogs .into_iter() .map(|d| (d.id, d.name, d.updated_at)) .collect(), _ => get_default_files(), } }) .await .unwrap_or_else(|_| get_default_files()); let mut html = String::new(); html.push_str("
"); for (id, name, updated_at) in &files { let time_str = format_relative_time(*updated_at); html.push_str("
"); html.push_str("
"); html.push_str(""); html.push_str( "", ); html.push_str(""); html.push_str(""); html.push_str("
"); html.push_str("
"); html.push_str(""); html.push_str(&html_escape(name)); html.push_str(""); html.push_str(""); html.push_str(&html_escape(&time_str)); html.push_str(""); html.push_str("
"); html.push_str("
"); } if files.is_empty() { html.push_str("
"); html.push_str("

No dialog files found

"); html.push_str("

Create a new dialog to get started

"); html.push_str("
"); } html.push_str("
"); Html(html) } fn get_default_files() -> Vec<(String, String, DateTime)> { vec![ ( "welcome".to_string(), "Welcome Dialog".to_string(), Utc::now(), ), ("faq".to_string(), "FAQ Bot".to_string(), Utc::now()), ( "support".to_string(), "Customer Support".to_string(), Utc::now(), ), ] } /// GET /api/v1/designer/load - Load a specific dialog file pub async fn handle_load_file( State(state): State>, Query(params): Query, ) -> impl IntoResponse { let file_id = params.path.unwrap_or_else(|| "welcome".to_string()); let conn = state.conn.clone(); let dialog = tokio::task::spawn_blocking(move || { let mut db_conn = match conn.get() { Ok(c) => c, Err(e) => { log::error!("DB connection error: {}", e); return None; } }; diesel::sql_query( "SELECT id, name, content, updated_at FROM designer_dialogs WHERE id = $1", ) .bind::(&file_id) .get_result::(&mut db_conn) .ok() }) .await .unwrap_or(None); let content = match dialog { Some(d) => d.content, None => get_default_dialog_content(), }; // Return the canvas nodes as HTML for HTMX to swap let mut html = String::new(); html.push_str("
"); // Parse content and generate node HTML let nodes = parse_basic_to_nodes(&content); for node in &nodes { html.push_str(&format_node_html(node)); } html.push_str("
"); html.push_str(""); Html(html) } /// POST /api/v1/designer/save - Save dialog pub async fn handle_save( State(state): State>, Json(payload): Json, ) -> impl IntoResponse { let conn = state.conn.clone(); let now = Utc::now(); let name = payload.name.unwrap_or_else(|| "Untitled".to_string()); let content = payload.content.unwrap_or_default(); let dialog_id = Uuid::new_v4().to_string(); let result = tokio::task::spawn_blocking(move || { let mut db_conn = match conn.get() { Ok(c) => c, Err(e) => { log::error!("DB connection error: {}", e); return Err(format!("Database error: {}", e)); } }; diesel::sql_query( "INSERT INTO designer_dialogs (id, name, description, bot_id, content, is_active, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT (id) DO UPDATE SET content = $5, updated_at = $8", ) .bind::(&dialog_id) .bind::(&name) .bind::("") .bind::("default") .bind::(&content) .bind::(false) .bind::(now) .bind::(now) .execute(&mut db_conn) .map_err(|e| format!("Save failed: {}", e))?; Ok(()) }) .await .unwrap_or_else(|e| Err(format!("Task error: {}", e))); match result { Ok(_) => { let mut html = String::new(); html.push_str("
"); html.push_str("*"); html.push_str("Saved successfully"); html.push_str("
"); Html(html) } Err(e) => { let mut html = String::new(); html.push_str("
"); html.push_str("x"); html.push_str("Save failed: "); html.push_str(&html_escape(&e)); html.push_str(""); html.push_str("
"); Html(html) } } } /// POST /api/v1/designer/validate - Validate dialog code pub async fn handle_validate( State(_state): State>, Json(payload): Json, ) -> impl IntoResponse { let content = payload.content.unwrap_or_default(); let validation = validate_basic_code(&content); let mut html = String::new(); html.push_str("
"); if validation.valid { html.push_str("
"); html.push_str("*"); html.push_str("Dialog is valid"); html.push_str("
"); } else { html.push_str("
"); html.push_str("
"); html.push_str("x"); html.push_str(""); html.push_str(&validation.errors.len().to_string()); html.push_str(" error(s) found"); html.push_str("
"); html.push_str("
    "); for error in &validation.errors { html.push_str("
  • "); html.push_str("Line "); html.push_str(&error.line.to_string()); html.push_str(": "); html.push_str(&html_escape(&error.message)); html.push_str("
  • "); } html.push_str("
"); html.push_str("
"); } if !validation.warnings.is_empty() { html.push_str("
"); html.push_str("
"); html.push_str("!"); html.push_str(""); html.push_str(&validation.warnings.len().to_string()); html.push_str(" warning(s)"); html.push_str("
"); html.push_str("
    "); for warning in &validation.warnings { html.push_str("
  • "); html.push_str("Line "); html.push_str(&warning.line.to_string()); html.push_str(": "); html.push_str(&html_escape(&warning.message)); html.push_str("
  • "); } html.push_str("
"); html.push_str("
"); } html.push_str("
"); Html(html) } /// GET /api/v1/designer/export - Export dialog as .bas file pub async fn handle_export( State(_state): State>, Query(params): Query, ) -> impl IntoResponse { let _file_id = params.path.unwrap_or_else(|| "dialog".to_string()); // In production, this would generate and download the file Html("".to_string()) } /// GET /api/designer/dialogs - List dialogs (legacy endpoint) pub async fn handle_list_dialogs(State(state): State>) -> impl IntoResponse { let conn = state.conn.clone(); let dialogs = tokio::task::spawn_blocking(move || { let mut db_conn = match conn.get() { Ok(c) => c, Err(e) => { log::error!("DB connection error: {}", e); return Vec::new(); } }; diesel::sql_query( "SELECT id, name, content, updated_at FROM designer_dialogs ORDER BY updated_at DESC LIMIT 50", ) .load::(&mut db_conn) .unwrap_or_default() }) .await .unwrap_or_default(); let mut html = String::new(); html.push_str("
"); for dialog in &dialogs { html.push_str("
"); html.push_str("

"); html.push_str(&html_escape(&dialog.name)); html.push_str("

"); html.push_str(""); html.push_str(&format_relative_time(dialog.updated_at)); html.push_str(""); html.push_str("
"); } if dialogs.is_empty() { html.push_str("
"); html.push_str("

No dialogs yet

"); html.push_str("
"); } html.push_str("
"); Html(html) } /// POST /api/designer/dialogs - Create new dialog (legacy endpoint) pub async fn handle_create_dialog( State(state): State>, Json(payload): Json, ) -> impl IntoResponse { let conn = state.conn.clone(); let now = Utc::now(); let dialog_id = Uuid::new_v4().to_string(); let name = payload.name.unwrap_or_else(|| "New Dialog".to_string()); let content = payload.content.unwrap_or_else(get_default_dialog_content); let result = tokio::task::spawn_blocking(move || { let mut db_conn = match conn.get() { Ok(c) => c, Err(e) => { log::error!("DB connection error: {}", e); return Err(format!("Database error: {}", e)); } }; diesel::sql_query( "INSERT INTO designer_dialogs (id, name, description, bot_id, content, is_active, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", ) .bind::(&dialog_id) .bind::(&name) .bind::("") .bind::("default") .bind::(&content) .bind::(false) .bind::(now) .bind::(now) .execute(&mut db_conn) .map_err(|e| format!("Create failed: {}", e))?; Ok(dialog_id) }) .await .unwrap_or_else(|e| Err(format!("Task error: {}", e))); match result { Ok(id) => { let mut html = String::new(); html.push_str("
"); html.push_str("Dialog created"); html.push_str("
"); Html(html) } Err(e) => { let mut html = String::new(); html.push_str("
"); html.push_str(&html_escape(&e)); html.push_str("
"); Html(html) } } } /// GET /api/designer/dialogs/{id} - Get specific dialog (legacy endpoint) pub async fn handle_get_dialog( State(state): State>, axum::extract::Path(id): axum::extract::Path, ) -> impl IntoResponse { let conn = state.conn.clone(); let dialog = tokio::task::spawn_blocking(move || { let mut db_conn = match conn.get() { Ok(c) => c, Err(e) => { log::error!("DB connection error: {}", e); return None; } }; diesel::sql_query( "SELECT id, name, content, updated_at FROM designer_dialogs WHERE id = $1", ) .bind::(&id) .get_result::(&mut db_conn) .ok() }) .await .unwrap_or(None); match dialog { Some(d) => { let mut html = String::new(); html.push_str("
"); html.push_str("
"); html.push_str("

"); html.push_str(&html_escape(&d.name)); html.push_str("

"); html.push_str("
"); html.push_str("
"); html.push_str("
");
            html.push_str(&html_escape(&d.content));
            html.push_str("
"); html.push_str("
"); html.push_str("
"); Html(html) } None => Html("
Dialog not found
".to_string()), } } fn validate_basic_code(code: &str) -> ValidationResult { let mut errors = Vec::new(); let mut warnings = Vec::new(); let lines: Vec<&str> = code.lines().collect(); for (i, line) in lines.iter().enumerate() { let line_num = i + 1; let trimmed = line.trim(); if trimmed.is_empty() || trimmed.starts_with('\'') || trimmed.starts_with("REM ") { continue; } // Check for common syntax issues let upper = trimmed.to_uppercase(); if upper.starts_with("IF ") && !upper.contains(" THEN") { errors.push(ValidationError { line: line_num, column: 1, message: "IF statement missing THEN keyword".to_string(), node_id: None, }); } if upper.starts_with("FOR ") && !upper.contains(" TO ") { errors.push(ValidationError { line: line_num, column: 1, message: "FOR statement missing TO keyword".to_string(), node_id: None, }); } // Check for unclosed strings let quote_count = trimmed.chars().filter(|c| *c == '"').count(); if quote_count % 2 != 0 { errors.push(ValidationError { line: line_num, column: trimmed.find('"').unwrap_or(0) + 1, message: "Unclosed string literal".to_string(), node_id: None, }); } // Warnings if upper.starts_with("GOTO ") { warnings.push(ValidationWarning { line: line_num, message: "GOTO statements can make code harder to maintain".to_string(), node_id: None, }); } if trimmed.len() > 120 { warnings.push(ValidationWarning { line: line_num, message: "Line exceeds recommended length of 120 characters".to_string(), node_id: None, }); } } // Check block structures let mut if_count = 0i32; let mut for_count = 0i32; let mut sub_count = 0i32; for line in &lines { let upper = line.to_uppercase(); let trimmed = upper.trim(); if trimmed.starts_with("IF ") && !trimmed.ends_with(" THEN") && trimmed.contains(" THEN") { // Single-line IF } else if trimmed.starts_with("IF ") { if_count += 1; } else if trimmed == "END IF" || trimmed == "ENDIF" { if_count -= 1; } if trimmed.starts_with("FOR ") { for_count += 1; } else if trimmed == "NEXT" || trimmed.starts_with("NEXT ") { for_count -= 1; } if trimmed.starts_with("SUB ") { sub_count += 1; } else if trimmed == "END SUB" { sub_count -= 1; } } if if_count > 0 { errors.push(ValidationError { line: lines.len(), column: 1, message: format!("{} unclosed IF statement(s)", if_count), node_id: None, }); } if for_count > 0 { errors.push(ValidationError { line: lines.len(), column: 1, message: format!("{} unclosed FOR loop(s)", for_count), node_id: None, }); } if sub_count > 0 { errors.push(ValidationError { line: lines.len(), column: 1, message: format!("{} unclosed SUB definition(s)", sub_count), node_id: None, }); } ValidationResult { valid: errors.is_empty(), errors, warnings, } } fn get_default_dialog_content() -> String { "' Welcome Dialog\n\ ' Created with Dialog Designer\n\ \n\ SUB Main()\n\ TALK \"Hello! How can I help you today?\"\n\ \n\ answer = HEAR\n\ \n\ IF answer LIKE \"*help*\" THEN\n\ TALK \"I'm here to assist you.\"\n\ ELSE IF answer LIKE \"*bye*\" THEN\n\ TALK \"Goodbye!\"\n\ ELSE\n\ TALK \"I understand: \" + answer\n\ END IF\n\ END SUB\n" .to_string() } struct DialogNode { id: String, node_type: String, content: String, x: i32, y: i32, } fn parse_basic_to_nodes(content: &str) -> Vec { let mut nodes = Vec::new(); let mut y_pos = 100; for (i, line) in content.lines().enumerate() { let trimmed = line.trim(); if trimmed.is_empty() || trimmed.starts_with('\'') { continue; } let upper = trimmed.to_uppercase(); let node_type = if upper.starts_with("TALK ") { "talk" } else if upper.starts_with("HEAR") { "hear" } else if upper.starts_with("IF ") { "if" } else if upper.starts_with("FOR ") { "for" } else if upper.starts_with("SET ") || upper.contains(" = ") { "set" } else if upper.starts_with("CALL ") { "call" } else if upper.starts_with("SUB ") { "sub" } else { continue; }; nodes.push(DialogNode { id: format!("node-{}", i), node_type: node_type.to_string(), content: trimmed.to_string(), x: 400, y: y_pos, }); y_pos += 80; } nodes } fn format_node_html(node: &DialogNode) -> String { let mut html = String::new(); html.push_str("
"); html.push_str("
"); html.push_str(""); html.push_str(&node.node_type.to_uppercase()); html.push_str(""); html.push_str("
"); html.push_str("
"); html.push_str(&html_escape(&node.content)); html.push_str("
"); html.push_str("
"); html.push_str("
"); html.push_str("
"); html.push_str("
"); html.push_str("
"); html } fn format_relative_time(time: DateTime) -> String { let now = Utc::now(); let duration = now.signed_duration_since(time); if duration.num_seconds() < 60 { "just now".to_string() } else if duration.num_minutes() < 60 { format!("{}m ago", duration.num_minutes()) } else if duration.num_hours() < 24 { format!("{}h ago", duration.num_hours()) } else if duration.num_days() < 7 { format!("{}d ago", duration.num_days()) } else { time.format("%b %d").to_string() } } fn html_escape(s: &str) -> String { s.replace('&', "&") .replace('<', "<") .replace('>', ">") .replace('"', """) .replace('\'', "'") }