"""Check the running local site without sending forms or creating contacts."""
import json
import concurrent.futures
import urllib.request
import urllib.error
import subprocess
from pathlib import Path
from html.parser import HTMLParser

ROOT = Path(__file__).resolve().parents[1]
BASE = 'http://localhost:3000'
modules = json.loads(subprocess.check_output([
    'node', '--input-type=module', '-e',
    "import {modules} from './data/tesol/course.mjs'; console.log(JSON.stringify(modules.map(m=>({path:m.path,title:m.title,pdf:m.sourceInfo.file,topics:m.topics.map(t=>({path:t.path,title:t.title}))}))));"
], cwd=ROOT, encoding='utf-8'))

class Page(HTMLParser):
    def __init__(self):
        super().__init__()
        self.h1 = False
        self.headings = []
        self.canonical = None
        self.schemas = []
        self.script = False
        self.script_text = ''
        self.meta = {}
        self.article = False
        self.article_text = []
    def handle_starttag(self, tag, attrs):
        attrs = dict(attrs)
        if tag == 'h1': self.h1 = True
        if tag == 'link' and attrs.get('rel') == 'canonical': self.canonical = attrs.get('href')
        if tag == 'meta': self.meta[attrs.get('name') or attrs.get('property')] = attrs.get('content', '')
        if tag == 'article': self.article = True
        if tag == 'script' and attrs.get('type') == 'application/ld+json': self.script = True; self.script_text = ''
    def handle_endtag(self, tag):
        if tag == 'h1': self.h1 = False
        if tag == 'article': self.article = False
        if tag == 'script' and self.script:
            self.schemas.append(json.loads(self.script_text))
            self.script = False
    def handle_data(self, text):
        if self.h1: self.headings.append(text)
        if self.script: self.script_text += text
        if self.article and not self.script: self.article_text.append(text)

targets = [{'path': '/tesol/resources', 'title': 'Understand the teaching.'}]
for module in modules:
    targets.append({'path': module['path'], 'title': module['title']})
    targets.extend(module['topics'])

def check(target):
    path = target['path']
    with urllib.request.urlopen(BASE + path, timeout=60) as response:
        html = response.read().decode()
        assert response.status == 200
    page = Page()
    page.feed(html)
    assert target['title'] in ''.join(page.headings), (path, 'wrong or missing heading')
    assert page.canonical and page.canonical.endswith(path), (path, 'missing canonical')
    assert any(isinstance(schema, (list, dict)) for schema in page.schemas), (path, 'missing schema')
    assert page.meta.get('description'), (path, 'missing description')
    assert page.meta.get('twitter:card') == 'summary_large_image', (path, 'missing social preview')
    assert '/tesol/resources/share-image' in page.meta.get('og:image', ''), (path, 'missing topic share image')
    assert 'TESOL course guidance' in html, (path, 'missing advisor card')
    assert 'https://www.linkedin.com/in/aisha-vats-4b969121b/' in html
    if path.count('/') == 4:
        assert 'A closer look at the teaching' in html
        assert 'A teaching sequence you can adapt' in html
        assert 'Sources and authorship' in html
        assert len(' '.join(page.article_text).split()) >= 650, (path, 'insufficient rendered content')
    assert 'NEXT_HTTP_ERROR_FALLBACK;500' not in html
    return path

results = []
errors = []
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    futures = {executor.submit(check, target): target for target in targets}
    for future in concurrent.futures.as_completed(futures):
        try:
            results.append(future.result())
            if len(results) % 25 == 0: print(f'Checked {len(results)}/{len(targets)} pages', flush=True)
        except Exception as error: errors.append({'path': futures[future]['path'], 'error': str(error)})

for path in ['/tesol/resources/missing-module', '/tesol/resources/teaching-vocabulary/missing-topic']:
    try:
        urllib.request.urlopen(BASE + path, timeout=60)
        errors.append({'path': path, 'error': 'Expected a 404'})
    except urllib.error.HTTPError as error:
        if error.code != 404: errors.append({'path': path, 'error': str(error)})

source_count = 0
for module in modules:
    try:
        with urllib.request.urlopen(BASE + '/uploads/pdf/tesol/' + module['pdf'], timeout=60) as response:
            assert response.headers.get_content_type() == 'application/pdf'
            assert response.read(5) == b'%PDF-'
            source_count += 1
    except Exception as error: errors.append({'path': module['pdf'], 'error': str(error)})

report = {'pagesPassed': len(results), 'pagesExpected': len(targets), 'sourcePdfsPassed': source_count, 'errors': errors}
(ROOT / 'reports/tesol/http-checks.json').write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
print(json.dumps(report, indent=2), flush=True)
raise SystemExit(1 if errors else 0)
