use tauri::{
    menu::{MenuBuilder, MenuItemBuilder},
    AppHandle,
};

pub fn create_menu() -> Menu {
    let quit = MenuItemBuilder::with_id("quit", "Quit")
        .shortcut("CmdOrCtrl+Q")
        .build()
        .expect("Failed to create quit menu item");

    let preferences = MenuItemBuilder::with_id("preferences", "Preferences")
        .shortcut("CmdOrCtrl+,")
        .build()
        .expect("Failed to create preferences menu item");

    let check_updates = MenuItemBuilder::with_id("check_updates", "Check for Updates")
        .build()
        .expect("Failed to create check updates menu item");

    #[cfg(target_os = "macos")]
    let app_name = std::env::var("TAURI_APP_NAME").unwrap_or_else(|_| "FrenoCorp".to_string());

    #[cfg(target_os = "macos")]
    let app_menu = MenuBuilder::new(&app_name)
        .item(&quit)
        .separator()
        .item(&preferences)
        .separator()
        .item(&check_updates)
        .build()
        .expect("Failed to create app menu");

    let file_menu = MenuBuilder::new("File")
        .item(&MenuItemBuilder::with_id("new", "New").shortcut("CmdOrCtrl+N").build().unwrap())
        .item(&MenuItemBuilder::with_id("open", "Open").shortcut("CmdOrCtrl+O").build().unwrap())
        .separator()
        .item(&MenuItemBuilder::with_id("save", "Save").shortcut("CmdOrCtrl+S").build().unwrap())
        .item(&MenuItemBuilder::with_id("save_as", "Save As").shortcut("CmdOrCtrl+Shift+S").build().unwrap())
        .separator()
        .item(&quit)
        .build()
        .expect("Failed to create file menu");

    let edit_menu = MenuBuilder::new("Edit")
        .item(&MenuItemBuilder::with_id("undo", "Undo").shortcut("CmdOrCtrl+Z").build().unwrap())
        .item(&MenuItemBuilder::with_id("redo", "Redo").shortcut("CmdOrCtrl+Shift+Z").build().unwrap())
        .separator()
        .item(&MenuItemBuilder::with_id("cut", "Cut").shortcut("CmdOrCtrl+X").build().unwrap())
        .item(&MenuItemBuilder::with_id("copy", "Copy").shortcut("CmdOrCtrl+C").build().unwrap())
        .item(&MenuItemBuilder::with_id("paste", "Paste").shortcut("CmdOrCtrl+V").build().unwrap())
        .separator()
        .item(&MenuItemBuilder::with_id("select_all", "Select All").shortcut("CmdOrCtrl+A").build().unwrap())
        .build()
        .expect("Failed to create edit menu");

    let view_menu = MenuBuilder::new("View")
        .item(&MenuItemBuilder::with_id("zoom_in", "Zoom In").shortcut("CmdOrCtrl+Plus").build().unwrap())
        .item(&MenuItemBuilder::with_id("zoom_out", "Zoom Out").shortcut("CmdOrCtrl+Minus").build().unwrap())
        .item(&MenuItemBuilder::with_id("reset_zoom", "Reset Zoom").shortcut("CmdOrCtrl+0").build().unwrap())
        .separator()
        .item(&MenuItemBuilder::with_id("fullscreen", "Toggle Fullscreen").shortcut("F11").build().unwrap())
        .build()
        .expect("Failed to create view menu");

    let window_menu = MenuBuilder::new("Window")
        .item(&MenuItemBuilder::with_id("minimize", "Minimize").shortcut("CmdOrCtrl+M").build().unwrap())
        .item(&MenuItemBuilder::with_id("close", "Close").shortcut("CmdOrCtrl+W").build().unwrap())
        .separator()
        .item(&MenuItemBuilder::with_id("always_on_top", "Always on Top").build().unwrap())
        .build()
        .expect("Failed to create window menu");

    let help_menu = MenuBuilder::new("Help")
        .item(&MenuItemBuilder::with_id("documentation", "Documentation").shortcut("F1").build().unwrap())
        .item(&MenuItemBuilder::with_id("about", "About").build().unwrap())
        .separator()
        .item(&check_updates)
        .build()
        .expect("Failed to create help menu");

    #[cfg(target_os = "macos")]
    let menu = MenuBuilder::new("Main")
        .item(&app_menu)
        .item(&file_menu)
        .item(&edit_menu)
        .item(&view_menu)
        .item(&window_menu)
        .item(&help_menu)
        .build()
        .expect("Failed to create menu");

    #[cfg(not(target_os = "macos"))]
    let menu = MenuBuilder::new("Main")
        .item(&file_menu)
        .item(&edit_menu)
        .item(&view_menu)
        .item(&window_menu)
        .item(&help_menu)
        .build()
        .expect("Failed to create menu");

    menu
}
