"""
Final verification script for Morizon Level 2 HTML Collection
"""
import sys
import os

sys.path.insert(0, '.')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NetworkMonitoring.settings')

import django
django.setup()

from extractly.models import NetworkMonitoredPage, SourceNetwork, SourceHtml

print('='*70)
print('FINAL VERIFICATION - MORIZON LEVEL 2')
print('='*70)

# 1. Check SourceNetwork
s = SourceNetwork.objects.filter(name='Morizon').first()
print(f'\n1. SourceNetwork:')
print(f'   - ID: {s.id}')
print(f'   - Name: {s.name}')
print(f'   - Has cookies config: {"cookies" in s.selectors}')
print(f'   - Cookie timeout: {s.selectors.get("cookies", {}).get("timeout")}')

# 2. Check SourceHtml
h = SourceHtml.objects.filter(source_id=s.id).first()
print(f'\n2. SourceHtml:')
print(f'   - Exists: {h is not None}')
print(f'   - Name: {h.name if h else "N/A"}')
print(f'   - Has selectors: {bool(h.selectors) if h else False}')
print(f'   - Has actions: {bool(h.actions) if h else False}')

# 3. Check NetworkMonitoredPages
pages = NetworkMonitoredPage.objects.filter(source_id=s.id)
total = pages.count()
with_cookies = sum(1 for p in pages if p.meta and 'cookies' in p.meta)
with_selector = sum(1 for p in pages if p.meta and 'cookies' in p.meta and 'accept_selector' in p.meta.get('cookies', {}))
fetched = pages.filter(is_fetched=True).count()

print(f'\n3. NetworkMonitoredPages:')
print(f'   - Total pages: {total}')
print(f'   - With cookies dict: {with_cookies} ({with_cookies/total*100:.1f}%)')
print(f'   - With accept_selector: {with_selector} ({with_selector/total*100:.1f}%)')
print(f'   - Already fetched: {fetched} ({fetched/total*100:.1f}%)')
print(f'   - Remaining: {total - fetched}')

# 4. Cookie Function Status
print(f'\n4. Cookie Function:')
print(f'   - File: link_agregator/utils/cookies.py')
print(f'   - Status: ✅ Optimized (config-first strategy)')
print(f'   - Dynamic detection: Quick selectors only')
print(f'   - Wait timeout: Fixed (1s instead of 45s)')

# 5. Summary
print('\n' + '='*70)
all_ok = (
    h is not None and
    with_cookies == total and
    with_selector == total
)

if all_ok:
    print('STATUS: ✅ ALL CHECKS PASSED - READY FOR COLLECTION')
    print('='*70)
    print('\nRUN COLLECTION:')
    print('  cd extractly')
    print('  python -m scripts.parallel_run_html --name morizon --workers 8 --headless')
else:
    print('STATUS: ⚠️ SOME CHECKS FAILED - REVIEW ABOVE')
    
print('='*70)
