#!/usr/bin/env python3
"""
Deployment Validation Script for Issue #146: Asset Management Implementation

This script validates that the asset management system is ready for production deployment
by running comprehensive tests and checks.
"""

import sys
import time
import tempfile
import shutil
from pathlib import Path
from typing import List, Dict, Any

def main():
    """Run comprehensive deployment validation."""
    print("🚀 MarkiTect Asset Management - Deployment Validation")
    print("=" * 60)

    validation_results = []

    # Test 1: Core Module Imports
    print("\\n1. Testing Core Module Imports...")
    try:
        from markitect.assets import AssetManager
        from markitect.assets.registry import AssetRegistry
        from markitect.assets.deduplicator import AssetDeduplicator
        from markitect.assets.packager import MarkdownPackager
        validation_results.append(("Core Imports", True, "All core modules imported successfully"))
        print("   ✅ All core modules imported successfully")
    except Exception as e:
        validation_results.append(("Core Imports", False, f"Import error: {e}"))
        print(f"   ❌ Import error: {e}")
        return False

    # Test 2: Asset Manager Initialization
    print("\\n2. Testing Asset Manager Initialization...")
    try:
        with tempfile.TemporaryDirectory() as temp_dir:
            storage_path = Path(temp_dir) / "assets"
            manager = AssetManager(storage_path=storage_path)
            assert manager.storage_path.exists()
            validation_results.append(("Asset Manager Init", True, "AssetManager initialized correctly"))
            print("   ✅ AssetManager initialized correctly")
    except Exception as e:
        validation_results.append(("Asset Manager Init", False, f"Initialization error: {e}"))
        print(f"   ❌ Initialization error: {e}")
        return False

    # Test 3: Asset Operations
    print("\\n3. Testing Basic Asset Operations...")
    try:
        with tempfile.TemporaryDirectory() as temp_dir:
            storage_path = Path(temp_dir) / "assets"
            manager = AssetManager(storage_path=storage_path)

            # Create test file
            test_file = Path(temp_dir) / "test.txt"
            test_file.write_text("Deployment validation test content")

            # Add asset
            result = manager.add_asset(test_file)
            asset_hash = result['content_hash']

            # Verify asset exists
            assert manager.registry.asset_exists(asset_hash)

            # Get asset info
            info = manager.get_asset_info(asset_hash)
            assert info['content_hash'] == asset_hash

            validation_results.append(("Asset Operations", True, "Add, verify, and info operations working"))
            print("   ✅ Add, verify, and info operations working")
    except Exception as e:
        validation_results.append(("Asset Operations", False, f"Operation error: {e}"))
        print(f"   ❌ Operation error: {e}")
        return False

    # Test 4: Deduplication
    print("\\n4. Testing Asset Deduplication...")
    try:
        with tempfile.TemporaryDirectory() as temp_dir:
            storage_path = Path(temp_dir) / "assets"
            manager = AssetManager(storage_path=storage_path)

            # Create identical test files
            test_file1 = Path(temp_dir) / "test1.txt"
            test_file2 = Path(temp_dir) / "test2.txt"
            content = "Identical content for deduplication test"
            test_file1.write_text(content)
            test_file2.write_text(content)

            # Add both files
            result1 = manager.add_asset(test_file1)
            result2 = manager.add_asset(test_file2)

            # Should have same hash (deduplicated)
            assert result1['content_hash'] == result2['content_hash']
            assert result2.get('deduplicated', False)

            validation_results.append(("Deduplication", True, "Content-based deduplication working"))
            print("   ✅ Content-based deduplication working")
    except Exception as e:
        validation_results.append(("Deduplication", False, f"Deduplication error: {e}"))
        print(f"   ❌ Deduplication error: {e}")
        return False

    # Test 5: Package Creation and Extraction
    print("\\n5. Testing Package Operations...")
    try:
        with tempfile.TemporaryDirectory() as temp_dir:
            storage_path = Path(temp_dir) / "assets"
            manager = AssetManager(storage_path=storage_path)

            # Create test document structure
            doc_dir = Path(temp_dir) / "test_doc"
            doc_dir.mkdir()
            (doc_dir / "README.md").write_text("# Test Document")

            assets_dir = doc_dir / "assets"
            assets_dir.mkdir()
            (assets_dir / "test_asset.txt").write_text("Test asset content")

            # Create package
            package_path = Path(temp_dir) / "test.mdpkg"
            manager.create_package(doc_dir, package_path)
            assert package_path.exists()

            # Extract package
            extract_dir = Path(temp_dir) / "extracted"
            manager.extract_package(package_path, extract_dir)
            assert extract_dir.exists()
            assert (extract_dir / "README.md").exists()

            validation_results.append(("Package Operations", True, "Package creation and extraction working"))
            print("   ✅ Package creation and extraction working")
    except Exception as e:
        validation_results.append(("Package Operations", False, f"Package error: {e}"))
        print(f"   ❌ Package error: {e}")
        return False

    # Test 6: Performance Benchmark
    print("\\n6. Testing Performance Benchmarks...")
    try:
        with tempfile.TemporaryDirectory() as temp_dir:
            storage_path = Path(temp_dir) / "assets"
            manager = AssetManager(storage_path=storage_path)

            # Create test files
            test_files = []
            for i in range(10):
                test_file = Path(temp_dir) / f"perf_test_{i}.txt"
                test_file.write_text(f"Performance test content {i}")
                test_files.append(test_file)

            # Benchmark asset addition
            start_time = time.time()
            for test_file in test_files:
                manager.add_asset(test_file)
            elapsed = time.time() - start_time

            # Should process 10 assets in under 1 second
            avg_time = elapsed / len(test_files)
            assert elapsed < 1.0, f"Too slow: {elapsed:.2f}s"
            assert avg_time < 0.1, f"Average too slow: {avg_time:.3f}s"

            validation_results.append(("Performance", True, f"10 assets processed in {elapsed:.3f}s"))
            print(f"   ✅ 10 assets processed in {elapsed:.3f}s (avg: {avg_time*1000:.1f}ms)")
    except Exception as e:
        validation_results.append(("Performance", False, f"Performance error: {e}"))
        print(f"   ❌ Performance error: {e}")
        return False

    # Test 7: Error Handling
    print("\\n7. Testing Error Handling...")
    try:
        with tempfile.TemporaryDirectory() as temp_dir:
            storage_path = Path(temp_dir) / "assets"
            manager = AssetManager(storage_path=storage_path)

            # Test nonexistent file
            nonexistent = Path(temp_dir) / "does_not_exist.txt"
            try:
                manager.add_asset(nonexistent)
                assert False, "Should have raised exception"
            except Exception:
                pass  # Expected

            # Test invalid hash lookup
            try:
                manager.get_asset_info("invalid_hash")
                assert False, "Should have raised exception"
            except Exception:
                pass  # Expected

            validation_results.append(("Error Handling", True, "Error scenarios handled gracefully"))
            print("   ✅ Error scenarios handled gracefully")
    except Exception as e:
        validation_results.append(("Error Handling", False, f"Error handling error: {e}"))
        print(f"   ❌ Error handling error: {e}")
        return False

    # Test 8: CLI Integration
    print("\\n8. Testing CLI Integration...")
    try:
        from markitect.cli.asset_commands import AssetCommands

        with tempfile.TemporaryDirectory() as temp_dir:
            storage_path = Path(temp_dir) / "assets"
            manager = AssetManager(storage_path=storage_path)
            cli_commands = AssetCommands(manager)

            # Test CLI command structure
            assert hasattr(cli_commands, 'add_asset')
            assert hasattr(cli_commands, 'list_assets')
            assert hasattr(cli_commands, 'get_asset_info')

            validation_results.append(("CLI Integration", True, "CLI commands available and accessible"))
            print("   ✅ CLI commands available and accessible")
    except Exception as e:
        validation_results.append(("CLI Integration", False, f"CLI error: {e}"))
        print(f"   ❌ CLI error: {e}")
        return False

    # Summary
    print("\\n" + "=" * 60)
    print("📊 Deployment Validation Summary")
    print("=" * 60)

    passed = sum(1 for _, success, _ in validation_results if success)
    total = len(validation_results)
    success_rate = (passed / total) * 100

    for test_name, success, message in validation_results:
        status = "✅ PASS" if success else "❌ FAIL"
        print(f"{status:<8} {test_name:<20} {message}")

    print(f"\\nOverall Success Rate: {passed}/{total} ({success_rate:.1f}%)")

    if success_rate == 100:
        print("\\n🎉 DEPLOYMENT VALIDATION SUCCESSFUL!")
        print("✅ Asset Management system is ready for production deployment.")
        return True
    else:
        print("\\n❌ DEPLOYMENT VALIDATION FAILED!")
        print("❗ Please address the failed tests before deployment.")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)