rustisan 0.0.1

Command-line interface for the Rustisan web framework
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
1024
1025
1026
1027
1028
//! New command for creating Rustisan projects
//!
//! This command creates a new Rustisan application with the proper structure.

use anyhow::Result;
use colored::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use super::CommandUtils;

/// Handle the new command
pub async fn handle(name: String, path: Option<String>, template: Option<String>, git: bool) -> Result<()> {
    // Determine the project path
    let project_path = if let Some(p) = path {
        PathBuf::from(p).join(&name)
    } else {
        PathBuf::from(&name)
    };

    // Check if directory already exists
    if project_path.exists() {
        anyhow::bail!("Directory '{}' already exists", project_path.display());
    }

    CommandUtils::info(&format!("Creating new Rustisan application '{}'...", name));

    // Create project directory
    fs::create_dir_all(&project_path)?;

    // Create project structure
    create_project_structure(&project_path, &name, template.as_deref()).await?;

    // Initialize git repository if requested
    if git {
        initialize_git(&project_path)?;
    }

    CommandUtils::success(&format!("Successfully created Rustisan application '{}'", name));

    println!("\n{}", "Next steps:".bold());
    println!("  cd {}", name);
    println!("  rustisan config:generate-key  # Generate application key");
    println!("  # Configure your database in rustisan.toml");
    println!("  rustisan migrate           # Run database migrations");
    println!("  rustisan serve             # Start development server");

    Ok(())
}

/// Create the project structure
async fn create_project_structure(path: &Path, name: &str, template: Option<&str>) -> Result<()> {
    // Create main Cargo.toml
    create_main_cargo_toml(path, name)?;

    // Create rustisan.toml configuration
    create_rustisan_config(path)?;

    // Create .gitignore
    create_gitignore(path)?;

    // Create src directory structure
    create_src_structure(path)?;

    // Create other directories
    create_directory_structure(path)?;

    // Create main.rs
    create_main_rs(path, name)?;

    // Create README.md
    create_readme(path, name)?;

    // Apply template if specified
    if let Some(template_name) = template {
        apply_template(path, template_name).await?;
    }

    Ok(())
}

/// Create main Cargo.toml
fn create_main_cargo_toml(path: &Path, name: &str) -> Result<()> {
    let cargo_toml = format!(r#"[package]
name = "{}"
version = "0.1.0"
edition = "2024"
authors = ["Your Name <your.email@example.com>"]
description = "A Rustisan web application"

[dependencies]
rustisan-core = "0.0.1"

tokio = {{ version = "1.0", features = ["full"] }}
serde_json = "1.0"
tracing = "0.1"
chrono = {{ version = "0.4", features = ["serde"] }}

[dev-dependencies]
tokio-test = "0.4"

[[bin]]
name = "{}"
path = "src/main.rs"

"#, name, name);

    fs::write(path.join("Cargo.toml"), cargo_toml)?;
    Ok(())
}

/// Create rustisan.toml configuration
fn create_rustisan_config(path: &Path) -> Result<()> {
    let config = r#"[app]
name = "Rustisan App"
env = "development"
debug = true
url = "http://localhost:3000"
timezone = "UTC"
locale = "en"
key = ""
cors_enabled = true

[server]
host = "127.0.0.1"
port = 3000
timeout = 60
max_connections = 1000
https_enabled = false

[database]
default = "default"

[database.connections.default]
driver = "mysql"
host = "localhost"
port = 3306
database = "rustisan_app"
username = "root"
password = ""
charset = "utf8mb4"
pool_min = 1
pool_max = 10
timeout = 30

[cache]
default = "memory"
ttl = 3600

[session]
driver = "cookie"
lifetime = 120
expire_on_close = false
encrypt = true
cookie_name = "rustisan_session"
cookie_path = "/"
cookie_secure = false
cookie_http_only = true

[logging]
level = "info"
default = "console"

# Additional configuration sections can be added here
# For example:
# [mail]
# driver = "smtp"
# host = "localhost"
# port = 587
# encryption = "tls"
# username = ""
# password = ""

# [redis]
# host = "localhost"
# port = 6379
# password = ""
# database = 0

# [api]
# rate_limit_enabled = true
# rate_limit_max_requests = 60
# rate_limit_window = 60
# default_version = "v1"
# prefix = "api"

"#;

    fs::write(path.join("rustisan.toml"), config)?;
    Ok(())
}



/// Create .gitignore
fn create_gitignore(path: &Path) -> Result<()> {
    let gitignore = r#"# Rust
/target/
Cargo.lock

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Environment
# No .env files needed - configuration is in rustisan.toml

# Logs
*.log
logs/

# Database
*.db
*.sqlite
*.sqlite3

# Cache
storage/cache/
storage/sessions/
storage/logs/

# Build artifacts
dist/
build/
"#;

    fs::write(path.join(".gitignore"), gitignore)?;
    Ok(())
}

/// Create src directory structure
fn create_src_structure(path: &Path) -> Result<()> {
    let src_path = path.join("src");
    fs::create_dir_all(&src_path)?;

    // Create subdirectories
    fs::create_dir_all(src_path.join("controllers"))?;
    fs::create_dir_all(src_path.join("models"))?;
    fs::create_dir_all(src_path.join("middleware"))?;
    fs::create_dir_all(src_path.join("requests"))?;
    fs::create_dir_all(src_path.join("resources"))?;
    fs::create_dir_all(src_path.join("services"))?;
    fs::create_dir_all(src_path.join("jobs"))?;
    fs::create_dir_all(src_path.join("events"))?;
    fs::create_dir_all(src_path.join("listeners"))?;

    // Create module files
    create_controllers_module(&src_path)?;
    fs::write(src_path.join("models").join("mod.rs"), "//! Application models\n")?;
    fs::write(src_path.join("middleware").join("mod.rs"), "//! Application middleware\n")?;
    fs::write(src_path.join("requests").join("mod.rs"), "//! Form request validators\n")?;
    fs::write(src_path.join("resources").join("mod.rs"), "//! API resources\n")?;
    fs::write(src_path.join("services").join("mod.rs"), "//! Application services\n")?;
    fs::write(src_path.join("jobs").join("mod.rs"), "//! Background jobs\n")?;
    fs::write(src_path.join("events").join("mod.rs"), "//! Application events\n")?;
    fs::write(src_path.join("listeners").join("mod.rs"), "//! Event listeners\n")?;

    // Create routes.rs file
    fs::write(src_path.join("routes.rs"), "\n")?;

    Ok(())
}

/// Create other directory structure
fn create_directory_structure(path: &Path) -> Result<()> {
    // Database directories
    fs::create_dir_all(path.join("database").join("migrations"))?;
    fs::create_dir_all(path.join("database").join("seeders"))?;
    fs::create_dir_all(path.join("database").join("factories"))?;

    // Storage directories
    fs::create_dir_all(path.join("storage").join("logs"))?;
    fs::create_dir_all(path.join("storage").join("cache"))?;
    fs::create_dir_all(path.join("storage").join("sessions"))?;
    fs::create_dir_all(path.join("storage").join("uploads"))?;

    // Resources directories
    fs::create_dir_all(path.join("resources").join("views"))?;
    fs::create_dir_all(path.join("resources").join("assets"))?;

    // Config directory
    fs::create_dir_all(path.join("config"))?;

    // Tests directory
    fs::create_dir_all(path.join("tests").join("unit"))?;
    fs::create_dir_all(path.join("tests").join("integration"))?;

    // Routes directory
    fs::create_dir_all(path.join("routes"))?;

    Ok(())
}

/// Create controllers module with UserController
fn create_controllers_module(src_path: &Path) -> Result<()> {
    let controllers_path = src_path.join("controllers");

    // Create controllers/mod.rs
    let controllers_mod = r#"//! Application controllers

pub mod user_controller;

pub use user_controller::UserController;
"#;
    fs::write(controllers_path.join("mod.rs"), controllers_mod)?;

    // Create controllers/user_controller.rs
    let user_controller = r#"//! User controller for handling user-related HTTP requests
//!
//! This controller demonstrates the Laravel-like structure for handling
//! user operations in a Rustisan application.

use std::sync::Arc;
use serde_json::json;
use rustisan_core::{Request, Response, Result};

/// UserController handles all user-related HTTP requests
pub struct UserController;

impl UserController {
    /// Creates a new UserController instance
    pub fn new() -> Self {
        Self
    }

    /// Display a listing of users (GET /users)
    pub async fn index(&self) -> Result<Response> {
        // In a real application, this would fetch users from a database
        let users = vec![
            json!({
                "id": 1,
                "name": "John Doe",
                "email": "john@example.com",
                "created_at": "2024-01-01T00:00:00Z"
            }),
            json!({
                "id": 2,
                "name": "Jane Smith",
                "email": "jane@example.com",
                "created_at": "2024-01-02T00:00:00Z"
            }),
            json!({
                "id": 3,
                "name": "Bob Johnson",
                "email": "bob@example.com",
                "created_at": "2024-01-03T00:00:00Z"
            })
        ];

        Response::json(json!({
            "users": users,
            "count": users.len(),
            "message": "Users retrieved successfully"
        }))
    }

    /// Show a specific user (GET /users/{id})
    pub async fn show(&self, id: u32) -> Result<Response> {
        // In a real application, this would fetch the user from a database
        let user = json!({
            "id": id,
            "name": "User Name",
            "email": format!("user{}@example.com", id),
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z"
        });

        Response::json(json!({
            "user": user,
            "message": format!("User {} retrieved successfully", id)
        }))
    }

    /// Store a new user (POST /users)
    pub async fn store(&self, _request: Request) -> Result<Response> {
        // In a real application, this would validate the request and save to database

        // For demonstration, we'll just return a success response
        let new_user = json!({
            "id": 999,
            "name": "New User",
            "email": "newuser@example.com",
            "created_at": chrono::Utc::now().to_rfc3339(),
            "updated_at": chrono::Utc::now().to_rfc3339()
        });

        Response::created(json!({
            "user": new_user,
            "message": "User created successfully"
        }))
    }

    /// Update an existing user (PUT /users/{id})
    pub async fn update(&self, id: u32, _request: Request) -> Result<Response> {
        // In a real application, this would validate the request and update the database

        let updated_user = json!({
            "id": id,
            "name": "Updated User Name",
            "email": format!("updated{}@example.com", id),
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": chrono::Utc::now().to_rfc3339()
        });

        Response::json(json!({
            "user": updated_user,
            "message": format!("User {} updated successfully", id)
        }))
    }

    /// Delete a user (DELETE /users/{id})
    pub async fn destroy(&self, id: u32) -> Result<Response> {
        // In a real application, this would delete the user from the database

        Response::json(json!({
            "message": format!("User {} deleted successfully", id),
            "deleted_id": id
        }))
    }

    /// Get user statistics (GET /users/stats)
    pub async fn stats(&self) -> Result<Response> {
        let stats = json!({
            "total_users": 150,
            "active_users": 120,
            "inactive_users": 30,
            "users_created_today": 5,
            "users_created_this_week": 25,
            "users_created_this_month": 100
        });

        Response::json(json!({
            "statistics": stats,
            "message": "User statistics retrieved successfully"
        }))
    }

    /// Search users (GET /users/search?q=term)
    pub async fn search(&self, _request: Request) -> Result<Response> {
        // In a real application, this would search the database
        let search_term = "sample"; // In a real implementation, extract from request

        let results = vec![
            json!({
                "id": 1,
                "name": "John Doe",
                "email": "john@example.com",
                "relevance": 0.95
            }),
            json!({
                "id": 2,
                "name": "Jane Smith",
                "email": "jane@example.com",
                "relevance": 0.85
            })
        ];

        Response::json(json!({
            "results": results,
            "search_term": search_term,
            "count": results.len(),
            "message": "Search completed successfully"
        }))
    }
}

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

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

    #[tokio::test]
    async fn test_user_controller_index() {
        let controller = UserController::new();
        let response = controller.index().await;
        assert!(response.is_ok());
    }

    #[tokio::test]
    async fn test_user_controller_show() {
        let controller = UserController::new();
        let response = controller.show(1).await;
        assert!(response.is_ok());
    }

    #[tokio::test]
    async fn test_user_controller_stats() {
        let controller = UserController::new();
        let response = controller.stats().await;
        assert!(response.is_ok());
    }
}
"#;
    fs::write(controllers_path.join("user_controller.rs"), user_controller)?;

    Ok(())
}

/// Create main.rs
fn create_main_rs(path: &Path, name: &str) -> Result<()> {
    let main_rs = format!(r#"//! {} - A Rustisan web application
//!
//! This is the main entry point for the application.

use std::net::SocketAddr;
use std::sync::Arc;

use rustisan_core::{{
    app::Application,
    config::Config,
    init_logging,
    routing::create_success_response,
    Response, Result,
}};
use serde_json::json;
use tracing::{{error, info, warn}};

mod controllers;
mod routes;

use controllers::UserController;

#[tokio::main]
async fn main() -> Result<()> {{
    // Initialize logging
    init_logging();
    info!("🚀 Starting {} Application...");

    // Create application with configuration
    let mut app = create_application().await?;

    // Register all routes
    register_routes(&mut app).await?;

    // Start the server
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    info!("🌐 Server starting on http://{{}}", addr);
    info!("📚 Available endpoints:");
    print_available_routes();

    // Serve the application
    if let Err(e) = app.serve(addr).await {{
        error!("❌ Server error: {{}}", e);
        return Err(e);
    }}

    Ok(())
}}

/// Creates and configures the Rustisan application
async fn create_application() -> Result<Application> {{
    info!("🔧 Configuring Rustisan application...");

    // Load configuration
    let mut config = Config::default();
    config.app_name = "{} Application".to_string();
    config.app_env = "development".to_string();
    config.app_debug = true;

    // Create application
    let mut app = Application::with_config(config);

    // Set application state
    app.set_state("version", "0.1.0");
    app.set_state("author", "Rustisan Team");
    app.set_state("description", "A {} application built with Rustisan framework");

    info!("✅ Application configured successfully");
    Ok(app)
}}

/// Registers all application routes
async fn register_routes(app: &mut Application) -> Result<()> {{
    info!("📍 Registering application routes...");

    let router = app.router();

    // Basic routes
    router.get("/", || async {{
        create_success_response(json!({{
            "message": "Welcome to {} Application!",
            "version": "0.1.0",
            "status": "success",
            "framework": "Rustisan",
            "inspired_by": "Laravel",
            "documentation": "Visit /docs for API documentation",
            "health_check": "Visit /health for health status",
            "timestamp": chrono::Utc::now()
        }})).unwrap_or_else(|_| Response::internal_error("Failed to create response").unwrap())
    }});

    // Health check route
    router.get("/health", || async {{
        create_success_response(json!({{
            "status": "healthy",
            "service": "{}",
            "version": "0.1.0",
            "uptime": "running",
            "timestamp": chrono::Utc::now(),
            "checks": {{
                "database": "ok",
                "memory": "ok",
                "disk": "ok"
            }}
        }})).unwrap_or_else(|_| Response::internal_error("Health check failed").unwrap())
    }});

    // Documentation route
    router.get("/docs", || async {{
        create_success_response(json!({{
            "documentation": {{
                "title": "{} API Documentation",
                "version": "1.0.0",
                "description": "A comprehensive API built with the Rustisan framework",
                "framework": "Rustisan (inspired by Laravel)",
                "endpoints": {{
                    "basic": {{
                        "GET /": "Welcome message and application info",
                        "GET /health": "Application health check",
                        "GET /docs": "This documentation"
                    }},
                    "users": {{
                        "GET /users": "List all users",
                        "GET /users/:id": "Get specific user by ID",
                        "POST /users": "Create a new user",
                        "PUT /users/:id": "Update existing user",
                        "DELETE /users/:id": "Delete user",
                        "GET /users/stats": "Get user statistics"
                    }},
                    "api_v1": {{
                        "description": "All user endpoints are also available with /api/v1 prefix",
                        "base_url": "/api/v1",
                        "GET /api/v1/status": "API status information"
                    }}
                }},
                "response_format": {{
                    "success": {{
                        "structure": "{{ data: any, message?: string }}",
                        "example": {{
                            "data": {{"key": "value"}},
                            "message": "Operation successful"
                        }}
                    }},
                    "error": {{
                        "structure": "{{ error: {{ code: string, message: string, status: number }} }}",
                        "example": {{
                            "error": {{
                                "code": "NOT_FOUND",
                                "message": "Resource not found",
                                "status": 404
                            }}
                        }}
                    }}
                }}
            }}
        }})).unwrap_or_else(|_| Response::internal_error("Failed to generate documentation").unwrap())
    }});

    // Create controller instance
    let controller = Arc::new(UserController::new());

    // User routes - demonstrating Laravel-like resource routing
    register_user_routes(router, controller.clone()).await?;

    // API v1 routes - demonstrating route groups
    register_api_routes(router, controller).await?;

    info!("✅ All routes registered successfully");
    Ok(())
}}

/// Registers user-related routes
async fn register_user_routes(
    router: &mut rustisan_core::routing::Router,
    controller: Arc<UserController>,
) -> Result<()> {{
    info!("📍 Registering user routes...");

    // GET /users - List all users
    {{
        let ctrl = controller.clone();
        router.get("/users", move || {{
            let controller = ctrl.clone();
            async move {{
                match controller.index().await {{
                    Ok(response) => response,
                    Err(_) => Response::internal_error("Failed to fetch users").unwrap()
                }}
            }}
        }});
    }}

    // GET /users/:id - Get specific user
    {{
        let ctrl = controller.clone();
        router.get("/users/:id", move || {{
            let controller = ctrl.clone();
            async move {{
                match controller.show(1).await {{
                    Ok(response) => response,
                    Err(_) => Response::internal_error("Failed to fetch user").unwrap()
                }}
            }}
        }});
    }}

    // GET /users/stats - User statistics
    {{
        let ctrl = controller.clone();
        router.get("/users/stats", move || {{
            let controller = ctrl.clone();
            async move {{
                match controller.stats().await {{
                    Ok(response) => response,
                    Err(_) => Response::internal_error("Failed to fetch user statistics").unwrap()
                }}
            }}
        }});
    }}

    Ok(())
}}

/// Registers API v1 routes using route groups
async fn register_api_routes(
    router: &mut rustisan_core::routing::Router,
    controller: Arc<UserController>,
) -> Result<()> {{
    info!("📍 Registering API v1 routes...");

    router.group("/api/v1", |group| {{
        // API status endpoint
        group.get("/status", move || async {{
            create_success_response(json!({{
                "api": {{
                    "name": "{} API",
                    "version": "v1",
                    "status": "active",
                    "uptime": "running",
                    "framework": "Rustisan",
                    "endpoints": {{
                        "users": [
                            "GET /api/v1/users - List users",
                            "GET /api/v1/users/:id - Get user",
                            "POST /api/v1/users - Create user",
                            "PUT /api/v1/users/:id - Update user",
                            "DELETE /api/v1/users/:id - Delete user"
                        ]
                    }},
                    "features": [
                        "RESTful API design",
                        "JSON responses",
                        "Error handling",
                        "Request validation",
                        "Route grouping"
                    ],
                    "timestamp": chrono::Utc::now()
                }}
            }})).unwrap_or_else(|_| Response::internal_error("Failed to get API status").unwrap())
        }});

        // API user endpoints
        let api_controller = controller.clone();
        {{
            let ctrl = api_controller.clone();
            group.get("/users", move || {{
                let controller = ctrl.clone();
                async move {{
                    match controller.index().await {{
                        Ok(response) => response,
                        Err(_) => Response::internal_error("Failed to fetch users via API").unwrap()
                    }}
                }}
            }});
        }}

        {{
            let ctrl = api_controller.clone();
            group.get_with_id("/users/:id", move |id| {{
                let controller = ctrl.clone();
                async move {{
                    match controller.show(id).await {{
                        Ok(response) => response,
                        Err(_) => Response::internal_error("Failed to fetch user via API").unwrap()
                    }}
                }}
            }});
        }}
    }});

    Ok(())
}}

/// Prints available routes for user reference
fn print_available_routes() {{
    println!("   📋 Route List:");
    println!("   ┌─────────────────────────────────────────────────────────────┐");
    println!("   │ METHOD │ PATH                    │ DESCRIPTION              │");
    println!("   ├─────────────────────────────────────────────────────────────┤");
    println!("   │ GET    │ /                       │ Welcome & app info       │");
    println!("   │ GET    │ /health                 │ Health check             │");
    println!("   │ GET    │ /docs                   │ API documentation        │");
    println!("   │ GET    │ /users                  │ List all users           │");
    println!("   │ GET    │ /users/:id              │ Get user by ID           │");
    println!("   │ GET    │ /users/stats            │ User statistics          │");
    println!("   │ GET    │ /api/v1/status          │ API status               │");
    println!("   │ GET    │ /api/v1/users           │ List users (API)         │");
    println!("   │ GET    │ /api/v1/users/:id       │ Get user (API)           │");
    println!("   └─────────────────────────────────────────────────────────────┘");
    println!();
    println!("   🔗 Quick Links:");
    println!("   • Application: http://127.0.0.1:3000/");
    println!("   • Health Check: http://127.0.0.1:3000/health");
    println!("   • Documentation: http://127.0.0.1:3000/docs");
    println!("   • Users API: http://127.0.0.1:3000/users");
    println!("   • API Status: http://127.0.0.1:3000/api/v1/status");
    println!();
}}

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

    #[tokio::test]
    async fn test_create_application() {{
        let app = create_application().await;
        assert!(app.is_ok());

        let app = app.unwrap();
        assert_eq!(app.config().app_name, "{} Application");
        assert!(app.config().is_development());
    }}

    #[tokio::test]
    async fn test_register_routes() {{
        let mut app = create_application().await.unwrap();
        let result = register_routes(&mut app).await;
        assert!(result.is_ok());
    }}

    #[test]
    fn test_print_routes() {{
        // Test that the function doesn't panic
        print_available_routes();
    }}
}}
"#, name, name, name, name, name, name, name, name, name);

    fs::write(path.join("src").join("main.rs"), main_rs)?;
    Ok(())
}

/// Create README.md
fn create_readme(path: &Path, name: &str) -> Result<()> {
    let readme = format!(r#"# {}

A web application built with Rustisan, a Laravel-inspired web framework for Rust.

## Getting Started

### Prerequisites

- Rust 1.70+ with 2024 edition support
- MySQL or PostgreSQL database

### Installation

1. Clone this repository
2. Configure your environment in `rustisan.toml`
3. Install dependencies:
   ```bash
   cargo build
   ```

### Running the Application

```bash
# Development server
rustisan serve

# Or with cargo
cargo run
```

The application will be available at `http://localhost:3000`.

### Database Setup

```bash
# Create database
rustisan db:create

# Run migrations
rustisan migrate

# Seed database (optional)
rustisan seed
```

### Available Commands

```bash
# Generate a controller
rustisan make:controller UserController

# Generate a model
rustisan make:model User --migration

# Generate a migration
rustisan make:migration create_users_table

# View all routes
rustisan route:list

# Run tests
rustisan test
```

## Project Structure

```
src/
├── controllers/     # HTTP controllers
├── models/         # Data models
├── middleware/     # HTTP middleware
├── requests/       # Form request validators
├── resources/      # API resources
├── services/       # Business logic services
├── jobs/          # Background jobs
├── events/        # Application events
└── listeners/     # Event listeners

database/
├── migrations/    # Database migrations
├── seeders/      # Database seeders
└── factories/    # Model factories

routes/           # Route definitions
resources/        # Views and assets
storage/         # Application storage
tests/           # Test files
```

## Documentation

For more information about Rustisan, visit: [https://github.com/lugotardo/rustisan](https://github.com/lugotardo/rustisan)

## License

This project is licensed under the MIT License.
"#, name);

    fs::write(path.join("README.md"), readme)?;
    Ok(())
}

/// Initialize git repository
fn initialize_git(path: &Path) -> Result<()> {
    CommandUtils::info("Initializing git repository...");

    Command::new("git")
        .args(&["init"])
        .current_dir(path)
        .output()?;

    Command::new("git")
        .args(&["add", "."])
        .current_dir(path)
        .output()?;

    Command::new("git")
        .args(&["commit", "-m", "Initial commit"])
        .current_dir(path)
        .output()?;

    Ok(())
}

/// Apply a project template
async fn apply_template(path: &Path, template: &str) -> Result<()> {
    CommandUtils::info(&format!("Applying template '{}'...", template));

    match template {
        "api" => apply_api_template(path).await?,
        "web" => apply_web_template(path).await?,
        "minimal" => apply_minimal_template(path).await?,
        _ => {
            CommandUtils::warning(&format!("Unknown template '{}', using default", template));
        }
    }

    Ok(())
}

/// Apply API template
async fn apply_api_template(_path: &Path) -> Result<()> {
    // Add API-specific configuration and files
    CommandUtils::info("API template applied");
    Ok(())
}

/// Apply web template
async fn apply_web_template(_path: &Path) -> Result<()> {
    // Add web-specific configuration and files
    CommandUtils::info("Web template applied");
    Ok(())
}

/// Apply minimal template
async fn apply_minimal_template(_path: &Path) -> Result<()> {
    // Apply minimal configuration
    CommandUtils::info("Minimal template applied");
    Ok(())
}