sevensense-api 0.1.0

REST, GraphQL, and WebSocket API server for 7sense bioacoustics platform
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
//! REST API handlers for 7sense endpoints.
//!
//! Each handler follows the pattern:
//! 1. Extract and validate request parameters
//! 2. Call the appropriate service layer
//! 3. Transform results to API response types
//! 4. Handle errors with proper status codes

use axum::{
    extract::{Multipart, Path, Query, State},
    http::StatusCode,
    Json,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use utoipa::{IntoParams, ToSchema};
use uuid::Uuid;

use crate::{
    error::{ApiError, ApiResult},
    services::SpeciesInfo,
    AppContext, ProcessingEvent, ProcessingStatus,
};

// ============================================================================
// Request/Response Types
// ============================================================================

/// Recording metadata and processing status.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct Recording {
    /// Unique recording identifier
    #[schema(example = "550e8400-e29b-41d4-a716-446655440000")]
    pub id: Uuid,
    /// Original filename
    #[schema(example = "dawn_chorus_2024.wav")]
    pub filename: String,
    /// Recording duration in seconds
    #[schema(example = 120.5)]
    pub duration_secs: f64,
    /// Sample rate in Hz
    #[schema(example = 44100)]
    pub sample_rate: u32,
    /// Number of audio channels
    #[schema(example = 1)]
    pub channels: u16,
    /// Current processing status
    pub status: ProcessingStatus,
    /// Number of detected segments
    #[schema(example = 42)]
    pub segment_count: usize,
    /// Upload timestamp
    pub created_at: DateTime<Utc>,
    /// Last processing update
    pub updated_at: DateTime<Utc>,
}

/// Upload response after successful recording ingestion.
#[derive(Debug, Serialize, ToSchema)]
pub struct UploadResponse {
    /// The created recording
    pub recording: Recording,
    /// WebSocket URL for status updates
    #[schema(example = "/ws/recordings/550e8400-e29b-41d4-a716-446655440000")]
    pub status_url: String,
}

/// Query parameters for neighbor search.
#[derive(Debug, Deserialize, IntoParams, ToSchema)]
pub struct NeighborParams {
    /// Number of neighbors to return (default: 10)
    #[param(default = 10, minimum = 1, maximum = 100)]
    pub k: Option<usize>,
    /// Minimum similarity threshold (0.0 to 1.0)
    #[param(default = 0.0, minimum = 0.0, maximum = 1.0)]
    pub min_similarity: Option<f32>,
    /// Filter by species (optional)
    pub species: Option<String>,
    /// Include segment audio URLs
    #[param(default = false)]
    pub include_audio: Option<bool>,
}

/// A similar segment (neighbor) in the embedding space.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct Neighbor {
    /// Segment identifier
    pub segment_id: Uuid,
    /// Parent recording ID
    pub recording_id: Uuid,
    /// Similarity score (0.0 to 1.0)
    #[schema(example = 0.95)]
    pub similarity: f32,
    /// Distance in embedding space
    #[schema(example = 0.123)]
    pub distance: f32,
    /// Segment start time in seconds
    #[schema(example = 12.5)]
    pub start_time: f64,
    /// Segment end time in seconds
    #[schema(example = 14.2)]
    pub end_time: f64,
    /// Detected species (if any)
    pub species: Option<SpeciesInfo>,
    /// URL to audio clip (if requested)
    pub audio_url: Option<String>,
}

/// A discovered cluster of similar calls.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct Cluster {
    /// Cluster identifier
    pub id: Uuid,
    /// Human-assigned label (if any)
    pub label: Option<String>,
    /// Number of segments in cluster
    #[schema(example = 156)]
    pub size: usize,
    /// Cluster centroid (mean embedding)
    pub centroid: Vec<f32>,
    /// Cluster density/compactness score
    #[schema(example = 0.87)]
    pub density: f32,
    /// Representative segment IDs
    pub exemplar_ids: Vec<Uuid>,
    /// Detected species distribution
    pub species_distribution: Vec<SpeciesCount>,
    /// Cluster creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Species count within a cluster.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct SpeciesCount {
    /// Species common name
    #[schema(example = "American Robin")]
    pub name: String,
    /// Scientific name
    #[schema(example = "Turdus migratorius")]
    pub scientific_name: Option<String>,
    /// Count of segments with this species
    #[schema(example = 42)]
    pub count: usize,
    /// Percentage of cluster
    #[schema(example = 27.3)]
    pub percentage: f64,
}

/// Evidence pack for interpretability.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct EvidencePack {
    /// Query identifier
    pub query_id: Uuid,
    /// Segment that was queried
    pub query_segment: SegmentSummary,
    /// Retrieved neighbors with evidence
    pub neighbors: Vec<NeighborEvidence>,
    /// Shared acoustic features
    pub shared_features: Vec<AcousticFeature>,
    /// Visualization data
    pub visualizations: EvidenceVisualizations,
    /// Generation timestamp
    pub generated_at: DateTime<Utc>,
}

/// Summary of a segment for evidence packs.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct SegmentSummary {
    /// Segment ID
    pub id: Uuid,
    /// Recording ID
    pub recording_id: Uuid,
    /// Start time
    pub start_time: f64,
    /// End time
    pub end_time: f64,
    /// Detected species
    pub species: Option<SpeciesInfo>,
}

/// Evidence for a neighbor relationship.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct NeighborEvidence {
    /// The neighbor segment
    pub segment: SegmentSummary,
    /// Similarity score
    pub similarity: f32,
    /// Contributing feature dimensions
    pub contributing_features: Vec<FeatureContribution>,
    /// Spectrogram comparison URL
    pub spectrogram_comparison_url: Option<String>,
}

/// Feature contribution to similarity.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct FeatureContribution {
    /// Feature name
    #[schema(example = "fundamental_frequency")]
    pub name: String,
    /// Contribution weight
    #[schema(example = 0.23)]
    pub weight: f32,
    /// Query value
    pub query_value: f64,
    /// Neighbor value
    pub neighbor_value: f64,
}

/// Acoustic feature shared between segments.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct AcousticFeature {
    /// Feature name
    #[schema(example = "frequency_modulation")]
    pub name: String,
    /// Feature description
    #[schema(example = "Rapid upward sweep in 200-400ms")]
    pub description: String,
    /// Confidence score
    #[schema(example = 0.92)]
    pub confidence: f32,
}

/// Visualization URLs for evidence pack.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub struct EvidenceVisualizations {
    /// UMAP projection of embedding space
    pub umap_url: Option<String>,
    /// Spectrogram grid URL
    pub spectrogram_grid_url: Option<String>,
    /// Feature importance chart URL
    pub feature_importance_url: Option<String>,
}

/// Search query for semantic search.
#[derive(Debug, Deserialize, ToSchema)]
pub struct SearchQuery {
    /// Text description to search for
    #[schema(example = "ascending whistle followed by trill")]
    pub query: Option<String>,
    /// Segment ID to find similar segments
    pub segment_id: Option<Uuid>,
    /// Embedding vector for direct search
    pub embedding: Option<Vec<f32>>,
    /// Number of results (default: 20)
    #[schema(default = 20, minimum = 1, maximum = 200)]
    pub limit: Option<usize>,
    /// Species filter
    pub species_filter: Option<Vec<String>>,
    /// Time range filter (start)
    pub time_start: Option<DateTime<Utc>>,
    /// Time range filter (end)
    pub time_end: Option<DateTime<Utc>>,
}

/// Search results response.
#[derive(Debug, Serialize, ToSchema)]
pub struct SearchResults {
    /// Search query echo
    pub query: SearchQueryEcho,
    /// Matching segments
    pub results: Vec<SearchResult>,
    /// Total count (may be estimated)
    pub total_count: usize,
    /// Search latency in milliseconds
    pub latency_ms: u64,
}

/// Echo of the search query.
#[derive(Debug, Serialize, ToSchema)]
pub struct SearchQueryEcho {
    /// Text query
    pub text: Option<String>,
    /// Segment ID query
    pub segment_id: Option<Uuid>,
    /// Result limit
    pub limit: usize,
}

/// Individual search result.
#[derive(Debug, Serialize, ToSchema)]
pub struct SearchResult {
    /// Segment information
    pub segment: SegmentSummary,
    /// Search score
    #[schema(example = 0.87)]
    pub score: f32,
    /// Highlight/explanation
    pub highlight: Option<String>,
}

/// Cluster label assignment request.
#[derive(Debug, Deserialize, ToSchema)]
pub struct AssignLabelRequest {
    /// Label to assign
    #[schema(example = "Northern Cardinal song type A")]
    pub label: String,
}

/// Request to generate evidence pack.
#[derive(Debug, Deserialize, ToSchema)]
pub struct GenerateEvidenceRequest {
    /// Segment ID to analyze
    pub segment_id: Uuid,
    /// Number of neighbors to include
    #[schema(default = 10)]
    pub k: Option<usize>,
    /// Include spectrogram comparisons
    #[schema(default = true)]
    pub include_spectrograms: Option<bool>,
}

// ============================================================================
// Handlers
// ============================================================================

/// Upload and process an audio recording.
///
/// Accepts multipart form data with an audio file. Supported formats:
/// - WAV (recommended)
/// - FLAC
/// - MP3
/// - OGG
///
/// Processing is asynchronous. Subscribe to the WebSocket URL for status updates.
#[utoipa::path(
    post,
    path = "/recordings",
    request_body(content = Vec<u8>, content_type = "multipart/form-data"),
    responses(
        (status = 201, description = "Recording uploaded and processing started", body = UploadResponse),
        (status = 400, description = "Invalid audio file", body = crate::error::ErrorResponse),
        (status = 413, description = "File too large", body = crate::error::ErrorResponse),
        (status = 415, description = "Unsupported audio format", body = crate::error::ErrorResponse),
    ),
    tag = "recordings"
)]
pub async fn upload_recording(
    State(ctx): State<AppContext>,
    mut multipart: Multipart,
) -> ApiResult<(StatusCode, Json<UploadResponse>)> {
    // Extract file from multipart
    let mut audio_data: Option<Vec<u8>> = None;
    let mut filename: Option<String> = None;
    let mut content_type: Option<String> = None;

    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| ApiError::BadRequest(format!("Invalid multipart data: {e}")))?
    {
        let field_name = field.name().unwrap_or_default().to_string();

        if field_name == "file" || field_name == "audio" {
            filename = field.file_name().map(String::from);
            content_type = field.content_type().map(String::from);

            let data = field
                .bytes()
                .await
                .map_err(|e| ApiError::BadRequest(format!("Failed to read file: {e}")))?;

            // Check file size
            if data.len() > ctx.config.max_upload_size {
                return Err(ApiError::PayloadTooLarge(format!(
                    "File size {} exceeds maximum {}",
                    data.len(),
                    ctx.config.max_upload_size
                )));
            }

            audio_data = Some(data.to_vec());
        }
    }

    let audio_data =
        audio_data.ok_or_else(|| ApiError::BadRequest("No audio file provided".into()))?;
    let filename = filename.unwrap_or_else(|| "unknown.wav".to_string());

    // Validate content type
    let valid_types = ["audio/wav", "audio/x-wav", "audio/flac", "audio/mpeg", "audio/ogg"];
    if let Some(ref ct) = content_type {
        if !valid_types.iter().any(|t| ct.contains(t)) {
            tracing::warn!(content_type = %ct, "Unknown content type, proceeding anyway");
        }
    }

    // Create recording record
    let recording_id = Uuid::new_v4();
    let now = Utc::now();

    // Publish initial event
    ctx.publish_event(ProcessingEvent {
        recording_id,
        status: ProcessingStatus::Queued,
        progress: 0.0,
        message: Some("Recording queued for processing".into()),
    });

    // Start async processing
    let ctx_clone = ctx.clone();
    let audio_data_clone = audio_data.clone();
    tokio::spawn(async move {
        process_recording(ctx_clone, recording_id, audio_data_clone).await;
    });

    // Get audio metadata (quick parse)
    let (duration, sample_rate, channels) = ctx.audio_pipeline.get_metadata(&audio_data)?;

    let recording = Recording {
        id: recording_id,
        filename,
        duration_secs: duration,
        sample_rate,
        channels,
        status: ProcessingStatus::Queued,
        segment_count: 0,
        created_at: now,
        updated_at: now,
    };

    let response = UploadResponse {
        recording: recording.clone(),
        status_url: format!("/ws/recordings/{recording_id}"),
    };

    Ok((StatusCode::CREATED, Json(response)))
}

/// Background task to process a recording.
async fn process_recording(ctx: AppContext, recording_id: Uuid, audio_data: Vec<u8>) {
    // Loading
    ctx.publish_event(ProcessingEvent {
        recording_id,
        status: ProcessingStatus::Loading,
        progress: 0.1,
        message: Some("Loading audio file".into()),
    });

    // Parse audio
    let audio = match ctx.audio_pipeline.load_audio(&audio_data) {
        Ok(a) => a,
        Err(e) => {
            ctx.publish_event(ProcessingEvent {
                recording_id,
                status: ProcessingStatus::Failed,
                progress: 0.0,
                message: Some(format!("Failed to load audio: {e}")),
            });
            return;
        }
    };

    // Segmenting
    ctx.publish_event(ProcessingEvent {
        recording_id,
        status: ProcessingStatus::Segmenting,
        progress: 0.3,
        message: Some("Detecting call segments".into()),
    });

    let segments = match ctx.audio_pipeline.segment(&audio) {
        Ok(s) => s,
        Err(e) => {
            ctx.publish_event(ProcessingEvent {
                recording_id,
                status: ProcessingStatus::Failed,
                progress: 0.0,
                message: Some(format!("Segmentation failed: {e}")),
            });
            return;
        }
    };

    // Embedding
    ctx.publish_event(ProcessingEvent {
        recording_id,
        status: ProcessingStatus::Embedding,
        progress: 0.5,
        message: Some(format!("Generating embeddings for {} segments", segments.len())),
    });

    let embeddings = match ctx.embedding_model.embed_batch(&segments).await {
        Ok(e) => e,
        Err(e) => {
            ctx.publish_event(ProcessingEvent {
                recording_id,
                status: ProcessingStatus::Failed,
                progress: 0.0,
                message: Some(format!("Embedding failed: {e}")),
            });
            return;
        }
    };

    // Indexing
    ctx.publish_event(ProcessingEvent {
        recording_id,
        status: ProcessingStatus::Indexing,
        progress: 0.7,
        message: Some("Adding to vector index".into()),
    });

    if let Err(e) = ctx.vector_index.add_batch(&embeddings) {
        ctx.publish_event(ProcessingEvent {
            recording_id,
            status: ProcessingStatus::Failed,
            progress: 0.0,
            message: Some(format!("Indexing failed: {e}")),
        });
        return;
    }

    // Analyzing
    ctx.publish_event(ProcessingEvent {
        recording_id,
        status: ProcessingStatus::Analyzing,
        progress: 0.9,
        message: Some("Running cluster analysis".into()),
    });

    if let Err(e) = ctx.cluster_engine.update_clusters(&embeddings) {
        tracing::warn!(error = %e, "Cluster update failed, continuing");
    }

    // Complete
    ctx.publish_event(ProcessingEvent {
        recording_id,
        status: ProcessingStatus::Complete,
        progress: 1.0,
        message: Some(format!(
            "Processing complete: {} segments indexed",
            segments.len()
        )),
    });
}

/// Get a recording by ID.
#[utoipa::path(
    get,
    path = "/recordings/{id}",
    params(
        ("id" = Uuid, Path, description = "Recording ID")
    ),
    responses(
        (status = 200, description = "Recording found", body = Recording),
        (status = 404, description = "Recording not found", body = crate::error::ErrorResponse),
    ),
    tag = "recordings"
)]
pub async fn get_recording(
    State(_ctx): State<AppContext>,
    Path(id): Path<Uuid>,
) -> ApiResult<Json<Recording>> {
    // In production, this would query a database
    // For now, return a mock or error
    Err(ApiError::not_found("Recording", id))
}

/// Find similar segments (neighbors) for a given segment.
#[utoipa::path(
    get,
    path = "/segments/{id}/neighbors",
    params(
        ("id" = Uuid, Path, description = "Segment ID"),
        NeighborParams
    ),
    responses(
        (status = 200, description = "Neighbors found", body = Vec<Neighbor>),
        (status = 404, description = "Segment not found", body = crate::error::ErrorResponse),
    ),
    tag = "segments"
)]
pub async fn get_neighbors(
    State(ctx): State<AppContext>,
    Path(segment_id): Path<Uuid>,
    Query(params): Query<NeighborParams>,
) -> ApiResult<Json<Vec<Neighbor>>> {
    let k = params.k.unwrap_or(10).min(100);
    let min_similarity = params.min_similarity.unwrap_or(0.0);

    // Get segment embedding
    let embedding = ctx
        .vector_index
        .get_embedding(&segment_id)?
        .ok_or_else(|| ApiError::not_found("Segment", segment_id))?;

    // Search for neighbors
    let results = ctx.vector_index.search(&embedding, k, min_similarity)?;

    // Convert to response format
    let neighbors: Vec<Neighbor> = results
        .into_iter()
        .filter(|r| r.id != segment_id) // Exclude query segment
        .map(|r| Neighbor {
            segment_id: r.id,
            recording_id: r.recording_id,
            similarity: 1.0 - r.distance, // Convert distance to similarity
            distance: r.distance,
            start_time: r.start_time,
            end_time: r.end_time,
            species: r.species,
            audio_url: if params.include_audio.unwrap_or(false) {
                Some(format!("/api/v1/segments/{}/audio", r.id))
            } else {
                None
            },
        })
        .collect();

    Ok(Json(neighbors))
}

/// List all discovered clusters.
#[utoipa::path(
    get,
    path = "/clusters",
    responses(
        (status = 200, description = "Clusters retrieved", body = Vec<Cluster>),
    ),
    tag = "clusters"
)]
pub async fn list_clusters(State(ctx): State<AppContext>) -> ApiResult<Json<Vec<Cluster>>> {
    let cluster_data = ctx.cluster_engine.get_all_clusters()?;

    let clusters: Vec<Cluster> = cluster_data
        .into_iter()
        .map(|c| Cluster {
            id: c.id,
            label: c.label,
            size: c.size,
            centroid: c.centroid,
            density: c.density,
            exemplar_ids: c.exemplar_ids,
            species_distribution: c
                .species_distribution
                .into_iter()
                .map(|(name, count, percentage)| SpeciesCount {
                    name: name.clone(),
                    scientific_name: None, // Would be looked up
                    count,
                    percentage,
                })
                .collect(),
            created_at: c.created_at,
        })
        .collect();

    Ok(Json(clusters))
}

/// Get a specific cluster by ID.
#[utoipa::path(
    get,
    path = "/clusters/{id}",
    params(
        ("id" = Uuid, Path, description = "Cluster ID")
    ),
    responses(
        (status = 200, description = "Cluster found", body = Cluster),
        (status = 404, description = "Cluster not found", body = crate::error::ErrorResponse),
    ),
    tag = "clusters"
)]
pub async fn get_cluster(
    State(ctx): State<AppContext>,
    Path(id): Path<Uuid>,
) -> ApiResult<Json<Cluster>> {
    let cluster_data = ctx
        .cluster_engine
        .get_cluster(&id)?
        .ok_or_else(|| ApiError::not_found("Cluster", id))?;

    let cluster = Cluster {
        id: cluster_data.id,
        label: cluster_data.label,
        size: cluster_data.size,
        centroid: cluster_data.centroid,
        density: cluster_data.density,
        exemplar_ids: cluster_data.exemplar_ids,
        species_distribution: cluster_data
            .species_distribution
            .into_iter()
            .map(|(name, count, percentage)| SpeciesCount {
                name,
                scientific_name: None,
                count,
                percentage,
            })
            .collect(),
        created_at: cluster_data.created_at,
    };

    Ok(Json(cluster))
}

/// Assign a label to a cluster.
#[utoipa::path(
    put,
    path = "/clusters/{id}/label",
    params(
        ("id" = Uuid, Path, description = "Cluster ID")
    ),
    request_body = AssignLabelRequest,
    responses(
        (status = 200, description = "Label assigned", body = Cluster),
        (status = 404, description = "Cluster not found", body = crate::error::ErrorResponse),
    ),
    tag = "clusters"
)]
pub async fn assign_cluster_label(
    State(ctx): State<AppContext>,
    Path(id): Path<Uuid>,
    Json(request): Json<AssignLabelRequest>,
) -> ApiResult<Json<Cluster>> {
    let cluster_data = ctx
        .cluster_engine
        .assign_label(&id, &request.label)?
        .ok_or_else(|| ApiError::not_found("Cluster", id))?;

    let cluster = Cluster {
        id: cluster_data.id,
        label: cluster_data.label,
        size: cluster_data.size,
        centroid: cluster_data.centroid,
        density: cluster_data.density,
        exemplar_ids: cluster_data.exemplar_ids,
        species_distribution: cluster_data
            .species_distribution
            .into_iter()
            .map(|(name, count, percentage)| SpeciesCount {
                name,
                scientific_name: None,
                count,
                percentage,
            })
            .collect(),
        created_at: cluster_data.created_at,
    };

    Ok(Json(cluster))
}

/// Get evidence pack for interpretability.
#[utoipa::path(
    get,
    path = "/evidence/{id}",
    params(
        ("id" = String, Path, description = "Evidence pack ID (query UUID)")
    ),
    responses(
        (status = 200, description = "Evidence pack retrieved", body = EvidencePack),
        (status = 404, description = "Evidence not found", body = crate::error::ErrorResponse),
    ),
    tag = "evidence"
)]
pub async fn get_evidence_pack(
    State(ctx): State<AppContext>,
    Path(id): Path<String>,
) -> ApiResult<Json<EvidencePack>> {
    let query_id = Uuid::parse_str(&id)
        .map_err(|_| ApiError::BadRequest(format!("Invalid UUID: {id}")))?;

    let evidence = ctx
        .interpretation_engine
        .get_evidence_pack(&query_id)?
        .ok_or_else(|| ApiError::not_found("Evidence pack", id))?;

    let pack = EvidencePack {
        query_id: evidence.query_id,
        query_segment: SegmentSummary {
            id: evidence.query_segment.id,
            recording_id: evidence.query_segment.recording_id,
            start_time: evidence.query_segment.start_time,
            end_time: evidence.query_segment.end_time,
            species: evidence.query_segment.species,
        },
        neighbors: evidence
            .neighbors
            .into_iter()
            .map(|n| NeighborEvidence {
                segment: SegmentSummary {
                    id: n.segment.id,
                    recording_id: n.segment.recording_id,
                    start_time: n.segment.start_time,
                    end_time: n.segment.end_time,
                    species: n.segment.species,
                },
                similarity: n.similarity,
                contributing_features: n
                    .contributing_features
                    .into_iter()
                    .map(|f| FeatureContribution {
                        name: f.name,
                        weight: f.weight,
                        query_value: f.query_value,
                        neighbor_value: f.neighbor_value,
                    })
                    .collect(),
                spectrogram_comparison_url: n.spectrogram_comparison_url,
            })
            .collect(),
        shared_features: evidence
            .shared_features
            .into_iter()
            .map(|f| AcousticFeature {
                name: f.name,
                description: f.description,
                confidence: f.confidence,
            })
            .collect(),
        visualizations: EvidenceVisualizations {
            umap_url: evidence.visualizations.umap_url,
            spectrogram_grid_url: evidence.visualizations.spectrogram_grid_url,
            feature_importance_url: evidence.visualizations.feature_importance_url,
        },
        generated_at: evidence.generated_at,
    };

    Ok(Json(pack))
}

/// Generate evidence pack for a segment query.
#[utoipa::path(
    post,
    path = "/evidence",
    request_body = GenerateEvidenceRequest,
    responses(
        (status = 201, description = "Evidence pack generated", body = EvidencePack),
        (status = 404, description = "Segment not found", body = crate::error::ErrorResponse),
    ),
    tag = "evidence"
)]
pub async fn generate_evidence_pack(
    State(ctx): State<AppContext>,
    Json(request): Json<GenerateEvidenceRequest>,
) -> ApiResult<(StatusCode, Json<EvidencePack>)> {
    // Get segment embedding
    let embedding = ctx
        .vector_index
        .get_embedding(&request.segment_id)?
        .ok_or_else(|| ApiError::not_found("Segment", request.segment_id))?;

    // Find neighbors
    let k = request.k.unwrap_or(10);
    let neighbors = ctx.vector_index.search(&embedding, k, 0.0)?;

    // Generate evidence pack
    let evidence = ctx
        .interpretation_engine
        .generate_evidence_pack(&request.segment_id, &neighbors)
        .await?;

    let pack = EvidencePack {
        query_id: evidence.query_id,
        query_segment: SegmentSummary {
            id: evidence.query_segment.id,
            recording_id: evidence.query_segment.recording_id,
            start_time: evidence.query_segment.start_time,
            end_time: evidence.query_segment.end_time,
            species: evidence.query_segment.species,
        },
        neighbors: evidence
            .neighbors
            .into_iter()
            .map(|n| NeighborEvidence {
                segment: SegmentSummary {
                    id: n.segment.id,
                    recording_id: n.segment.recording_id,
                    start_time: n.segment.start_time,
                    end_time: n.segment.end_time,
                    species: n.segment.species,
                },
                similarity: n.similarity,
                contributing_features: n
                    .contributing_features
                    .into_iter()
                    .map(|f| FeatureContribution {
                        name: f.name,
                        weight: f.weight,
                        query_value: f.query_value,
                        neighbor_value: f.neighbor_value,
                    })
                    .collect(),
                spectrogram_comparison_url: n.spectrogram_comparison_url,
            })
            .collect(),
        shared_features: evidence
            .shared_features
            .into_iter()
            .map(|f| AcousticFeature {
                name: f.name,
                description: f.description,
                confidence: f.confidence,
            })
            .collect(),
        visualizations: EvidenceVisualizations {
            umap_url: evidence.visualizations.umap_url,
            spectrogram_grid_url: evidence.visualizations.spectrogram_grid_url,
            feature_importance_url: evidence.visualizations.feature_importance_url,
        },
        generated_at: evidence.generated_at,
    };

    Ok((StatusCode::CREATED, Json(pack)))
}

/// Semantic search across segments.
#[utoipa::path(
    post,
    path = "/search",
    request_body = SearchQuery,
    responses(
        (status = 200, description = "Search results", body = SearchResults),
        (status = 400, description = "Invalid search query", body = crate::error::ErrorResponse),
    ),
    tag = "search"
)]
pub async fn search(
    State(ctx): State<AppContext>,
    Json(query): Json<SearchQuery>,
) -> ApiResult<Json<SearchResults>> {
    let start = std::time::Instant::now();

    // Validate query - need at least one search method
    if query.query.is_none() && query.segment_id.is_none() && query.embedding.is_none() {
        return Err(ApiError::BadRequest(
            "Must provide query text, segment_id, or embedding".into(),
        ));
    }

    let limit = query.limit.unwrap_or(20).min(200);

    // Get search embedding
    let search_embedding = if let Some(ref text) = query.query {
        // Text-to-embedding search
        ctx.embedding_model.embed_text(text).await?
    } else if let Some(segment_id) = query.segment_id {
        // Search by segment
        ctx.vector_index
            .get_embedding(&segment_id)?
            .ok_or_else(|| ApiError::not_found("Segment", segment_id))?
    } else if let Some(ref embedding) = query.embedding {
        // Direct embedding search
        embedding.clone()
    } else {
        unreachable!()
    };

    // Perform search
    let results = ctx.vector_index.search(&search_embedding, limit, 0.0)?;

    let latency_ms = start.elapsed().as_millis() as u64;

    let search_results = SearchResults {
        query: SearchQueryEcho {
            text: query.query,
            segment_id: query.segment_id,
            limit,
        },
        results: results
            .into_iter()
            .map(|r| SearchResult {
                segment: SegmentSummary {
                    id: r.id,
                    recording_id: r.recording_id,
                    start_time: r.start_time,
                    end_time: r.end_time,
                    species: r.species,
                },
                score: 1.0 - r.distance,
                highlight: None,
            })
            .collect(),
        total_count: limit, // Would be actual count from index
        latency_ms,
    };

    Ok(Json(search_results))
}

/// Health check endpoint.
#[utoipa::path(
    get,
    path = "/health",
    responses(
        (status = 200, description = "Service healthy", body = crate::HealthResponse),
    ),
    tag = "system"
)]
pub async fn health_check() -> Json<crate::HealthResponse> {
    Json(crate::HealthResponse {
        status: "healthy".into(),
        version: env!("CARGO_PKG_VERSION").into(),
        uptime_secs: 0, // Would track actual uptime
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_neighbor_params_defaults() {
        let params: NeighborParams = serde_json::from_str("{}").unwrap();
        assert!(params.k.is_none());
        assert!(params.min_similarity.is_none());
    }

    #[test]
    fn test_search_query_validation() {
        let query = SearchQuery {
            query: None,
            segment_id: None,
            embedding: None,
            limit: None,
            species_filter: None,
            time_start: None,
            time_end: None,
        };
        // This should fail validation in the handler
        assert!(query.query.is_none() && query.segment_id.is_none() && query.embedding.is_none());
    }
}