#!/usr/bin/env python3
"""
Test script to verify that new projects get global configurations applied
"""

import sys
import os
from pathlib import Path

# Add src to path so we can import the modules
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))

from models.config_model import Config
from services.project_discovery_service import ProjectDiscoveryService
from models.project_model import ProjectManager
import tempfile


def test_new_project_global_config():
    """Test that new projects get global configurations applied"""

    # Create a temporary config
    config = Config()

    # Create project manager
    project_manager = ProjectManager()

    # Create discovery service
    discovery_service = ProjectDiscoveryService(config, project_manager)

    # Test manual project creation
    print("Testing manual project creation...")
    obs_dir = {
        "path": "D:\\Test\\TestProject",
        "type": "manual",
        "description": "Test project",
    }

    # Create the manual project
    manual_project = discovery_service._create_manual_project(obs_dir)

    if manual_project:
        print("✅ Manual project created successfully")

        # Check if global configurations were applied
        schedule_config_keys = list(manual_project.to_dict()["schedule_config"].keys())
        has_backup_options = "backup_options" in manual_project.to_dict()

        print(f"Schedule config keys: {schedule_config_keys}")
        print(f"Has backup_options: {has_backup_options}")

        # Verify specific global settings
        project_dict = manual_project.to_dict()
        schedule_config = project_dict["schedule_config"]
        backup_options = project_dict.get("backup_options", {})

        print(
            f"Retry delay hours: {schedule_config.get('retry_delay_hours', 'MISSING')}"
        )
        print(
            f"Backup timeout: {schedule_config.get('backup_timeout_minutes', 'MISSING')}"
        )
        print(
            f"Min backup interval: {schedule_config.get('min_backup_interval_minutes', 'MISSING')}"
        )
        print(f"Min free space: {schedule_config.get('min_free_space_mb', 'MISSING')}")
        print(
            f"Compression level: {backup_options.get('compression_level', 'MISSING')}"
        )
        print(
            f"Include subdirectories: {backup_options.get('include_subdirectories', 'MISSING')}"
        )

        # Check if all expected keys are present
        expected_schedule_keys = [
            "schedule",
            "schedule_time",
            "enabled",
            "next_scheduled_backup",
            "retry_delay_hours",
            "backup_timeout_minutes",
            "min_backup_interval_minutes",
            "min_free_space_mb",
        ]

        expected_backup_keys = [
            "compression_level",
            "include_subdirectories",
            "preserve_directory_structure",
            "hash_algorithm",
            "hash_includes",
            "backup_type",
            "process_priority",
            "sequential_execution",
            "filename_format",
            "date_format",
        ]

        missing_schedule_keys = [
            key for key in expected_schedule_keys if key not in schedule_config
        ]
        missing_backup_keys = [
            key for key in expected_backup_keys if key not in backup_options
        ]

        if not missing_schedule_keys and not missing_backup_keys:
            print("✅ ALL global configurations successfully applied to new project!")
        else:
            print(f"❌ Missing schedule keys: {missing_schedule_keys}")
            print(f"❌ Missing backup keys: {missing_backup_keys}")

    else:
        print("❌ Failed to create manual project")


if __name__ == "__main__":
    test_new_project_global_config()
