mockforge-ui 0.3.88

Admin UI for MockForge - web-based interface for managing mock servers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! AI Studio API handlers for Admin UI
//!
//! Provides endpoints for the unified AI Studio interface, including natural language
//! chat, mock generation, debugging, persona generation, and artifact freezing.

use axum::{
    extract::{Json, Query, State},
    http::StatusCode,
    response::Json as ResponseJson,
};
use json_patch::{patch, Patch};
use jsonptr::PointerBuf;
use mockforge_core::ai_studio::{
    get_conversation_store, initialize_conversation_store, ArtifactFreezer, BudgetConfig,
    BudgetManager, ChatContext, ChatMessage, ChatOrchestrator, ChatRequest, ChatResponse,
    ContractDiffHandler, ContractDiffQueryResult, DebugAnalyzer, DebugContextIntegrator,
    DebugRequest, DebugResponse, FreezeRequest, FrozenArtifact, MockGenerator, OrgAiControlsConfig,
    OrgControls, PersonaGenerationRequest, PersonaGenerationResponse, PersonaGenerator, UsageStats,
};
use mockforge_core::intelligent_behavior::IntelligentBehaviorConfig;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

use crate::handlers::AdminState;
use crate::models::ApiResponse;
use mockforge_core::ai_studio::config::DeterministicModeConfig;

/// Request for chat interaction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatRequestPayload {
    /// User's message
    pub message: String,

    /// Optional conversation ID
    pub conversation_id: Option<String>,

    /// Optional workspace ID
    pub workspace_id: Option<String>,
}

/// Process a chat message
///
/// POST /api/v1/ai-studio/chat
pub async fn chat(
    Json(request): Json<ChatRequestPayload>,
) -> Result<ResponseJson<ApiResponse<ChatResponse>>, StatusCode> {
    if request.message.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    // Create orchestrator with default config
    let config = IntelligentBehaviorConfig::default();
    let orchestrator = ChatOrchestrator::new(config);

    // Initialize conversation store if not already done
    // Note: In production, this should be done at application startup
    let _ = initialize_conversation_store().await;

    // Load conversation context if conversation_id provided
    let context = if let Some(conv_id) = &request.conversation_id {
        let store = get_conversation_store();
        match store.get_context(conv_id).await {
            Ok(Some(ctx)) => Some(ctx),
            Ok(None) => {
                // Conversation not found, create new context
                Some(ChatContext {
                    history: vec![],
                    workspace_id: request.workspace_id.clone(),
                })
            }
            Err(_) => {
                // Error loading, use empty context
                Some(ChatContext {
                    history: vec![],
                    workspace_id: request.workspace_id.clone(),
                })
            }
        }
    } else {
        None
    };

    // Build chat request
    let chat_request = ChatRequest {
        message: request.message.clone(),
        context,
        workspace_id: request.workspace_id.clone(),
        org_id: None,
        user_id: None,
    };

    // Process request
    let response_result = orchestrator.process(&chat_request).await;

    // Save conversation history if conversation_id provided
    if let Some(conv_id) = &request.conversation_id {
        let store = get_conversation_store();

        // Add user message to conversation
        let user_message = ChatMessage {
            role: "user".to_string(),
            content: request.message.clone(),
        };
        let _ = store.add_message(conv_id, user_message).await;

        // Add assistant response to conversation
        if let Ok(ref response) = response_result {
            let assistant_message = ChatMessage {
                role: "assistant".to_string(),
                content: response.message.clone(),
            };
            let _ = store.add_message(conv_id, assistant_message).await;
        }
    } else {
        // Create new conversation if none specified
        let store = get_conversation_store();
        if let Ok(conv_id) = store.create_conversation(request.workspace_id.clone()).await {
            // Add messages to new conversation
            let user_message = ChatMessage {
                role: "user".to_string(),
                content: request.message.clone(),
            };
            let _ = store.add_message(&conv_id, user_message).await;

            if let Ok(ref response) = response_result {
                let assistant_message = ChatMessage {
                    role: "assistant".to_string(),
                    content: response.message.clone(),
                };
                let _ = store.add_message(&conv_id, assistant_message).await;
            }
        }
    }

    // Return response
    match response_result {
        Ok(response) => Ok(ResponseJson(ApiResponse::success(response))),
        Err(e) => Ok(ResponseJson(ApiResponse::error(format!("Failed to process chat: {}", e)))),
    }
}

/// Request for mock generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerateMockRequest {
    /// Natural language description
    pub description: String,

    /// Optional workspace ID
    pub workspace_id: Option<String>,
}

/// Response from mock generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerateMockResponse {
    /// Generated OpenAPI spec (if any)
    pub spec: Option<Value>,

    /// Status message
    pub message: String,
}

/// Generate a mock from natural language
///
/// POST /api/v1/ai-studio/generate-mock
pub async fn generate_mock(
    State(state): State<AdminState>,
    Json(request): Json<GenerateMockRequest>,
) -> Result<ResponseJson<ApiResponse<GenerateMockResponse>>, StatusCode> {
    if request.description.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    let generator = MockGenerator::new();

    // Get workspace config to check ai_mode and deterministic config
    let ai_mode = if let Some(workspace_id) = &request.workspace_id {
        // Try to load workspace to get ai_mode
        if let Ok(workspace) = state.workspace_persistence.load_workspace(workspace_id).await {
            workspace.config.ai_mode
        } else {
            None // Default to live mode if workspace not found
        }
    } else {
        None // Default to live mode if no workspace_id provided
    };

    // Get deterministic config from workspace if available
    let deterministic_config = if let Some(workspace_id) = &request.workspace_id {
        if let Ok(_workspace) = state.workspace_persistence.load_workspace(workspace_id).await {
            // Check if workspace has deterministic mode config
            // For now, use default config - in production this would come from workspace config
            Some(DeterministicModeConfig::default())
        } else {
            None
        }
    } else {
        None
    };

    match generator
        .generate(
            &request.description,
            request.workspace_id.as_deref(),
            ai_mode,
            deterministic_config.as_ref(),
        )
        .await
    {
        Ok(result) => {
            let response = GenerateMockResponse {
                spec: result.spec,
                message: result.message,
            };
            Ok(ResponseJson(ApiResponse::success(response)))
        }
        Err(e) => Ok(ResponseJson(ApiResponse::error(format!("Failed to generate mock: {}", e)))),
    }
}

/// Request for debug analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugTestRequest {
    /// Test failure logs
    pub test_logs: String,

    /// Test name/identifier
    pub test_name: Option<String>,

    /// Workspace ID
    pub workspace_id: Option<String>,
}

/// Analyze a test failure
///
/// POST /api/v1/ai-studio/debug-test
pub async fn debug_test(
    Json(request): Json<DebugTestRequest>,
) -> Result<ResponseJson<ApiResponse<DebugResponse>>, StatusCode> {
    if request.test_logs.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    let analyzer = DebugAnalyzer::new();

    let debug_request = DebugRequest {
        test_logs: request.test_logs,
        test_name: request.test_name,
        workspace_id: request.workspace_id,
    };

    match analyzer.analyze(&debug_request).await {
        Ok(response) => Ok(ResponseJson(ApiResponse::success(response))),
        Err(e) => Ok(ResponseJson(ApiResponse::error(format!(
            "Failed to analyze test failure: {}",
            e
        )))),
    }
}

/// Request for debug analysis with context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebugWithContextRequest {
    /// Test failure logs
    pub test_logs: String,

    /// Test name/identifier
    pub test_name: Option<String>,

    /// Workspace ID
    pub workspace_id: Option<String>,

    /// Organization ID (for context)
    pub org_id: Option<String>,
}

/// Analyze a test failure with comprehensive context from subsystems
///
/// POST /api/v1/ai-studio/debug/analyze-with-context
pub async fn debug_analyze_with_context(
    Json(request): Json<DebugWithContextRequest>,
) -> Result<ResponseJson<ApiResponse<DebugResponse>>, StatusCode> {
    if request.test_logs.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    let analyzer = DebugAnalyzer::new();

    // Create debug context integrator (optional - would need subsystem references in production)
    // For now, pass None as we don't have direct access to RealityEngine, etc. here
    // In production, these would be injected via State
    let _integrator: Option<&DebugContextIntegrator> = None;

    let debug_request = DebugRequest {
        test_logs: request.test_logs,
        test_name: request.test_name,
        workspace_id: request.workspace_id,
    };

    match analyzer.analyze(&debug_request).await {
        Ok(response) => Ok(ResponseJson(ApiResponse::success(response))),
        Err(e) => Ok(ResponseJson(ApiResponse::error(format!(
            "Failed to analyze test failure with context: {}",
            e
        )))),
    }
}

/// Request for persona generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneratePersonaRequest {
    /// Natural language description
    pub description: String,

    /// Optional base persona ID to tweak
    pub base_persona_id: Option<String>,

    /// Workspace ID
    pub workspace_id: Option<String>,
}

/// Generate or tweak a persona
///
/// POST /api/v1/ai-studio/generate-persona
pub async fn generate_persona(
    State(state): State<AdminState>,
    Json(request): Json<GeneratePersonaRequest>,
) -> Result<ResponseJson<ApiResponse<PersonaGenerationResponse>>, StatusCode> {
    if request.description.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    let generator = PersonaGenerator::new();

    let persona_request = PersonaGenerationRequest {
        description: request.description.clone(),
        base_persona_id: request.base_persona_id,
        workspace_id: request.workspace_id.clone(),
    };

    // Get workspace config to check ai_mode and deterministic config
    let ai_mode = if let Some(workspace_id) = &request.workspace_id {
        if let Ok(workspace) = state.workspace_persistence.load_workspace(workspace_id).await {
            workspace.config.ai_mode
        } else {
            None
        }
    } else {
        None
    };

    let deterministic_config = if let Some(workspace_id) = &request.workspace_id {
        if let Ok(_workspace) = state.workspace_persistence.load_workspace(workspace_id).await {
            Some(DeterministicModeConfig::default())
        } else {
            None
        }
    } else {
        None
    };

    match generator
        .generate(&persona_request, ai_mode, deterministic_config.as_ref())
        .await
    {
        Ok(response) => Ok(ResponseJson(ApiResponse::success(response))),
        Err(e) => {
            Ok(ResponseJson(ApiResponse::error(format!("Failed to generate persona: {}", e))))
        }
    }
}

/// Request for artifact freezing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FreezeArtifactRequest {
    /// Type of artifact
    pub artifact_type: String,

    /// Artifact content
    pub content: Value,

    /// Output format (yaml or json)
    pub format: String,

    /// Output path
    pub path: Option<String>,
}

/// Request for artifact freezing with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FreezeArtifactRequestWithMetadata {
    /// Type of artifact
    pub artifact_type: String,

    /// Artifact content
    pub content: Value,

    /// Output format (yaml or json)
    pub format: String,

    /// Output path
    pub path: Option<String>,

    /// Optional metadata
    pub metadata: Option<FreezeMetadataPayload>,
}

/// Metadata payload for freezing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FreezeMetadataPayload {
    /// LLM provider used
    pub llm_provider: Option<String>,

    /// LLM model used
    pub llm_model: Option<String>,

    /// LLM version
    pub llm_version: Option<String>,

    /// Hash of the input prompt
    pub prompt_hash: Option<String>,

    /// Original prompt/description
    pub original_prompt: Option<String>,
}

/// Freeze an AI-generated artifact to deterministic format
///
/// POST /api/v1/ai-studio/freeze
pub async fn freeze_artifact(
    Json(request): Json<FreezeArtifactRequestWithMetadata>,
) -> Result<ResponseJson<ApiResponse<FrozenArtifact>>, StatusCode> {
    let freezer = ArtifactFreezer::new();

    let metadata = request.metadata.map(|m| {
        use mockforge_core::ai_studio::FreezeMetadata;
        FreezeMetadata {
            llm_provider: m.llm_provider,
            llm_model: m.llm_model,
            llm_version: m.llm_version,
            prompt_hash: m.prompt_hash,
            output_hash: None, // Will be calculated by freezer
            original_prompt: m.original_prompt,
        }
    });

    let freeze_request = FreezeRequest {
        artifact_type: request.artifact_type,
        content: request.content,
        format: request.format,
        path: request.path,
        metadata,
    };

    match freezer.freeze(&freeze_request).await {
        Ok(artifact) => Ok(ResponseJson(ApiResponse::success(artifact))),
        Err(e) => Ok(ResponseJson(ApiResponse::error(format!("Failed to freeze artifact: {}", e)))),
    }
}

/// Query parameters for listing frozen artifacts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListFrozenQuery {
    /// Filter by artifact type
    pub artifact_type: Option<String>,

    /// Workspace ID
    pub workspace_id: Option<String>,
}

/// List frozen artifacts
///
/// GET /api/v1/ai-studio/frozen
pub async fn list_frozen(
    Query(params): Query<ListFrozenQuery>,
) -> Result<ResponseJson<ApiResponse<Vec<FrozenArtifact>>>, StatusCode> {
    let freezer = ArtifactFreezer::new();
    let base_dir = freezer.base_dir().to_path_buf();

    // Read all files from the freeze directory
    let mut artifacts = Vec::new();

    if let Ok(mut entries) = tokio::fs::read_dir(&base_dir).await {
        while let Ok(Some(entry)) = entries.next_entry().await {
            let path = entry.path();
            if path.is_file() {
                // Check if file matches artifact_type filter
                let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
                if let Some(ref artifact_type) = params.artifact_type {
                    if !file_name.starts_with(&format!("{}_", artifact_type)) {
                        continue;
                    }
                }

                // Try to load the frozen artifact
                let content = match tokio::fs::read_to_string(&path).await {
                    Ok(c) => c,
                    Err(_) => continue,
                };

                let content_value: Value = if path.extension().and_then(|e| e.to_str())
                    == Some("yaml")
                    || path.extension().and_then(|e| e.to_str()) == Some("yml")
                {
                    match serde_yaml::from_str(&content) {
                        Ok(v) => v,
                        Err(_) => continue,
                    }
                } else {
                    match serde_json::from_str(&content) {
                        Ok(v) => v,
                        Err(_) => continue,
                    }
                };

                // Extract metadata from content
                let metadata = content_value
                    .get("_frozen_metadata")
                    .and_then(|m| serde_json::from_value(m.clone()).ok());

                // Extract output_hash from metadata
                let output_hash = content_value
                    .get("_frozen_metadata")
                    .and_then(|m| m.get("output_hash"))
                    .and_then(|h| h.as_str())
                    .map(|s| s.to_string());

                // Determine artifact type from filename or metadata
                let artifact_type = content_value
                    .get("_frozen_metadata")
                    .and_then(|m| m.get("artifact_type"))
                    .and_then(|t| t.as_str())
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| {
                        // Fallback: extract from filename
                        file_name.split('_').next().unwrap_or("unknown").to_string()
                    });

                artifacts.push(FrozenArtifact {
                    artifact_type,
                    content: content_value,
                    format: if path.extension().and_then(|e| e.to_str()) == Some("yaml")
                        || path.extension().and_then(|e| e.to_str()) == Some("yml")
                    {
                        "yaml".to_string()
                    } else {
                        "json".to_string()
                    },
                    path: path.to_string_lossy().to_string(),
                    metadata,
                    output_hash,
                });
            }
        }
    }

    // Sort by path (most recent first if timestamps are in filename)
    artifacts.sort_by(|a, b| b.path.cmp(&a.path));

    Ok(ResponseJson(ApiResponse::success(artifacts)))
}

/// Get usage statistics
///
/// GET /api/v1/ai-studio/usage
pub async fn get_usage(
    Query(params): Query<HashMap<String, String>>,
) -> Result<ResponseJson<ApiResponse<UsageStats>>, StatusCode> {
    let workspace_id = params.get("workspace_id").cloned().unwrap_or_default();

    let budget_config = BudgetConfig::default();
    let budget_manager = BudgetManager::new(budget_config);

    match budget_manager.get_usage(&workspace_id).await {
        Ok(stats) => Ok(ResponseJson(ApiResponse::success(stats))),
        Err(e) => Ok(ResponseJson(ApiResponse::error(format!("Failed to get usage stats: {}", e)))),
    }
}

/// Request for applying a debug patch
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplyPatchRequest {
    /// JSON Patch operation
    pub patch: Value,

    /// Configuration file path to apply patch to
    pub config_path: Option<String>,
}

/// Apply a debug patch to configuration
///
/// POST /api/v1/ai-studio/apply-patch
pub async fn apply_patch(
    State(_state): State<AdminState>,
    Json(request): Json<ApplyPatchRequest>,
) -> Result<ResponseJson<ApiResponse<Value>>, StatusCode> {
    // Determine config file path
    let config_path = request.config_path.unwrap_or_else(|| "mockforge.yaml".to_string());

    // Load the config file
    let config_content = match tokio::fs::read_to_string(&config_path).await {
        Ok(content) => content,
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to read config file {}: {}",
                config_path, e
            ))));
        }
    };

    // Parse config as JSON (works for YAML too via serde_yaml)
    let mut config_value: Value = if config_path.ends_with(".yaml") || config_path.ends_with(".yml")
    {
        serde_yaml::from_str(&config_content).map_err(|_e| StatusCode::BAD_REQUEST)?
    } else {
        serde_json::from_str(&config_content).map_err(|_e| StatusCode::BAD_REQUEST)?
    };

    // Parse patch operations
    let patch_ops: Patch =
        if let Some(ops_array) = request.patch.get("operations").and_then(|v| v.as_array()) {
            // Multiple operations
            Patch(ops_array.iter().filter_map(|op| parse_patch_operation(op).ok()).collect())
        } else {
            // Single operation
            Patch(vec![parse_patch_operation(&request.patch)?])
        };

    // Apply patch
    patch(&mut config_value, &patch_ops).map_err(|_e| StatusCode::BAD_REQUEST)?;

    // Save updated config
    let updated_content = if config_path.ends_with(".yaml") || config_path.ends_with(".yml") {
        serde_yaml::to_string(&config_value).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
    } else {
        serde_json::to_string_pretty(&config_value)
            .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
    };

    tokio::fs::write(&config_path, updated_content)
        .await
        .map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?;

    Ok(ResponseJson(ApiResponse::success(serde_json::json!({
        "message": "Patch applied successfully",
        "config_path": config_path,
        "updated_config": config_value
    }))))
}

/// Parse a single patch operation from JSON
fn parse_patch_operation(op: &Value) -> Result<json_patch::PatchOperation, StatusCode> {
    use json_patch::{AddOperation, PatchOperation, RemoveOperation, ReplaceOperation};

    let op_type = op.get("op").and_then(|v| v.as_str()).ok_or(StatusCode::BAD_REQUEST)?;

    let path_str = op.get("path").and_then(|v| v.as_str()).ok_or(StatusCode::BAD_REQUEST)?;

    let path: PointerBuf = path_str.parse().map_err(|_| StatusCode::BAD_REQUEST)?;

    match op_type {
        "add" => {
            let value = op.get("value").ok_or(StatusCode::BAD_REQUEST)?;
            Ok(PatchOperation::Add(AddOperation {
                path,
                value: value.clone(),
            }))
        }
        "remove" => Ok(PatchOperation::Remove(RemoveOperation { path })),
        "replace" => {
            let value = op.get("value").ok_or(StatusCode::BAD_REQUEST)?;
            Ok(PatchOperation::Replace(ReplaceOperation {
                path,
                value: value.clone(),
            }))
        }
        "copy" => {
            let from = op.get("from").and_then(|v| v.as_str()).ok_or(StatusCode::BAD_REQUEST)?;
            let from_path: PointerBuf = from.parse().map_err(|_| StatusCode::BAD_REQUEST)?;
            Ok(PatchOperation::Copy(json_patch::CopyOperation {
                path,
                from: from_path,
            }))
        }
        "move" => {
            let from = op.get("from").and_then(|v| v.as_str()).ok_or(StatusCode::BAD_REQUEST)?;
            let from_path: PointerBuf = from.parse().map_err(|_| StatusCode::BAD_REQUEST)?;
            Ok(PatchOperation::Move(json_patch::MoveOperation {
                path,
                from: from_path,
            }))
        }
        "test" => {
            let value = op.get("value").ok_or(StatusCode::BAD_REQUEST)?;
            Ok(PatchOperation::Test(json_patch::TestOperation {
                path,
                value: value.clone(),
            }))
        }
        _ => Err(StatusCode::BAD_REQUEST),
    }
}

/// Get organization-level AI controls
///
/// GET /api/v1/ai-studio/org-controls
pub async fn get_org_controls(
    Query(params): Query<HashMap<String, String>>,
) -> Result<ResponseJson<ApiResponse<OrgAiControlsConfig>>, StatusCode> {
    let org_id = params.get("org_id").cloned();
    let workspace_id = params.get("workspace_id").cloned();

    // Create org controls service with default YAML config
    // In production, this would be injected via State
    let org_controls = OrgControls::new(OrgAiControlsConfig::default());

    match org_controls
        .load_org_config(org_id.as_deref().unwrap_or("default"), workspace_id.as_deref())
        .await
    {
        Ok(controls) => Ok(ResponseJson(ApiResponse::success(controls))),
        Err(e) => {
            Ok(ResponseJson(ApiResponse::error(format!("Failed to get org controls: {}", e))))
        }
    }
}

/// Update organization-level AI controls
///
/// PUT /api/v1/ai-studio/org-controls
pub async fn update_org_controls(
    Query(params): Query<HashMap<String, String>>,
    Json(controls): Json<OrgAiControlsConfig>,
) -> Result<ResponseJson<ApiResponse<OrgAiControlsConfig>>, StatusCode> {
    let _org_id = params.get("org_id").cloned();
    let _workspace_id = params.get("workspace_id").cloned();

    // Note: Database persistence requires OrgControlsAccessor to be available in State
    // To enable database persistence:
    // 1. Add OrgControlsAccessor (e.g., DbOrgControls) to AdminState
    // 2. Call accessor.save_controls(org_id, workspace_id, &controls).await
    // 3. Return updated controls from database
    // For now, controls are returned as-is (in-memory only)

    Ok(ResponseJson(ApiResponse::success(controls)))
}

/// Get organization-level AI usage statistics
///
/// GET /api/v1/ai-studio/org-controls/usage
pub async fn get_org_usage(
    Query(params): Query<HashMap<String, String>>,
) -> Result<ResponseJson<ApiResponse<Value>>, StatusCode> {
    let org_id = params.get("org_id").cloned();
    let workspace_id = params.get("workspace_id").cloned();

    // Get period filter (default to current month)
    let period_start = params
        .get("period_start")
        .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
        .map(|dt| dt.with_timezone(&chrono::Utc))
        .unwrap_or_else(|| {
            // Default to start of current month
            let now = chrono::Utc::now();
            {
                use chrono::{Datelike, TimeZone};
                chrono::NaiveDate::from_ymd_opt(now.year(), now.month(), 1)
                    .and_then(|d| d.and_hms_opt(0, 0, 0))
                    .map(|dt| chrono::Utc.from_utc_datetime(&dt))
                    .unwrap()
            }
        });

    let period_end = params
        .get("period_end")
        .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
        .map(|dt| dt.with_timezone(&chrono::Utc))
        .unwrap_or_else(chrono::Utc::now);

    // Note: Database querying requires a database connection pool
    // In production with database access, you would:
    // 1. Get database pool from State
    // 2. Query org_ai_usage_logs table:
    //    SELECT
    //      SUM(tokens_used) as total_tokens,
    //      SUM(cost_usd) as total_cost,
    //      COUNT(*) as total_calls,
    //      feature_name,
    //      COUNT(DISTINCT user_id) as unique_users
    //    FROM org_ai_usage_logs
    //    WHERE org_id = $1
    //      AND (workspace_id = $2 OR $2 IS NULL)
    //      AND created_at >= $3
    //      AND created_at <= $4
    //    GROUP BY feature_name

    // For now, return structure that matches what the query would return
    Ok(ResponseJson(ApiResponse::success(serde_json::json!({
        "org_id": org_id,
        "workspace_id": workspace_id,
        "period_start": period_start.to_rfc3339(),
        "period_end": period_end.to_rfc3339(),
        "total_tokens": 0,
        "total_cost": 0.0,
        "total_calls": 0,
        "feature_breakdown": {},
        "message": "Usage stats require database connection. Connect to registry server database to enable."
    }))))
}

/// Request for contract diff query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractDiffQueryRequest {
    /// Natural language query
    pub query: String,

    /// Optional workspace ID
    pub workspace_id: Option<String>,

    /// Optional organization ID
    pub org_id: Option<String>,
}

/// Process a natural language query about contract diffs
///
/// POST /api/v1/ai-studio/contract-diff/query
pub async fn contract_diff_query(
    Json(request): Json<ContractDiffQueryRequest>,
) -> Result<ResponseJson<ApiResponse<ContractDiffQueryResult>>, StatusCode> {
    if request.query.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    let handler = ContractDiffHandler::new().map_err(|e| {
        tracing::error!("Failed to create ContractDiffHandler: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // For now, we don't have direct access to specs/requests in the handler
    // In production, these would be loaded from workspace/request storage
    match handler.analyze_from_query(&request.query, None, None).await {
        Ok(result) => Ok(ResponseJson(ApiResponse::success(result))),
        Err(e) => Ok(ResponseJson(ApiResponse::error(format!(
            "Failed to process contract diff query: {}",
            e
        )))),
    }
}