//! Tests for Topaz template system

#[cfg(test)]
mod tests {
    use super::super::topaz_templates::*;
    use std::path::Path;

    #[test]
    fn test_builtin_templates() {
        let templates = BuiltinTemplates::all();
        
        // Verify we have all expected templates
        assert!(templates.contains_key("upscale_to_4k"));
        assert!(templates.contains_key("convert_to_60fps"));
        assert!(templates.contains_key("remove_noise"));
        assert!(templates.contains_key("4x_slow_motion"));
        assert!(templates.contains_key("8x_super_slow_motion"));
        assert!(templates.contains_key("auto_crop_stabilization"));
        assert!(templates.contains_key("deinterlace_upscale_fhd"));
        assert!(templates.contains_key("film_stock_4k_strong"));
        
        // Verify template structure
        let upscale_template = &templates["upscale_to_4k"];
        assert_eq!(upscale_template.name, "放大至4K");
        assert!(upscale_template.settings.enhance.active);
        assert_eq!(upscale_template.settings.enhance.model, "prob-4");
        assert_eq!(upscale_template.settings.output.out_size_method, 7);
    }

    #[test]
    fn test_template_manager() {
        let mut manager = TopazTemplateManager::new();
        
        // Test getting builtin templates
        assert!(manager.get_template("upscale_to_4k").is_some());
        assert!(manager.get_template("nonexistent").is_none());
        
        // Test listing templates
        let template_names = manager.list_templates();
        assert!(!template_names.is_empty());
        assert!(template_names.contains(&"upscale_to_4k".to_string()));
    }

    #[test]
    fn test_template_serialization() {
        let template = BuiltinTemplates::upscale_to_4k();
        
        // Test serialization
        let json = serde_json::to_string_pretty(&template).unwrap();
        assert!(json.contains("放大至4K"));
        assert!(json.contains("prob-4"));
        
        // Test deserialization
        let deserialized: TopazTemplate = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.name, template.name);
        assert_eq!(deserialized.settings.enhance.model, template.settings.enhance.model);
    }

    #[test]
    fn test_template_validation() {
        let manager = TopazTemplateManager::new();
        
        // Test valid template
        let valid_template = BuiltinTemplates::upscale_to_4k();
        assert!(manager.validate_template(&valid_template).is_ok());
        
        // Test invalid template - empty name
        let mut invalid_template = valid_template.clone();
        invalid_template.name = "".to_string();
        assert!(manager.validate_template(&invalid_template).is_err());
        
        // Test invalid enhancement model
        let mut invalid_template = BuiltinTemplates::upscale_to_4k();
        invalid_template.settings.enhance.model = "invalid-model".to_string();
        assert!(manager.validate_template(&invalid_template).is_err());
    }

    #[test]
    fn test_json_loading() {
        let mut manager = TopazTemplateManager::new();
        
        // Test loading valid JSON
        let json = r#"{
            "name": "测试模板",
            "description": "测试描述",
            "author": "Test Author",
            "saveOutputSettings": false,
            "date": "Tue Jul 11 12:00:00 2023 GMT-0500",
            "veaiversion": "3.3.5",
            "editable": false,
            "enabled": true,
            "settings": {
                "stabilize": {
                    "active": false,
                    "smooth": 50,
                    "method": 1,
                    "rsc": false,
                    "reduceMotion": false,
                    "reduceMotionIteration": 2
                },
                "motionblur": {
                    "active": false,
                    "model": "thm-2"
                },
                "slowmo": {
                    "active": false,
                    "model": "apo-8",
                    "factor": 1,
                    "duplicate": true,
                    "duplicateThreshold": 10
                },
                "enhance": {
                    "active": true,
                    "model": "prob-4",
                    "videoType": 1,
                    "auto": 0,
                    "fieldOrder": 0,
                    "compress": 0,
                    "detail": 0,
                    "sharpen": 0,
                    "denoise": 0,
                    "dehalo": 0,
                    "deblur": 0,
                    "addNoise": 0,
                    "recoverOriginalDetailValue": 20,
                    "isArtemis": false,
                    "isGaia": false,
                    "isTheia": false,
                    "isProteus": true,
                    "isIris": false
                },
                "grain": {
                    "active": false,
                    "grain": 5,
                    "grainSize": 2
                },
                "output": {
                    "active": true,
                    "outSizeMethod": 7,
                    "cropToFit": false,
                    "outputPAR": 0,
                    "outFPS": 0
                }
            }
        }"#;
        
        assert!(manager.load_from_json("test_template".to_string(), json).is_ok());
        assert!(manager.get_template("test_template").is_some());
        
        let loaded_template = manager.get_template("test_template").unwrap();
        assert_eq!(loaded_template.name, "测试模板");
        assert!(loaded_template.settings.enhance.active);
        assert_eq!(loaded_template.settings.enhance.model, "prob-4");
    }

    #[test]
    fn test_export_import_roundtrip() {
        let mut manager = TopazTemplateManager::new();
        let original_template = BuiltinTemplates::upscale_to_4k();
        
        // Export to JSON
        manager.add_template("test_export".to_string(), original_template.clone());
        let json = manager.export_to_json("test_export").unwrap();
        
        // Import from JSON
        manager.load_from_json("test_import".to_string(), &json).unwrap();
        let imported_template = manager.get_template("test_import").unwrap();
        
        // Verify they match
        assert_eq!(imported_template.name, original_template.name);
        assert_eq!(imported_template.description, original_template.description);
        assert_eq!(imported_template.settings.enhance.model, original_template.settings.enhance.model);
        assert_eq!(imported_template.settings.output.out_size_method, original_template.settings.output.out_size_method);
    }
}
