ceres-core 0.4.0

Core types, harvesting logic, and services for Ceres
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
//! Test utilities and mock implementations for integration tests.
//!
//! Provides mock implementations of the core traits for testing
//! `HarvestService` and `SearchService` in isolation.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use ceres_core::job::{CreateJobRequest, HarvestJob, JobStatus};
use ceres_core::job_queue::JobQueue;
use ceres_core::models::NewDataset;
use ceres_core::traits::{DatasetStore, EmbeddingProvider, PortalClient, PortalClientFactory};
use ceres_core::{AppError, Dataset, SearchResult, SyncStats};
use chrono::{DateTime, Utc};
use futures::stream::BoxStream;
use uuid::Uuid;

// =============================================================================
// MockEmbeddingProvider
// =============================================================================

/// Mock embedding provider that returns deterministic vectors.
///
/// Returns vectors based on the input text length, making embeddings
/// reproducible for testing. Dimension is configurable for testing
/// different provider configurations.
#[derive(Clone)]
pub struct MockEmbeddingProvider {
    dimension: usize,
}

impl MockEmbeddingProvider {
    /// Creates a new mock provider with 768 dimensions (Gemini default).
    pub fn new() -> Self {
        Self { dimension: 768 }
    }

    /// Creates a mock provider with a custom dimension.
    ///
    /// # Examples
    ///
    /// ```
    /// // Simulate OpenAI text-embedding-3-small (1536 dimensions)
    /// let provider = MockEmbeddingProvider::with_dimension(1536);
    /// ```
    #[allow(dead_code)]
    pub fn with_dimension(dimension: usize) -> Self {
        Self { dimension }
    }
}

impl Default for MockEmbeddingProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl EmbeddingProvider for MockEmbeddingProvider {
    fn name(&self) -> &'static str {
        "mock"
    }

    fn dimension(&self) -> usize {
        self.dimension
    }

    fn max_batch_size(&self) -> usize {
        100
    }

    async fn generate(&self, text: &str) -> Result<Vec<f32>, AppError> {
        // Generate a deterministic embedding based on text length
        let seed = text.len() as f32;
        let embedding: Vec<f32> = (0..self.dimension)
            .map(|i| (seed + i as f32) / 1000.0)
            .collect();
        Ok(embedding)
    }
}

// =============================================================================
// MockPortalClient
// =============================================================================

/// Mock portal data for testing.
#[derive(Clone)]
pub struct MockPortalData {
    pub id: String,
    pub title: String,
    pub description: Option<String>,
}

/// Mock portal client with configurable datasets.
#[derive(Clone)]
pub struct MockPortalClient {
    #[allow(dead_code)]
    portal_url: String,
    datasets: Vec<MockPortalData>,
}

impl MockPortalClient {
    pub fn new(portal_url: &str, datasets: Vec<MockPortalData>) -> Self {
        Self {
            portal_url: portal_url.to_string(),
            datasets,
        }
    }
}

impl PortalClient for MockPortalClient {
    type PortalData = MockPortalData;

    fn portal_type(&self) -> &'static str {
        "mock"
    }

    fn base_url(&self) -> &str {
        &self.portal_url
    }

    async fn list_dataset_ids(&self) -> Result<Vec<String>, AppError> {
        Ok(self.datasets.iter().map(|d| d.id.clone()).collect())
    }

    async fn get_dataset(&self, id: &str) -> Result<Self::PortalData, AppError> {
        self.datasets
            .iter()
            .find(|d| d.id == id)
            .cloned()
            .ok_or_else(|| AppError::Generic(format!("Dataset not found: {}", id)))
    }

    fn into_new_dataset(
        data: Self::PortalData,
        portal_url: &str,
        url_template: Option<&str>,
        language: &str,
    ) -> NewDataset {
        let content_hash = NewDataset::compute_content_hash_with_language(
            &data.title,
            data.description.as_deref(),
            language,
        );

        // Mock uses `id` for both `{id}` and `{name}` since MockPortalData
        // has no separate name/slug field. This is sufficient for testing
        // the template plumbing; CKAN-specific substitution is tested in ckan.rs.
        let url = match url_template {
            Some(template) => template
                .replace("{id}", &data.id)
                .replace("{name}", &data.id),
            None => format!("{}/dataset/{}", portal_url.trim_end_matches('/'), data.id),
        };

        NewDataset {
            original_id: data.id.clone(),
            source_portal: portal_url.to_string(),
            url,
            title: data.title,
            description: data.description,
            embedding: None,
            metadata: serde_json::json!({}),
            content_hash,
        }
    }

    async fn search_modified_since(
        &self,
        _since: DateTime<Utc>,
    ) -> Result<Vec<Self::PortalData>, AppError> {
        // For testing, return all datasets as "modified"
        Ok(self.datasets.clone())
    }

    async fn search_all_datasets(&self) -> Result<Vec<Self::PortalData>, AppError> {
        // For testing, return all datasets
        Ok(self.datasets.clone())
    }

    async fn dataset_count(&self) -> Result<usize, AppError> {
        Ok(self.datasets.len())
    }
}

// =============================================================================
// MockPortalClientFactory
// =============================================================================

/// Factory for creating mock portal clients.
#[derive(Clone)]
pub struct MockPortalClientFactory {
    datasets: Vec<MockPortalData>,
}

impl MockPortalClientFactory {
    pub fn new(datasets: Vec<MockPortalData>) -> Self {
        Self { datasets }
    }
}

impl PortalClientFactory for MockPortalClientFactory {
    type Client = MockPortalClient;

    fn create(
        &self,
        portal_url: &str,
        _portal_type: ceres_core::config::PortalType,
        _language: &str,
        _profile: Option<&str>,
        _sparql_endpoint: Option<&str>,
    ) -> Result<Self::Client, AppError> {
        Ok(MockPortalClient::new(portal_url, self.datasets.clone()))
    }
}

// =============================================================================
// MockDatasetStore
// =============================================================================

/// Records a call to record_sync_status
#[derive(Clone, Debug)]
pub struct SyncStatusRecord {
    pub portal_url: String,
    #[allow(dead_code)]
    pub sync_mode: String,
    pub sync_status: String,
    pub datasets_synced: i32,
}

/// In-memory dataset store for testing.
///
/// Stores datasets in a `HashMap` and provides full implementation of
/// `DatasetStore` trait for verifying harvest/search logic.
#[derive(Clone)]
pub struct MockDatasetStore {
    /// Stored datasets keyed by (source_portal, original_id)
    datasets: Arc<Mutex<HashMap<(String, String), StoredDataset>>>,
    /// History of sync status records
    pub sync_history: Arc<Mutex<Vec<SyncStatusRecord>>>,
}

/// Internal representation of a stored dataset.
#[derive(Clone)]
struct StoredDataset {
    id: Uuid,
    dataset: NewDataset,
}

impl MockDatasetStore {
    pub fn new() -> Self {
        Self {
            datasets: Arc::new(Mutex::new(HashMap::new())),
            sync_history: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Returns the number of stored datasets.
    #[allow(dead_code)]
    pub fn len(&self) -> usize {
        self.datasets.lock().unwrap().len()
    }

    /// Returns true if no datasets are stored.
    #[allow(dead_code)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Checks if a dataset exists for the given portal and original_id.
    #[allow(dead_code)]
    pub fn contains(&self, portal_url: &str, original_id: &str) -> bool {
        let key = (portal_url.to_string(), original_id.to_string());
        self.datasets.lock().unwrap().contains_key(&key)
    }

    /// Gets a dataset by portal URL and original ID.
    #[allow(dead_code)]
    pub fn get(&self, portal_url: &str, original_id: &str) -> Option<NewDataset> {
        let key = (portal_url.to_string(), original_id.to_string());
        self.datasets
            .lock()
            .unwrap()
            .get(&key)
            .map(|s| s.dataset.clone())
    }
}

impl Default for MockDatasetStore {
    fn default() -> Self {
        Self::new()
    }
}

impl DatasetStore for MockDatasetStore {
    async fn get_by_id(&self, id: Uuid) -> Result<Option<Dataset>, AppError> {
        let datasets = self.datasets.lock().unwrap();
        for stored in datasets.values() {
            if stored.id == id {
                return Ok(Some(Dataset {
                    id: stored.id,
                    original_id: stored.dataset.original_id.clone(),
                    source_portal: stored.dataset.source_portal.clone(),
                    url: stored.dataset.url.clone(),
                    title: stored.dataset.title.clone(),
                    description: stored.dataset.description.clone(),
                    embedding: stored.dataset.embedding.clone(),
                    metadata: stored.dataset.metadata.clone(),
                    first_seen_at: chrono::Utc::now(),
                    last_updated_at: chrono::Utc::now(),
                    content_hash: Some(stored.dataset.content_hash.clone()),
                    is_stale: false,
                }));
            }
        }
        Ok(None)
    }

    async fn get_hashes_for_portal(
        &self,
        portal_url: &str,
    ) -> Result<HashMap<String, Option<String>>, AppError> {
        let datasets = self.datasets.lock().unwrap();
        let hashes: HashMap<String, Option<String>> = datasets
            .iter()
            .filter(|((portal, _), _)| portal == portal_url)
            .map(|((_, original_id), stored)| {
                (
                    original_id.clone(),
                    Some(stored.dataset.content_hash.clone()),
                )
            })
            .collect();
        Ok(hashes)
    }

    async fn update_timestamp_only(
        &self,
        _portal_url: &str,
        _original_id: &str,
    ) -> Result<(), AppError> {
        // No-op for mock - we don't track timestamps
        Ok(())
    }

    async fn batch_update_timestamps(
        &self,
        _portal_url: &str,
        original_ids: &[String],
    ) -> Result<u64, AppError> {
        // Return count as if all were updated
        Ok(original_ids.len() as u64)
    }

    async fn mark_stale_datasets(
        &self,
        _portal_url: &str,
        _sync_start: DateTime<Utc>,
    ) -> Result<u64, AppError> {
        Ok(0)
    }

    async fn mark_stale_by_exclusion(
        &self,
        _portal_url: &str,
        _seen_ids: &[String],
    ) -> Result<u64, AppError> {
        Ok(0)
    }

    async fn upsert(&self, dataset: &NewDataset) -> Result<Uuid, AppError> {
        let mut datasets = self.datasets.lock().unwrap();
        let key = (dataset.source_portal.clone(), dataset.original_id.clone());

        let id = if let Some(existing) = datasets.get(&key) {
            existing.id
        } else {
            Uuid::new_v4()
        };

        datasets.insert(
            key,
            StoredDataset {
                id,
                dataset: dataset.clone(),
            },
        );

        Ok(id)
    }

    async fn batch_upsert(&self, datasets: &[NewDataset]) -> Result<Vec<Uuid>, AppError> {
        let mut ids = Vec::with_capacity(datasets.len());
        for dataset in datasets {
            ids.push(self.upsert(dataset).await?);
        }
        Ok(ids)
    }

    async fn search(
        &self,
        _query_vector: Vec<f32>,
        limit: usize,
    ) -> Result<Vec<SearchResult>, AppError> {
        // Simple mock: return first `limit` datasets with fake similarity scores
        let datasets = self.datasets.lock().unwrap();
        let results: Vec<SearchResult> = datasets
            .values()
            .take(limit)
            .enumerate()
            .map(|(i, stored)| {
                // Create a minimal Dataset for SearchResult
                SearchResult {
                    dataset: ceres_core::Dataset {
                        id: stored.id,
                        original_id: stored.dataset.original_id.clone(),
                        source_portal: stored.dataset.source_portal.clone(),
                        url: stored.dataset.url.clone(),
                        title: stored.dataset.title.clone(),
                        description: stored.dataset.description.clone(),
                        embedding: stored.dataset.embedding.clone(),
                        metadata: stored.dataset.metadata.clone(),
                        first_seen_at: chrono::Utc::now(),
                        last_updated_at: chrono::Utc::now(),
                        content_hash: Some(stored.dataset.content_hash.clone()),
                        is_stale: false,
                    },
                    similarity_score: 1.0 - (i as f32 * 0.1),
                }
            })
            .collect();
        Ok(results)
    }

    fn list_stream<'a>(
        &'a self,
        portal_filter: Option<&'a str>,
        limit: Option<usize>,
    ) -> BoxStream<'a, Result<Dataset, AppError>> {
        // Simple mock: collect all matching datasets and stream them
        let datasets = self.datasets.lock().unwrap();
        let results: Vec<Result<Dataset, AppError>> = datasets
            .iter()
            .filter(|((portal, _), _)| portal_filter.is_none_or(|filter| portal == filter))
            .take(limit.unwrap_or(usize::MAX))
            .map(|((_, _), stored)| {
                Ok(Dataset {
                    id: stored.id,
                    original_id: stored.dataset.original_id.clone(),
                    source_portal: stored.dataset.source_portal.clone(),
                    url: stored.dataset.url.clone(),
                    title: stored.dataset.title.clone(),
                    description: stored.dataset.description.clone(),
                    embedding: stored.dataset.embedding.clone(),
                    metadata: stored.dataset.metadata.clone(),
                    first_seen_at: chrono::Utc::now(),
                    last_updated_at: chrono::Utc::now(),
                    content_hash: Some(stored.dataset.content_hash.clone()),
                    is_stale: false,
                })
            })
            .collect();
        Box::pin(futures::stream::iter(results))
    }

    async fn get_last_sync_time(
        &self,
        _portal_url: &str,
    ) -> Result<Option<DateTime<Utc>>, AppError> {
        // Always return None to simulate first sync (full sync)
        Ok(None)
    }

    async fn record_sync_status(
        &self,
        portal_url: &str,
        _sync_time: DateTime<Utc>,
        sync_mode: &str,
        sync_status: &str,
        datasets_synced: i32,
    ) -> Result<(), AppError> {
        self.sync_history.lock().unwrap().push(SyncStatusRecord {
            portal_url: portal_url.to_string(),
            sync_mode: sync_mode.to_string(),
            sync_status: sync_status.to_string(),
            datasets_synced,
        });
        Ok(())
    }

    async fn get_duplicate_titles(&self) -> Result<std::collections::HashSet<String>, AppError> {
        // Find titles that appear in more than one portal
        let datasets = self.datasets.lock().unwrap();
        let mut title_portals: HashMap<String, std::collections::HashSet<String>> = HashMap::new();
        for ((portal, _), stored) in datasets.iter() {
            title_portals
                .entry(stored.dataset.title.to_lowercase())
                .or_default()
                .insert(portal.clone());
        }
        Ok(title_portals
            .into_iter()
            .filter(|(_, portals)| portals.len() > 1)
            .map(|(title, _)| title)
            .collect())
    }

    async fn list_pending_embeddings(
        &self,
        portal_filter: Option<&str>,
        limit: Option<usize>,
    ) -> Result<Vec<Dataset>, AppError> {
        let datasets = self.datasets.lock().unwrap();
        let results: Vec<Dataset> = datasets
            .iter()
            .filter(|((portal, _), stored)| {
                stored.dataset.embedding.is_none()
                    && portal_filter.is_none_or(|filter| portal == filter)
            })
            .take(limit.unwrap_or(usize::MAX))
            .map(|((_, _), stored)| Dataset {
                id: stored.id,
                original_id: stored.dataset.original_id.clone(),
                source_portal: stored.dataset.source_portal.clone(),
                url: stored.dataset.url.clone(),
                title: stored.dataset.title.clone(),
                description: stored.dataset.description.clone(),
                embedding: stored.dataset.embedding.clone(),
                metadata: stored.dataset.metadata.clone(),
                first_seen_at: chrono::Utc::now(),
                last_updated_at: chrono::Utc::now(),
                content_hash: Some(stored.dataset.content_hash.clone()),
                is_stale: false,
            })
            .collect();
        Ok(results)
    }

    async fn count_pending_embeddings(&self, portal_filter: Option<&str>) -> Result<i64, AppError> {
        let datasets = self.datasets.lock().unwrap();
        let count = datasets
            .iter()
            .filter(|((portal, _), stored)| {
                stored.dataset.embedding.is_none()
                    && portal_filter.is_none_or(|filter| portal == filter)
            })
            .count();
        Ok(count as i64)
    }

    async fn health_check(&self) -> Result<(), AppError> {
        // Mock is always healthy
        Ok(())
    }
}

// =============================================================================
// MockJobQueue
// =============================================================================

/// In-memory job queue for testing worker state machine logic.
#[derive(Clone)]
pub struct MockJobQueue {
    jobs: Arc<Mutex<HashMap<Uuid, HarvestJob>>>,
    claim_error: Arc<Mutex<Option<String>>>,
}

impl MockJobQueue {
    pub fn new() -> Self {
        Self {
            jobs: Arc::new(Mutex::new(HashMap::new())),
            claim_error: Arc::new(Mutex::new(None)),
        }
    }

    /// Insert a pending job and return its ID.
    pub fn with_pending_job(self, portal_url: &str) -> (Self, Uuid) {
        self.with_pending_job_config(portal_url, 0, 3)
    }

    /// Insert a pending job with specific retry configuration.
    pub fn with_pending_job_config(
        self,
        portal_url: &str,
        retry_count: u32,
        max_retries: u32,
    ) -> (Self, Uuid) {
        let id = Uuid::new_v4();
        let job = HarvestJob {
            id,
            portal_url: portal_url.to_string(),
            portal_name: None,
            portal_type: ceres_core::config::PortalType::default(),
            status: JobStatus::Pending,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            started_at: None,
            completed_at: None,
            retry_count,
            max_retries,
            next_retry_at: None,
            error_message: None,
            sync_stats: None,
            worker_id: None,
            force_full_sync: false,
            url_template: None,
            language: None,
            profile: None,
            sparql_endpoint: None,
        };
        self.jobs.lock().unwrap().insert(id, job);
        (self, id)
    }

    /// Get the current status of a job.
    pub fn job_status(&self, job_id: Uuid) -> Option<JobStatus> {
        self.jobs.lock().unwrap().get(&job_id).map(|j| j.status)
    }

    /// Get the job by ID (for inspecting full state in tests).
    #[allow(dead_code)]
    pub fn get_job_snapshot(&self, job_id: Uuid) -> Option<HarvestJob> {
        self.jobs.lock().unwrap().get(&job_id).cloned()
    }

    /// Inject an error for the next claim_job call.
    #[allow(dead_code)]
    pub fn set_claim_error(&self, error: Option<String>) {
        *self.claim_error.lock().unwrap() = error;
    }
}

impl Default for MockJobQueue {
    fn default() -> Self {
        Self::new()
    }
}

impl JobQueue for MockJobQueue {
    async fn create_job(&self, request: CreateJobRequest) -> Result<HarvestJob, AppError> {
        let id = Uuid::new_v4();
        let job = HarvestJob {
            id,
            portal_url: request.portal_url,
            portal_name: request.portal_name,
            portal_type: request.portal_type,
            status: JobStatus::Pending,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            started_at: None,
            completed_at: None,
            retry_count: 0,
            max_retries: request.max_retries.unwrap_or(3),
            next_retry_at: None,
            error_message: None,
            sync_stats: None,
            worker_id: None,
            force_full_sync: request.force_full_sync,
            url_template: request.url_template,
            language: request.language,
            profile: request.profile,
            sparql_endpoint: request.sparql_endpoint,
        };
        self.jobs.lock().unwrap().insert(id, job.clone());
        Ok(job)
    }

    async fn claim_job(&self, worker_id: &str) -> Result<Option<HarvestJob>, AppError> {
        // Check for injected error
        if let Some(err_msg) = self.claim_error.lock().unwrap().take() {
            return Err(AppError::DatabaseError(err_msg));
        }

        let mut jobs = self.jobs.lock().unwrap();
        // Find first pending job that is ready to be claimed
        // (no next_retry_at, or next_retry_at <= now)
        let now = Utc::now();
        let pending_id = jobs
            .values()
            .find(|j| {
                j.status == JobStatus::Pending
                    && j.next_retry_at.is_none_or(|retry_at| retry_at <= now)
            })
            .map(|j| j.id);

        if let Some(id) = pending_id {
            let job = jobs.get_mut(&id).unwrap();
            job.status = JobStatus::Running;
            job.worker_id = Some(worker_id.to_string());
            job.started_at = Some(Utc::now());
            job.updated_at = Utc::now();
            Ok(Some(job.clone()))
        } else {
            Ok(None)
        }
    }

    async fn complete_job(&self, job_id: Uuid, stats: SyncStats) -> Result<(), AppError> {
        let mut jobs = self.jobs.lock().unwrap();
        if let Some(job) = jobs.get_mut(&job_id) {
            job.status = JobStatus::Completed;
            job.completed_at = Some(Utc::now());
            job.sync_stats = Some(stats);
            job.updated_at = Utc::now();
        }
        Ok(())
    }

    async fn fail_job(
        &self,
        job_id: Uuid,
        error: &str,
        next_retry_at: Option<DateTime<Utc>>,
    ) -> Result<(), AppError> {
        let mut jobs = self.jobs.lock().unwrap();
        if let Some(job) = jobs.get_mut(&job_id) {
            if next_retry_at.is_some() {
                // Retryable: reset to pending
                job.status = JobStatus::Pending;
                job.next_retry_at = next_retry_at;
                job.retry_count += 1;
            } else {
                // Permanent failure
                job.status = JobStatus::Failed;
                job.completed_at = Some(Utc::now());
            }
            job.error_message = Some(error.to_string());
            job.worker_id = None;
            job.updated_at = Utc::now();
        }
        Ok(())
    }

    async fn cancel_job(&self, job_id: Uuid, stats: Option<SyncStats>) -> Result<(), AppError> {
        let mut jobs = self.jobs.lock().unwrap();
        if let Some(job) = jobs.get_mut(&job_id) {
            job.status = JobStatus::Cancelled;
            job.completed_at = Some(Utc::now());
            job.sync_stats = stats;
            job.updated_at = Utc::now();
        }
        Ok(())
    }

    async fn get_job(&self, job_id: Uuid) -> Result<Option<HarvestJob>, AppError> {
        Ok(self.jobs.lock().unwrap().get(&job_id).cloned())
    }

    async fn list_jobs(
        &self,
        status: Option<JobStatus>,
        limit: usize,
    ) -> Result<Vec<HarvestJob>, AppError> {
        let jobs = self.jobs.lock().unwrap();
        let mut result: Vec<HarvestJob> = jobs
            .values()
            .filter(|j| status.is_none_or(|s| j.status == s))
            .cloned()
            .collect();
        result.sort_by_key(|b| std::cmp::Reverse(b.created_at));
        result.truncate(limit);
        Ok(result)
    }

    async fn release_job(&self, job_id: Uuid) -> Result<(), AppError> {
        let mut jobs = self.jobs.lock().unwrap();
        if let Some(job) = jobs.get_mut(&job_id)
            && job.status == JobStatus::Running
        {
            job.status = JobStatus::Pending;
            job.worker_id = None;
            job.updated_at = Utc::now();
        }
        Ok(())
    }

    async fn release_worker_jobs(&self, worker_id: &str) -> Result<u64, AppError> {
        let mut jobs = self.jobs.lock().unwrap();
        let mut released = 0u64;
        for job in jobs.values_mut() {
            if job.status == JobStatus::Running && job.worker_id.as_deref() == Some(worker_id) {
                job.status = JobStatus::Pending;
                job.worker_id = None;
                job.updated_at = Utc::now();
                released += 1;
            }
        }
        Ok(released)
    }

    async fn count_by_status(&self, status: JobStatus) -> Result<i64, AppError> {
        let jobs = self.jobs.lock().unwrap();
        Ok(jobs.values().filter(|j| j.status == status).count() as i64)
    }
}