#!/usr/bin/env python3
"""
Test script to verify asset loading fix and version handling
"""

import os
import pytest
from pyPhotoAlbum.project_serializer import load_from_zip
from pyPhotoAlbum.models import ImageData


# Path to test file - this is a real file that may or may not exist
TEST_FILE = "/home/dtourolle/Nextcloud/Photo Gallery/gr58/Album_pytool.ppz"


@pytest.mark.skipif(not os.path.exists(TEST_FILE), reason=f"Test file not found: {TEST_FILE}")
def test_asset_loading_from_real_file():
    """Test asset loading from a real project file (if it exists)"""
    # Load project
    project = load_from_zip(TEST_FILE)

    assert project is not None, "Failed to load project"
    assert project.name is not None, "Project has no name"
    assert project.folder_path is not None, "Project has no folder path"
    assert project.asset_manager.assets_folder is not None, "Project has no assets folder"

    # Count assets
    total_assets = 0
    missing_assets = 0
    found_assets = 0

    for page in project.pages:
        for element in page.layout.elements:
            if isinstance(element, ImageData) and element.image_path:
                total_assets += 1

                # Check if asset exists
                if os.path.isabs(element.image_path):
                    full_path = element.image_path
                else:
                    full_path = os.path.join(project.folder_path, element.image_path)

                if os.path.exists(full_path):
                    found_assets += 1
                else:
                    missing_assets += 1
                    print(f"Missing asset: {element.image_path}")

    # Report results
    print(f"\nResults:")
    print(f"  Total assets: {total_assets}")
    print(f"  Found: {found_assets}")
    print(f"  Missing: {missing_assets}")

    # The test passes as long as we can load the project
    # Missing assets are acceptable (they might be on a different machine)
    assert total_assets >= 0, "Should have counted assets"
