// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/

// Standard library imports
use std::error::Error;
use std::fs::{self, File};
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::env;
use std::sync::Arc;
use std::time::Duration;

// Serde for serialization/deserialization
use serde::{Serialize, Deserialize};

// Tauri related imports
use tauri::{command, Emitter, AppHandle, Manager, State, Window};

// Internal modules
pub mod models;
pub mod services;
pub mod commands;
pub mod crawlers;
pub mod platform_matcher;

// Import our models
pub use models::server::{ServerType, ServerInfo, ScanResult, ScanProgress};
pub use models::plugin::{Plugin, PluginMeta};
pub use models::repository::{RepositorySource, RepositoryPlugin, PotentialPluginMatch};

// Import our services
pub use services::http::HttpClient;
pub use services::plugin_scanner::{scan_server_directory, perform_scan, extract_plugin_metadata, calculate_file_hash, is_file_locked};
pub use services::update_manager::{check_for_plugin_updates, check_single_plugin_update, backup_plugin, replace_plugin, normalize_version, compare_plugin_versions};

// Import our commands
pub use commands::plugin_commands::*;
pub use commands::scan_commands::*;

// Import our crawlers
pub use crawlers::HangarCrawler;
pub use crawlers::SpigotMCCrawler;
pub use crawlers::ModrinthCrawler;
pub use crawlers::GitHubCrawler;

// Import platform matchers
pub use platform_matcher::{get_compatible_modrinth_loaders, is_version_compatible_with_server};

/// Search for plugins in specified repositories
pub async fn lib_search_plugins_in_repositories(query: &str, repositories: Vec<RepositorySource>) -> Result<Vec<RepositoryPlugin>, String> {
    // Implementation details to be moved from original lib.rs
    Ok(Vec::new()) // Placeholder
}

/// Get plugin details from a repository
pub async fn lib_get_plugin_details_from_repository(
    plugin_id: &str,
    repository: RepositorySource,
    server_type: Option<&ServerType>
) -> Result<RepositoryPlugin, String> {
    // Implementation details to be moved from original lib.rs
    Ok(RepositoryPlugin {
        id: plugin_id.to_string(),
        name: "Example Plugin".to_string(),
        version: "1.0.0".to_string(),
        description: Some("This is a placeholder".to_string()),
        authors: vec!["Example Author".to_string()],
        download_url: "https://example.com".to_string(),
        repository: repository,
        page_url: "https://example.com".to_string(),
        download_count: Some(0),
        last_updated: Some("2023-01-01".to_string()),
        icon_url: None,
        minecraft_versions: vec!["1.19.2".to_string()],
        categories: vec![],
        rating: None,
        file_size: None,
        file_hash: None,
        changelog: None,
    }) // Placeholder
}

/// Download a plugin from a repository
pub async fn lib_download_plugin_from_repository(
    plugin_id: &str,
    version: &str,
    repository: RepositorySource,
    destination: &str,
    server_type: Option<&ServerType>
) -> Result<String, String> {
    // Implementation details to be moved from original lib.rs
    Ok(destination.to_string()) // Placeholder
}

/// Search for plugin variations
pub async fn search_with_variations(plugin_name: &str, repositories: &[RepositorySource]) -> Result<Vec<RepositoryPlugin>, String> {
    // Implementation details to be moved from original lib.rs
    Ok(Vec::new()) // Placeholder
}

/// Configure and run the Tauri application
pub fn run() {
    // Build the Tauri application
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![
            // Plugin discovery commands
            scan_server_dir,
            scan_server_dir_sync,

            // Plugin repository commands
            search_plugins,
            get_plugin_details,

            // Other commands to be added
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
