adminx 0.2.6

A powerful, modern admin panel framework for Rust built on Actix Web and MongoDB with automatic CRUD, role-based access control, and a beautiful responsive UI
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
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
// crates/adminx/src/helpers/resource_helper.rs - Complete Fixed Version
use actix_web::{web, HttpRequest, HttpResponse, Scope};
use serde_json::Value;
use std::sync::Arc;
use tera::Context;
use tracing::{info, warn, error};
use std::collections::HashSet;
use actix_session::Session;
use futures::TryStreamExt;

use crate::AdmixResource;
use crate::configs::initializer::AdminxConfig;
use crate::utils::auth::extract_claims_from_session;
use crate::utils::structs::Claims;
use crate::registry::get_registered_menus;
use crate::actions::CustomAction;
use serde_json::json;
use crate::helpers::{
    custom_helper::{
        adapt_action_with_id,
        adapt_action_get_with_id
    }
};

pub fn actions_to_meta(actions: Vec<CustomAction>) -> Value {
    let list: Vec<Value> = actions
        .into_iter()
        .map(|a| {
            json!({
                "name": a.name,
                "method": a.method,
                // if your CustomAction has `ui: Option<ActionUi>` (Serialize):
                "ui": a.ui
            })
        })
        .collect();
    json!(list)
}


/// Check authentication and return user claims or redirect response
pub async fn check_authentication(
    session: &Session,
    config: &AdminxConfig,
    resource_name: &str,
    action: &str,
) -> Result<Claims, HttpResponse> {
    match extract_claims_from_session(session, config).await {
        Ok(claims) => {
            info!("🔐 Authenticated user {} accessing {} action on resource {}", 
                  claims.email, action, resource_name);
            Ok(claims)
        }
        Err(_) => {
            warn!("⚠️  Unauthenticated access attempt to {} action on resource {}", action, resource_name);
            Err(HttpResponse::Found()
                .append_header(("Location", "/adminx/login"))
                .finish())
        }
    }
}

/// Check if user has permission for resource action - Enhanced version
pub async fn check_resource_permission(
    session: &Session,
    config: &AdminxConfig,
    resource: &dyn AdmixResource,
    action: &str,
) -> Result<Claims, HttpResponse> {
    match extract_claims_from_session(session, config).await {
        Ok(claims) => {
            let user_roles: HashSet<String> = {
                let mut roles = claims.roles.clone();
                roles.push(claims.role.clone());
                roles.into_iter().collect()
            };
            
            let allowed_roles: HashSet<String> = 
                resource.allowed_roles().into_iter().collect();
            
            if user_roles.intersection(&allowed_roles).next().is_some() {
                info!("User {} has permission for {} action on resource {}", 
                      claims.email, action, resource.resource_name());
                Ok(claims)
            } else {
                warn!("User {} lacks permission for {} action on resource {} (user roles: {:?}, required: {:?})", 
                      claims.email, action, resource.resource_name(), claims.roles, resource.allowed_roles());
                Err(HttpResponse::Forbidden().json(serde_json::json!({
                    "error": "Insufficient permissions",
                    "required_roles": resource.allowed_roles(),
                    "user_roles": claims.roles,
                    "action": action,
                    "resource": resource.resource_name()
                })))
            }
        }
        Err(_) => {
            Err(HttpResponse::Found()
                .append_header(("Location", "/adminx/login"))
                .finish())
        }
    }
}

/// Create template context for UI routes with common data
pub fn create_base_template_context(
    resource_name: &str,
    base_path: &str,
    claims: &Claims,
) -> Context {
    let mut ctx = Context::new();
    ctx.insert("resource_name", resource_name);
    ctx.insert("base_path", &format!("/adminx/{}", base_path));
    ctx.insert("menus", &get_registered_menus());
    ctx.insert("current_user", claims);
    ctx.insert("is_authenticated", &true);
    ctx
}


pub fn handle_delete_response(
    response: HttpResponse,
    base_path: &str,
    resource_name: &str,
) -> HttpResponse {
    if response.status().is_success() {
        info!("✅ Resource '{}' item deleted successfully, redirecting to list", resource_name);
        let location = format!("/adminx/{}/list?success=deleted", base_path);
        HttpResponse::Found()
            .append_header(("Location", location))
            .finish()
    } else {
        error!("❌ Resource '{}' item deletion failed with status: {}", resource_name, response.status());
        let location = format!("/adminx/{}/list?error=delete_failed", base_path);
        HttpResponse::Found()
            .append_header(("Location", location))
            .finish()
    }
}

/// Handle form data conversion from HTML form to JSON - Enhanced version
pub fn convert_form_data_to_json(
    form_data: std::collections::HashMap<String, String>
) -> Value {
    let mut json_data = serde_json::Map::new();
    
    for (key, value) in form_data {
        // Skip editor mode fields (they're just for UI state)
        if key.ends_with("_mode") {
            continue;
        }
        
        if !value.is_empty() {
            // Handle boolean fields
            if key == "deleted" || key == "active" || key == "enabled" || key.ends_with("_flag") {
                match value.as_str() {
                    "true" | "1" | "on" => {
                        json_data.insert(key, serde_json::Value::Bool(true));
                    }
                    "false" | "0" | "off" => {
                        json_data.insert(key, serde_json::Value::Bool(false));
                    }
                    _ => {
                        // If it's not a clear boolean, treat as string
                        json_data.insert(key, serde_json::Value::String(value));
                    }
                }
            }
            // Handle numeric fields
            else if key.ends_with("_id") || key.ends_with("_count") || key.ends_with("_number") {
                if let Ok(num) = value.parse::<i64>() {
                    json_data.insert(key, serde_json::Value::Number(serde_json::Number::from(num)));
                } else if let Ok(num) = value.parse::<f64>() {
                    if let Some(num_val) = serde_json::Number::from_f64(num) {
                        json_data.insert(key, serde_json::Value::Number(num_val));
                    } else {
                        json_data.insert(key, serde_json::Value::String(value));
                    }
                } else {
                    json_data.insert(key, serde_json::Value::String(value));
                }
            }
            // Handle JSON fields (try to parse as JSON first)
            else if key == "data" || key.ends_with("_json") || key.ends_with("_config") {
                // Try to parse as JSON first to validate, but store as string
                match serde_json::from_str::<serde_json::Value>(&value) {
                    Ok(_) => {
                        // If it parsed successfully, store as string (most APIs expect JSON fields as strings)
                        json_data.insert(key, serde_json::Value::String(value));
                    }
                    Err(_) => {
                        // If it's not valid JSON, store as-is
                        json_data.insert(key, serde_json::Value::String(value));
                    }
                }
            }
            // Default: treat as string
            else {
                json_data.insert(key, serde_json::Value::String(value));
            }
        }
    }
    
    serde_json::Value::Object(json_data)
}


/*-------------------------------------------------------------------------
/// START Handle resource creation response and return appropriate redirect
--------------------------------------------------------------------------*/
pub fn handle_create_response(
    response: HttpResponse,
    base_path: &str,
    resource_name: &str,
) -> HttpResponse {
    if response.status().is_success() {
        info!("✅ Resource '{}' created successfully, redirecting to list", resource_name);
        let location = format!("/adminx/{}/list?success=created", base_path);
        HttpResponse::Found()
            .append_header(("Location", location))
            .finish()
    } else {
        error!("❌ Resource '{}' creation failed with status: {}", resource_name, response.status());
        let location = format!("/adminx/{}/new?error=create_failed", base_path);
        HttpResponse::Found()
            .append_header(("Location", location))
            .finish()
    }
}

pub fn handle_update_response(
    response: HttpResponse,
    base_path: &str,
    item_id: &str,
    resource_name: &str,
) -> HttpResponse {
    if response.status().is_success() {
        info!("✅ Resource '{}' item '{}' updated successfully, redirecting to view", resource_name, item_id);
        let location = format!("/adminx/{}/view/{}?success=updated", base_path, item_id);
        HttpResponse::Found()
            .append_header(("Location", location))
            .finish()
    } else {
        error!("❌ Resource '{}' item '{}' update failed with status: {}", resource_name, item_id, response.status());
        let location = format!("/adminx/{}/edit/{}?error=update_failed", base_path, item_id);
        HttpResponse::Found()
            .append_header(("Location", location))
            .finish()
    }
}
/*-------------------------------------------------------------------------
/// END Handle resource creation response and return appropriate redirect
--------------------------------------------------------------------------*/



/// Get default list structure for resources that don't define one
pub fn get_default_list_structure() -> Value {
    serde_json::json!({
        "columns": [
            {
                "field": "id",
                "label": "ID",
                "sortable": false
            },
            {
                "field": "created_at",
                "label": "Created At",
                "type": "datetime",
                "sortable": true
            }
        ],
        "actions": ["view", "edit", "delete"]
    })
}

/// Fetch list data - Generic version that works with any resource
pub async fn fetch_list_data(
    resource: &Arc<Box<dyn AdmixResource>>,
    req: &HttpRequest,
    _query_string: String,
) -> Result<(Vec<String>, Vec<serde_json::Map<String, Value>>, Value), Box<dyn std::error::Error + Send + Sync>> {
    let collection = resource.get_collection();
    
    // Parse query parameters for pagination and filters
    let query_params: std::collections::HashMap<String, String> = 
        serde_urlencoded::from_str(req.query_string()).unwrap_or_default();
    
    let page: u64 = query_params.get("page")
        .and_then(|p| p.parse().ok())
        .unwrap_or(1);
    let per_page: u64 = query_params.get("per_page")
        .and_then(|p| p.parse().ok())
        .unwrap_or(10);
    
    let skip = (page - 1) * per_page;
    
    // Build filter document from query parameters
    let mut filter_doc = mongodb::bson::doc! {};
    
    // Get permitted query fields for security
    let permitted_fields: HashSet<&str> = resource.permit_keys().into_iter().collect();
    
    // Build filters based on query parameters
    for (key, value) in &query_params {
        if !value.is_empty() && (permitted_fields.contains(key.as_str()) || key == "search") {
            match key.as_str() {
                // Text fields that should use regex search
                "name" | "email" | "username" | "key" | "title" | "description" | "search" => {
                    if key == "search" {
                        // Global search across multiple fields
                        let search_fields = vec!["name", "email", "username", "key", "title", "description"];
                        let mut search_conditions = Vec::new();
                        
                        for field in search_fields {
                            if permitted_fields.contains(field) {
                                search_conditions.push(mongodb::bson::doc! {
                                    field: {
                                        "$regex": value,
                                        "$options": "i"
                                    }
                                });
                            }
                        }
                        
                        if !search_conditions.is_empty() {
                            filter_doc.insert("$or", search_conditions);
                        }
                    } else {
                        filter_doc.insert(key, mongodb::bson::doc! {
                            "$regex": value,
                            "$options": "i"
                        });
                    }
                }
                // Exact match fields
                "status" | "data_type" | "deleted" | "active" | "enabled" => {
                    // Handle boolean fields properly
                    if value == "true" || value == "false" {
                        let bool_val = value == "true";
                        filter_doc.insert(key, bool_val);
                    } else {
                        filter_doc.insert(key, value);
                    }
                }
                // Date range filters
                key if key.ends_with("_from") => {
                    let base_field = key.trim_end_matches("_from");
                    if permitted_fields.contains(base_field) {
                        if let Ok(date) = chrono::DateTime::parse_from_rfc3339(&format!("{}T00:00:00Z", value)) {
                            let existing_filter = filter_doc.get_mut(base_field);
                            match existing_filter {
                                Some(mongodb::bson::Bson::Document(ref mut doc)) => {
                                    doc.insert("$gte", mongodb::bson::DateTime::from_chrono(date.with_timezone(&chrono::Utc)));
                                }
                                _ => {
                                    filter_doc.insert(base_field, mongodb::bson::doc! {
                                        "$gte": mongodb::bson::DateTime::from_chrono(date.with_timezone(&chrono::Utc))
                                    });
                                }
                            }
                        }
                    }
                }
                key if key.ends_with("_to") => {
                    let base_field = key.trim_end_matches("_to");
                    if permitted_fields.contains(base_field) {
                        if let Ok(date) = chrono::DateTime::parse_from_rfc3339(&format!("{}T23:59:59Z", value)) {
                            let existing_filter = filter_doc.get_mut(base_field);
                            match existing_filter {
                                Some(mongodb::bson::Bson::Document(ref mut doc)) => {
                                    doc.insert("$lte", mongodb::bson::DateTime::from_chrono(date.with_timezone(&chrono::Utc)));
                                }
                                _ => {
                                    filter_doc.insert(base_field, mongodb::bson::doc! {
                                        "$lte": mongodb::bson::DateTime::from_chrono(date.with_timezone(&chrono::Utc))
                                    });
                                }
                            }
                        }
                    }
                }
                // Number range filters
                key if key.ends_with("_min") => {
                    let base_field = key.trim_end_matches("_min");
                    if permitted_fields.contains(base_field) {
                        if let Ok(num) = value.parse::<f64>() {
                            let existing_filter = filter_doc.get_mut(base_field);
                            match existing_filter {
                                Some(mongodb::bson::Bson::Document(ref mut doc)) => {
                                    doc.insert("$gte", num);
                                }
                                _ => {
                                    filter_doc.insert(base_field, mongodb::bson::doc! {
                                        "$gte": num
                                    });
                                }
                            }
                        }
                    }
                }
                key if key.ends_with("_max") => {
                    let base_field = key.trim_end_matches("_max");
                    if permitted_fields.contains(base_field) {
                        if let Ok(num) = value.parse::<f64>() {
                            let existing_filter = filter_doc.get_mut(base_field);
                            match existing_filter {
                                Some(mongodb::bson::Bson::Document(ref mut doc)) => {
                                    doc.insert("$lte", num);
                                }
                                _ => {
                                    filter_doc.insert(base_field, mongodb::bson::doc! {
                                        "$lte": num
                                    });
                                }
                            }
                        }
                    }
                }
                _ => {
                    // Default: exact match for other fields
                    filter_doc.insert(key, value);
                }
            }
        }
    }
    
    info!("Applied filters: {:?}", filter_doc);
    
    // Get total count with filters
    let total = collection.count_documents(filter_doc.clone(), None).await
        .unwrap_or(0);
    
    // Fetch documents with pagination and filters
    let mut find_options = mongodb::options::FindOptions::default();
    find_options.skip = Some(skip);
    find_options.limit = Some(per_page as i64);
    find_options.sort = Some(mongodb::bson::doc! { "created_at": -1 });
    
    let mut cursor = collection.find(filter_doc, find_options).await
        .map_err(|e| format!("Database query failed: {}", e))?;
    
    let mut documents = Vec::new();
    while let Some(doc) = cursor.try_next().await.unwrap_or(None) {
        documents.push(doc);
    }
    
    // Get column structure from resource's list_structure or use defaults
    let list_structure = resource.list_structure().unwrap_or_else(|| get_default_list_structure());
    let columns = list_structure.get("columns")
        .and_then(|c| c.as_array())
        .map(|cols| {
            cols.iter()
                .filter_map(|col| col.get("field").and_then(|f| f.as_str()))
                .map(|s| s.to_string())
                .collect::<Vec<String>>()
        })
        .unwrap_or_else(|| {
            // Default columns based on permitted fields
            let mut default_cols = vec!["id".to_string()];
            let permitted = resource.permit_keys();
            for field in permitted {
                if field != "_id" && field != "created_at" && field != "updated_at" {
                    default_cols.push(field.to_string());
                }
            }
            default_cols.push("created_at".to_string());
            default_cols
        });
    
    // Convert MongoDB documents to the format expected by the template
    let rows: Vec<serde_json::Map<String, Value>> = documents
        .into_iter()
        .map(|doc| {
            let mut row = serde_json::Map::new();
            
            // Handle MongoDB ObjectId
            if let Ok(oid) = doc.get_object_id("_id") {
                row.insert("id".to_string(), Value::String(oid.to_hex()));
            }
            
            // Extract fields based on permitted fields and columns
            for field_name in &columns {
                if field_name == "id" {
                    continue; // Already handled above
                }
                
                // Try different data types for each field
                if let Ok(string_val) = doc.get_str(field_name) {
                    row.insert(field_name.clone(), Value::String(string_val.to_string()));
                } else if let Ok(bool_val) = doc.get_bool(field_name) {
                    row.insert(field_name.clone(), Value::String(bool_val.to_string()));
                } else if let Ok(int_val) = doc.get_i32(field_name) {
                    row.insert(field_name.clone(), Value::String(int_val.to_string()));
                } else if let Ok(int64_val) = doc.get_i64(field_name) {
                    row.insert(field_name.clone(), Value::String(int64_val.to_string()));
                } else if let Ok(datetime_val) = doc.get_datetime(field_name) {
                    let timestamp_ms = datetime_val.timestamp_millis();
                    if let Some(datetime) = chrono::DateTime::from_timestamp_millis(timestamp_ms) {
                        row.insert(field_name.clone(), 
                                 Value::String(datetime.format("%Y-%m-%d %H:%M:%S").to_string()));
                    } else {
                        row.insert(field_name.clone(), Value::String("N/A".to_string()));
                    }
                } else if doc.contains_key(field_name) {
                    // Handle other BSON types
                    if let Some(bson_val) = doc.get(field_name) {
                        match bson_val {
                            mongodb::bson::Bson::String(s) => {
                                row.insert(field_name.clone(), Value::String(s.clone()));
                            }
                            mongodb::bson::Bson::Boolean(b) => {
                                row.insert(field_name.clone(), Value::String(b.to_string()));
                            }
                            mongodb::bson::Bson::Int32(i) => {
                                row.insert(field_name.clone(), Value::String(i.to_string()));
                            }
                            mongodb::bson::Bson::Int64(i) => {
                                row.insert(field_name.clone(), Value::String(i.to_string()));
                            }
                            mongodb::bson::Bson::Double(d) => {
                                row.insert(field_name.clone(), Value::String(d.to_string()));
                            }
                            mongodb::bson::Bson::Null => {
                                row.insert(field_name.clone(), Value::String("".to_string()));
                            }
                            _ => {
                                row.insert(field_name.clone(), Value::String(format!("{:?}", bson_val)));
                            }
                        }
                    }
                } else {
                    // Field doesn't exist in document
                    row.insert(field_name.clone(), Value::String("N/A".to_string()));
                }
            }
            
            row
        })
        .collect();
    
    let total_pages = if per_page > 0 { (total + per_page - 1) / per_page } else { 1 };
    
    // Build pagination with current filters
    let mut filter_params = Vec::new();
    for (key, value) in &query_params {
        if key != "page" && !value.is_empty() {
            filter_params.push(format!("{}={}", key, urlencoding::encode(value)));
        }
    }
    let filter_string = if filter_params.is_empty() {
        String::new()
    } else {
        format!("&{}", filter_params.join("&"))
    };
    
    let pagination = serde_json::json!({
        "current": page,
        "total": total_pages,
        "prev": if page > 1 { Some(page - 1) } else { None },
        "next": if page < total_pages { Some(page + 1) } else { None },
        "filter_params": filter_string
    });
    
    info!("Fetched {} items for list view (page {} of {}) with filters", rows.len(), page, total_pages);
    Ok((columns, rows, pagination))
}

/// Get filters data and current filter values for the template
pub fn get_filters_data(
    resource: &Arc<Box<dyn AdmixResource>>,
    query_params: &std::collections::HashMap<String, String>
) -> (Option<Value>, serde_json::Map<String, Value>) {
    let filters = resource.filters();
    let mut current_filters = serde_json::Map::new();
    
    // Extract current filter values from query parameters
    if let Some(filter_config) = &filters {
        if let Some(filter_array) = filter_config.get("filters").and_then(|f| f.as_array()) {
            for filter in filter_array {
                if let Some(field) = filter.get("field").and_then(|f| f.as_str()) {
                    if let Some(value) = query_params.get(field) {
                        if !value.is_empty() {
                            current_filters.insert(field.to_string(), Value::String(value.clone()));
                        }
                    }
                    
                    // Handle range filters (date_range, number_range)
                    let from_key = format!("{}_from", field);
                    let to_key = format!("{}_to", field);
                    let min_key = format!("{}_min", field);
                    let max_key = format!("{}_max", field);
                    
                    if let Some(from_value) = query_params.get(&from_key) {
                        if !from_value.is_empty() {
                            current_filters.insert(from_key, Value::String(from_value.clone()));
                        }
                    }
                    
                    if let Some(to_value) = query_params.get(&to_key) {
                        if !to_value.is_empty() {
                            current_filters.insert(to_key, Value::String(to_value.clone()));
                        }
                    }
                    
                    if let Some(min_value) = query_params.get(&min_key) {
                        if !min_value.is_empty() {
                            current_filters.insert(min_key, Value::String(min_value.clone()));
                        }
                    }
                    
                    if let Some(max_value) = query_params.get(&max_key) {
                        if !max_value.is_empty() {
                            current_filters.insert(max_key, Value::String(max_value.clone()));
                        }
                    }
                }
            }
        }
    }
    
    // Also handle global search
    if let Some(search_value) = query_params.get("search") {
        if !search_value.is_empty() {
            current_filters.insert("search".to_string(), Value::String(search_value.clone()));
        }
    }
    
    (filters, current_filters)
}

/// Fetch single item data for view/edit pages - Generic version that works with any resource
pub async fn fetch_single_item_data(
    resource: &Arc<Box<dyn AdmixResource>>,
    _req: &HttpRequest,
    id: &str,
) -> Result<serde_json::Map<String, Value>, Box<dyn std::error::Error + Send + Sync>> {
    let collection = resource.get_collection();
    
    // Parse ObjectId
    let oid = mongodb::bson::oid::ObjectId::parse_str(id)
        .map_err(|e| format!("Invalid ObjectId: {}", e))?;
    
    // Find the document
    let doc = collection.find_one(mongodb::bson::doc! { "_id": oid }, None).await
        .map_err(|e| format!("Database query failed: {}", e))?
        .ok_or("Document not found")?;
    
    // Convert to template-friendly format
    let mut record = serde_json::Map::new();
    
    // Handle MongoDB ObjectId first
    if let Ok(oid) = doc.get_object_id("_id") {
        record.insert("id".to_string(), Value::String(oid.to_hex()));
    }
    
    // Get all permitted fields from the resource and extract them from the document
    let permitted_fields = resource.permit_keys();
    
    for field_name in permitted_fields {
        // Try different data types for each field
        if let Ok(string_val) = doc.get_str(field_name) {
            record.insert(field_name.to_string(), Value::String(string_val.to_string()));
        } else if let Ok(bool_val) = doc.get_bool(field_name) {
            record.insert(field_name.to_string(), Value::String(bool_val.to_string()));
        } else if let Ok(int_val) = doc.get_i32(field_name) {
            record.insert(field_name.to_string(), Value::String(int_val.to_string()));
        } else if let Ok(int64_val) = doc.get_i64(field_name) {
            record.insert(field_name.to_string(), Value::String(int64_val.to_string()));
        } else if let Ok(float_val) = doc.get_f64(field_name) {
            record.insert(field_name.to_string(), Value::String(float_val.to_string()));
        } else if let Ok(datetime_val) = doc.get_datetime(field_name) {
            let timestamp_ms = datetime_val.timestamp_millis();
            if let Some(datetime) = chrono::DateTime::from_timestamp_millis(timestamp_ms) {
                // For date/datetime fields, format them appropriately
                if field_name.contains("date") || field_name.contains("time") || field_name == "created_at" || field_name == "updated_at" {
                    record.insert(field_name.to_string(), 
                                Value::String(datetime.format("%Y-%m-%d %H:%M:%S").to_string()));
                } else {
                    record.insert(field_name.to_string(), 
                                Value::String(datetime.to_rfc3339()));
                }
            } else {
                record.insert(field_name.to_string(), Value::String("N/A".to_string()));
            }
        }
        // If field exists but we can't parse it, try to get it as a generic BSON value
        else if doc.contains_key(field_name) {
            if let Some(bson_val) = doc.get(field_name) {
                match bson_val {
                    mongodb::bson::Bson::String(s) => {
                        record.insert(field_name.to_string(), Value::String(s.clone()));
                    }
                    mongodb::bson::Bson::Boolean(b) => {
                        record.insert(field_name.to_string(), Value::String(b.to_string()));
                    }
                    mongodb::bson::Bson::Int32(i) => {
                        record.insert(field_name.to_string(), Value::String(i.to_string()));
                    }
                    mongodb::bson::Bson::Int64(i) => {
                        record.insert(field_name.to_string(), Value::String(i.to_string()));
                    }
                    mongodb::bson::Bson::Double(d) => {
                        record.insert(field_name.to_string(), Value::String(d.to_string()));
                    }
                    mongodb::bson::Bson::Null => {
                        record.insert(field_name.to_string(), Value::String("".to_string()));
                    }
                    _ => {
                        // For complex types, convert to string representation
                        record.insert(field_name.to_string(), Value::String(format!("{:?}", bson_val)));
                    }
                }
            }
        }
    }
    
    // Always handle standard timestamp fields even if not in permit_keys
    if !record.contains_key("created_at") {
        if let Ok(created_at) = doc.get_datetime("created_at") {
            let timestamp_ms = created_at.timestamp_millis();
            if let Some(datetime) = chrono::DateTime::from_timestamp_millis(timestamp_ms) {
                record.insert("created_at".to_string(), 
                             Value::String(datetime.format("%Y-%m-%d %H:%M:%S").to_string()));
            }
        }
    }
    
    if !record.contains_key("updated_at") {
        if let Ok(updated_at) = doc.get_datetime("updated_at") {
            let timestamp_ms = updated_at.timestamp_millis();
            if let Some(datetime) = chrono::DateTime::from_timestamp_millis(timestamp_ms) {
                record.insert("updated_at".to_string(), 
                             Value::String(datetime.format("%Y-%m-%d %H:%M:%S").to_string()));
            }
        }
    }
    
    info!("Fetched single item with id: {} for resource: {} with fields: {:?}", 
          id, resource.resource_name(), record.keys().collect::<Vec<_>>());
    Ok(record)
}

pub fn get_default_form_structure() -> Value {
    serde_json::json!({
        "groups": [
            {
                "title": "Details",
                "fields": []
            }
        ]
    })
}

/// Get default view structure for resources that don't define one
pub fn get_default_view_structure() -> Value {
    serde_json::json!({
        "sections": [
            {
                "title": "Details",
                "fields": []
            }
        ]
    })
}

/// Register API-only routes without UI components
pub fn register_api_only_routes(resource: Box<dyn AdmixResource>) -> Scope {
    let resource_name = resource.resource_name().to_string();
    info!("Registering API-only routes for resource: {}", resource_name);
    
    let mut scope = web::scope("");

    // GET / - List all items
    let list_resource = resource.clone_box();
    scope = scope.route("", web::get().to(move |req: HttpRequest, query: web::Query<String>| {
        let resource = list_resource.clone_box();
        async move { 
            info!("📡 List API endpoint called for resource: {}", resource.resource_name());
            resource.list(&req, query.into_inner()).await 
        }
    }));

    // POST / - Create new item
    let create_resource = resource.clone_box();
    scope = scope.route("", web::post().to(move |req: HttpRequest, body: web::Json<Value>| {
        let resource = create_resource.clone_box();
        async move { 
            info!("📡 Create API endpoint called for resource: {}", resource.resource_name());
            resource.create(&req, body.into_inner()).await 
        }
    }));

    // GET /{id} - Get single item
    let get_resource = resource.clone_box();
    scope = scope.route("/{id}", web::get().to(move |req: HttpRequest, path: web::Path<String>| {
        let resource = get_resource.clone_box();
        async move { 
            let id = path.into_inner();
            info!("📡 Get API endpoint called for resource: {} with id: {}", resource.resource_name(), id);
            resource.get(&req, id).await 
        }
    }));

    // PUT /{id} - Update item
    let update_resource = resource.clone_box();
    scope = scope.route("/{id}", web::put().to(move |req: HttpRequest, path: web::Path<String>, body: web::Json<Value>| {
        let resource = update_resource.clone_box();
        async move { 
            let id = path.into_inner();
            info!("📡 Update API endpoint called for resource: {} with id: {}", resource.resource_name(), id);
            resource.update(&req, id, body.into_inner()).await 
        }
    }));

    // DELETE /{id} - Delete item
    let delete_resource = resource.clone_box();
    scope = scope.route("/{id}", web::delete().to(move |req: HttpRequest, path: web::Path<String>| {
        let resource = delete_resource.clone_box();
        async move { 
            let id = path.into_inner();
            info!("📡 Delete API endpoint called for resource: {} with id: {}", resource.resource_name(), id);
            resource.delete(&req, id).await 
        }
    }));

    // Add custom actions
    for action in resource.custom_actions() {
        let path = format!("/{{id}}/{}", action.name);

        match action.method {
            "POST" => {
                scope = scope.route(&path, web::post().to(adapt_action_with_id(action.handler)));
            }
            "PUT" => {
                scope = scope.route(&path, web::put().to(adapt_action_with_id(action.handler)));
            }
            "PATCH" => {
                scope = scope.route(&path, web::patch().to(adapt_action_with_id(action.handler)));
            }
            "GET" => {
                scope = scope.route(&path, web::get().to(adapt_action_get_with_id(action.handler)));
            }
            "DELETE" => {
                scope = scope.route(&path, web::delete().to(adapt_action_get_with_id(action.handler)));
            }
            method => {
                error!("Unsupported HTTP method: {} for action: {}", method, action.name);
            }
        }
    }


    scope
}

/// Register protected routes with role-based access control
pub fn register_protected_resource_routes(resource: Box<dyn AdmixResource>) -> Scope {
    let resource_name = resource.resource_name().to_string();
    let allowed_roles = resource.allowed_roles();
    
    info!("Registering protected routes for resource: {} with roles: {:?}", resource_name, allowed_roles);
    
    let mut scope = web::scope("");

    // GET / - List with role check
    let list_resource = resource.clone_box();
    scope = scope.route(
        "",
        web::get().to(move |req: HttpRequest, query: web::Query<String>, session: Session, config: web::Data<AdminxConfig>| {
            let resource = list_resource.clone_box();
            async move {
                match check_resource_permission(&session, &config, resource.as_ref(), "list").await {
                    Ok(_claims) => resource.list(&req, query.into_inner()).await,
                    Err(response) => response,
                }
            }
        }),
    );

    // POST / - Create with role check
    let create_resource = resource.clone_box();
    scope = scope.route(
        "",
        web::post().to(move |req: HttpRequest, body: web::Json<Value>, session: Session, config: web::Data<AdminxConfig>| {
            let resource = create_resource.clone_box();
            async move {
                match check_resource_permission(&session, &config, resource.as_ref(), "create").await {
                    Ok(_claims) => resource.create(&req, body.into_inner()).await,
                    Err(response) => response,
                }
            }
        }),
    );

    // GET /{id} - Get with role check
    let get_resource = resource.clone_box();
    scope = scope.route(
        "/{id}",
        web::get().to(move |req: HttpRequest, path: web::Path<String>, session: Session, config: web::Data<AdminxConfig>| {
            let resource = get_resource.clone_box();
            async move {
                let id = path.into_inner();
                match check_resource_permission(&session, &config, resource.as_ref(), "view").await {
                    Ok(_claims) => resource.get(&req, id).await,
                    Err(response) => response,
                }
            }
        }),
    );

    // PUT /{id} - Update with role check
    let update_resource = resource.clone_box();
    scope = scope.route(
        "/{id}",
        web::put().to(move |req: HttpRequest, path: web::Path<String>, body: web::Json<Value>, session: Session, config: web::Data<AdminxConfig>| {
            let resource = update_resource.clone_box();
            async move {
                let id = path.into_inner();
                match check_resource_permission(&session, &config, resource.as_ref(), "update").await {
                    Ok(_claims) => resource.update(&req, id, body.into_inner()).await,
                    Err(response) => response,
                }
            }
        }),
    );

    // DELETE /{id} - Delete with role check
    let delete_resource = resource.clone_box();
    scope = scope.route(
        "/{id}",
        web::delete().to(move |req: HttpRequest, path: web::Path<String>, session: Session, config: web::Data<AdminxConfig>| {
            let resource = delete_resource.clone_box();
            async move {
                let id = path.into_inner();
                match check_resource_permission(&session, &config, resource.as_ref(), "delete").await {
                    Ok(_claims) => resource.delete(&req, id).await,
                    Err(response) => response,
                }
            }
        }),
    );

    scope
}