"""Dublin Airport's own site (www.dublinairport.com): the shops in the terminals page.

Server-rendered by Next.js (11 Sep 2026): a card per shop (`<article aria-label="<shop>"
data-content-type="card">`) carrying the name, one line of hours ("Mon-Sun 04:00-22:00", a phone
number after it) and a terminal chip ("T1"). The duty free cards are the ones whose label says
so; the two stores read T1 04:00-22:00 and T2 04:00-21:00 on the day. The page repeats each card
for its filters, so readings are deduplicated. robots.txt permits the path; no Crawl-delay.
"""

import re

from app.services.hours.base import StoreHours, parse_timing, text_of

_ARTICLE = re.compile(r'<article\b[^>]*aria-label="([^"]*)"[^>]*data-content-type="card"[^>]*>(.*?)</article>', re.S)
_DUTY_FREE = re.compile(r"duty\s*free", re.I)
_TITLE_AND_LINE = re.compile(r"<h6[^>]*>(.*?)</h6>\s*<p[^>]*>(.*?)</p>", re.S)
_CHIP = re.compile(r'chip__text1">(.*?)</span>', re.S)


class DublinAirportHours:
    slug = "dublin-airport-hours"
    operator = "Dublin Airport (daa)"
    homepage = "https://www.dublinairport.com"
    airports = ("DUB",)
    parser_version = "dublin-hours-1"
    delay_seconds = 0.0

    def pages(self, iata: str) -> list[str]:
        return [f"{self.homepage}/at-the-airport/shopping/shops-in-the-terminals"]

    def follow(self, body: str, url: str) -> list[str]:
        return []

    def parse(self, body: str, url: str) -> list[StoreHours]:
        stores: list[StoreHours] = []
        seen: set[tuple] = set()
        for label, card in _ARTICLE.findall(body):
            if not _DUTY_FREE.search(text_of(label)):
                continue
            block = _TITLE_AND_LINE.search(card)
            if not block:
                continue
            name = text_of(block.group(1))
            times, days, statement = parse_timing(block.group(2))
            if not times and not statement:
                continue
            chip = _CHIP.search(card)
            terminal = text_of(chip.group(1)) if chip else None
            key = (name, terminal, times, days, statement)
            if key in seen:
                continue
            seen.add(key)
            stores.append(StoreHours(
                terminal=terminal, area=None, times=times, days=days, statement=statement, name=name,
            ))
        return stores
