"""Script to update Morizon SourceHtml configuration"""
from extractly.models import SourceHtml
import json

# Get Morizon configuration
sh = SourceHtml.objects.get(source_id='779dc8fa-ef0b-4627-8cf6-755f3375d949')

# Update actions - wait, scroll, expand description
sh.actions = [
    {'index': 0, 'action_type': 'delay', 'delay': 3000},
    {'index': 1, 'action_type': 'scroll', 'distance': 500},
    {'index': 2, 'action_type': 'delay', 'delay': 1000},
    {'index': 3, 'action_type': 'click_text', 'text': 'Pokaż cały opis'},
    {'index': 4, 'action_type': 'delay', 'delay': 1000}
]

# Update selectors - extract main content sections and remove ads
sh.selectors = {
    'list': {},
    'detail': {},
    'sliced_html': {
        'merge': True,
        'content_type': 'detail',
        'skip_selectors': [
            'header',
            'footer', 
            'nav',
            'script',
            'style',
            '#navbar',
            '#header',
            '.ad_wrapper',
            '.onet-ad',
            'iframe',
            '[data-cy="hamburgerDropdown"]',
            '[data-cy="hamburgerButton"]',
            '.nuxt-loading-indicator',
            '#teleports',
            '[id^="google_ads"]',
            '[id^="slot-"]',
            '.ad_adInfo',
            '[data-google-query-id]'
        ],
        'sliced_details_html': {
            'selector': 'section[data-v-7dd7c0e2]',
            'playwright_action': 'inner_html'
        },
        'sliced_gallery_html': {
            'selector': 'div.D-cGgV[data-v-1b365d2d]',
            'playwright_action': 'inner_html'
        },
        'sliced_contact_html': {
            'selector': 'aside#contact-form',
            'playwright_action': 'inner_html'
        }
    }
}

sh.save()

print('✅ Updated successfully!')
print(f'Actions: {len(sh.actions)} items')
print(f'Selectors keys: {list(sh.selectors.keys())}')
print(f'Skip selectors: {len(sh.selectors.get("sliced_html", {}).get("skip_selectors", []))} items')
print('\nActions configured:')
for action in sh.actions:
    print(f'  - {action.get("action_type")}: {action.get("text", "")} {action.get("delay", "")} {action.get("distance", "")}')
