"""Rule: ``.content-carousel`` → static ``.page-body__gallery`` with headline.

Source (/en/company/press-and-news/ "NEWS" block):

    <section class="content-carousel content-element">
      <div class="container-fluid container--padded">
        <div class="row"><div class="col-…">
          <h2 class="content-carousel__headline">NEWS</h2>
          <div class="content-carousel__slider-wrapper">
            <div class="content-carousel__slider swiper-container">
              <div class="swiper-wrapper">
                <div class="content-carousel__slider-item swiper-slide">
                  <picture><img …></picture>
                </div>
                …
              </div>
            </div>
          </div>
        </div></div>
      </div>
    </section>

The source renders this as a JS-powered carousel. The theme has no matching
page-body carousel library, so we emit a static gallery that just tiles every
slide. Label items, if present (``.content-carousel__labels-item``), are
carried as captions.

Target:

    <section class="page-body__gallery-section">
      <h2>NEWS</h2>
      <ul class="page-body__gallery page-body__gallery--captioned">
        <li class="page-body__gallery__item">
          <img …>
          <h3>Optional label</h3>
          <p>Optional caption</p>
        </li>
        …
      </ul>
    </section>
"""

from __future__ import annotations

from bs4 import BeautifulSoup, Tag

from . import TransformContext


def transform(soup: BeautifulSoup, ctx: TransformContext) -> None:
    for root in list(soup.find_all(class_="content-carousel")):
        if not isinstance(root, Tag):
            continue
        if root.attrs is None or root.parent is None:
            continue
        _upgrade(root, soup)


def _upgrade(root: Tag, soup: BeautifulSoup) -> None:
    headline_el = root.find(class_="content-carousel__headline")
    slide_items = root.find_all(class_="content-carousel__slider-item")
    label_items = root.find_all(class_="content-carousel__labels-item")

    new_section = soup.new_tag("section", **{"class": "page-body__gallery-section"})
    if isinstance(headline_el, Tag):
        text = " ".join(headline_el.get_text(" ", strip=True).split())
        if text:
            h2 = soup.new_tag("h2")
            h2.string = text
            new_section.append(h2)

    has_labels = bool(label_items)
    gallery_classes = "page-body__gallery page-body__gallery--captioned" if has_labels else "page-body__gallery"
    gallery = soup.new_tag("ul", **{"class": gallery_classes})

    for i, slide in enumerate(slide_items):
        if not isinstance(slide, Tag):
            continue
        li = soup.new_tag("li", **{"class": "page-body__gallery__item"})
        img = slide.find("img")
        if isinstance(img, Tag):
            li.append(img.extract())

        if has_labels and i < len(label_items):
            label = label_items[i]
            head = label.find(class_="content-carousel__labels-item-headline") if isinstance(label, Tag) else None
            body = label.find(class_="content-carousel__labels-item-text") if isinstance(label, Tag) else None
            if isinstance(head, Tag):
                text = " ".join(head.get_text(" ", strip=True).split())
                if text:
                    h3 = soup.new_tag("h3")
                    h3.string = text
                    li.append(h3)
            if isinstance(body, Tag):
                text = " ".join(body.get_text(" ", strip=True).split())
                if text:
                    p = soup.new_tag("p")
                    p.string = text
                    li.append(p)

        if li.contents:
            gallery.append(li)

    new_section.append(gallery)
    root.replace_with(new_section)
