"""Check public Danish source pages for the phrases used in the demonstration."""

from html.parser import HTMLParser
from urllib.request import Request, urlopen


class PageText(HTMLParser):
    def __init__(self):
        super().__init__()
        self.parts = []
        self.hidden = False

    def handle_starttag(self, tag, attrs):
        if tag in {"style", "script"}:
            self.hidden = True

    def handle_endtag(self, tag):
        if tag in {"style", "script"}:
            self.hidden = False

    def handle_data(self, data):
        if not self.hidden:
            self.parts.append(data)


SOURCES = (
    ("SIRI — Collective Agreement Scheme",
     "https://www.nyidanmark.dk/en-GB/News-Front-Page/2026/10/New-work-scheme-at-SIRI-per-1-January-2027",
     ("1 January 2027", "DKK 322,000", "2026 level", "16 countries", "must be certified")),
    ("Ny i Danmark — Pay Limit Scheme",
     "https://www.nyidanmark.dk/en-GB/You-want-to-apply/Work/Pay-limit-scheme",
     ("DKK 552,000 (2026 level)",)),
    ("Ny i Danmark — supplementary pay limit",
     "https://nyidanmark.dk/en-GB/Words-and-concepts/SIRI/Minimum-amounts/The-Supplementary-Pay-Limit-Scheme%27s-minimum-amount",
     ("DKK 446,000 (2026 level)",)),
    ("Ny i Danmark — permanent residence",
     "https://nyidanmark.dk/en-GB/You-want-to-apply/Permanent-residence-permit/Permanent-residence",
     ("8 years", "4 years", "2 of 4 supplementary requirements", "3 years and 6 months")),
)


def main():
    complete = True
    for name, url, phrases in SOURCES:
        print(f"\n{name}\n{url}", flush=True)
        try:
            with urlopen(Request(url, headers={"User-Agent":"E030-source-check/1.0"}), timeout=15) as response:
                page = PageText()
                page.feed(response.read().decode("utf-8", errors="replace"))
        except OSError as error:
            print(f"UNVERIFIED ({type(error).__name__})", flush=True)
            complete = False
            continue
        normalized = " ".join(" ".join(page.parts).split()).casefold()
        for phrase in phrases:
            found = phrase.casefold() in normalized
            print(f"{'Verified text' if found else 'UNVERIFIED'}  {phrase}", flush=True)
            complete &= found
    return 0 if complete else 1


if __name__ == "__main__":
    raise SystemExit(main())
