"""
Diagnostic Script - Discover Analytic Dimension Assignment Fields

This script will show you the actual field names in your Tryton instance.
Run this to see what fields are available.
"""

from proteus import config, Model

# XML-RPC Configuration
HTTPS = 'https://'
SERVER_URL = 'itsa.open-squared.tech'
DATABASE_NAME = 'tradon'
USERNAME = 'admin'
PASSWORD = 'dsproject'

def discover_assignment_fields():
    """Discover the actual fields in analytic_dimension_assignment model"""
    
    print("="*70)
    print("DISCOVERING ANALYTIC DIMENSION ASSIGNMENT FIELDS")
    print("="*70)
    
    try:
        # Get the model
        AnalyticDimensionAssignment = Model.get('analytic.dimension.assignment')
        
        # Create a new instance (don't save it)
        test_assignment = AnalyticDimensionAssignment()
        
        # Get all attributes
        print("\nAvailable fields on analytic.dimension.assignment:\n")
        
        # Method 1: Try _fields if available (Tryton models expose this)
        if hasattr(test_assignment, '_fields'):
            for field_name in sorted(test_assignment._fields.keys()):
                field = test_assignment._fields[field_name]
                print(f"  - {field_name}: {type(field).__name__}")
        else:
            # Method 2: Try __dict__ or dir()
            print("Fields found via dir():")
            for attr in sorted(dir(test_assignment)):
                if not attr.startswith('_') and not callable(getattr(test_assignment, attr, None)):
                    print(f"  - {attr}")
        
        print("\n" + "="*70)
        print("CHECKING EXISTING ASSIGNMENTS (if any)")
        print("="*70)
        
        # Try to find an existing assignment to see its structure
        existing = AnalyticDimensionAssignment.find([], limit=1)
        if existing:
            print(f"\nFound existing assignment (ID: {existing[0].id})")
            print("\nAttributes of existing assignment:")
            for attr in dir(existing[0]):
                if not attr.startswith('_') and not callable(getattr(existing[0], attr, None)):
                    try:
                        value = getattr(existing[0], attr)
                        print(f"  - {attr}: {type(value).__name__} = {value}")
                    except:
                        print(f"  - {attr}: <unable to read>")
        else:
            print("\nNo existing assignments found in database")
        
    except Exception as e:
        print(f"\nError: {e}")
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    # Make sure you're connected to Tryton first
    config.set_xmlrpc(f'{HTTPS}{USERNAME}:{PASSWORD}@{SERVER_URL}/{DATABASE_NAME}/')

    print(f"Connected to Tryton database '{DATABASE_NAME}' successfully!")
    
    discover_assignment_fields()