// 🔥 Comprehensive fini System Test - ChatGPT5 Design Validation

// Test Case 1: Basic finalization and usage prohibition
box SimpleResource {
    init { name, value }
    
    pack(resourceName) {
        me.name = resourceName
        me.value = 42
    }
    
    getValue() {
        return me.value
    }
    
    fini() {
        print("🔥 SimpleResource.fini(): Cleaning up " + me.name.toString())
    }
}

// Test Case 2: Circular reference with weak fields
box Parent {
    init { name, weak child }
    
    pack(parentName) {
        me.name = parentName
    }
    
    setChild(c) {
        me.child = c
    }
    
    getName() {
        return me.name
    }
}

box Child {
    init { id, parent }
    
    pack(childId, p) {
        me.id = childId
        me.parent = p
    }
    
    // Test Case 3: weak-fini prohibition
    fini() {
        print("🔥 Child.fini(): Cleaning up child " + me.id.toString())
        // me.parent.fini()  // This should be caught and prevented
    }
}

// Test Case 4: Deterministic cascade finalization
box Pipeline {
    init { r1, r2, r3, weak monitor }
    
    pack(name) {
        me.r1 = new SimpleResource(name + "_r1")
        me.r2 = new SimpleResource(name + "_r2") 
        me.r3 = new SimpleResource(name + "_r3")
    }
    
    setMonitor(m) {
        me.monitor = m
    }
    
    // Custom fini with specific order control
    fini() {
        print("🔥 Pipeline.fini(): Custom cleanup order")
        // Reverse dependency order: r3 → r2 (r1 will be auto-cascade)
        me.r3.fini()
        me.r2.fini()
        // r1 should be automatically finalized by cascade
        // monitor is weak, so it's not finalized
    }
}

static box Main {
    main() {
        print("=== 🔥 Comprehensive fini System Test ===")
        
        // Test 1: Basic finalization
        print("\n📋 Test 1: Basic finalization and usage prohibition")
        local resource = new SimpleResource("TestResource")
        print("Resource value before fini: " + resource.getValue().toString())
        
        resource.fini()
        print("Resource finalized")
        
        // This should throw an error - usage after finalization
        // print("Trying to access finalized resource...")
        // resource.getValue()  // Should fail with "Instance was finalized"
        
        // Test 2: Circular reference handling
        print("\n📋 Test 2: Circular reference with weak fields")
        local parent = new Parent("TestParent")
        local child = new Child("child1", parent)
        parent.setChild(child)
        
        print("Parent: " + parent.getName().toString())
        print("Before parent finalization")
        
        parent.fini()
        print("Parent finalized - child's weak reference should be safe")
        
        // Test 3: Deterministic cascade finalization
        print("\n📋 Test 3: Deterministic cascade finalization")
        local pipeline = new Pipeline("TestPipeline")
        local monitor = new SimpleResource("Monitor")
        pipeline.setMonitor(monitor)
        
        print("Pipeline created with resources and monitor")
        pipeline.fini()
        print("Pipeline finalized with custom order + auto-cascade")
        
        print("\n✅ fini System Test Completed!")
        return "All tests passed"
    }
}