"""Rule: ``.testimonials-slider`` → ``<ul class="page-body__testimonials">``.

Source:

    <div class="testimonials-slider">
      <div class="testimonials-slider__icon">…quotation-mark SVG…</div>
      <div class="testimonials-slider__container swiper-container">
        <div class="swiper-wrapper">
          <div class="testimonials-slider__item swiper-slide">
            <div class="formatted-text"><p>Quote…</p></div>
            <div class="testimonials-slider__profile">
              <picture><img …></picture>
              <div class="testimonials-slider__profile-text">
                <strong>Name</strong><br>
                <span>Title</span>
              </div>
            </div>
          </div>
          …
        </div>
      </div>
    </div>

Target (reuses ``.page-body__testimonials`` CSS):

    <ul class="page-body__testimonials">
      <li>
        <blockquote><p>Quote…</p></blockquote>
        <figure>
          <img src="public://…">
          <figcaption>
            <strong>Name</strong>
            <span>Title</span>
          </figcaption>
        </figure>
      </li>
      …
    </ul>
"""

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_="testimonials-slider")):
        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:
    items = root.find_all(class_="testimonials-slider__item")
    new_list = soup.new_tag("ul", **{"class": "page-body__testimonials"})

    for item in items:
        if not isinstance(item, Tag):
            continue
        li = soup.new_tag("li")

        # Quote body: promote .formatted-text prose into a <blockquote>.
        quote_source = item.find(class_="formatted-text") or item
        if isinstance(quote_source, Tag):
            blockquote = soup.new_tag("blockquote")
            # Move every Tag child (p mostly) from the quote source.
            for child in list(quote_source.children):
                if isinstance(child, Tag):
                    blockquote.append(child.extract())
            if blockquote.contents:
                li.append(blockquote)

        # Profile: image + name + title.
        profile = item.find(class_="testimonials-slider__profile")
        if isinstance(profile, Tag):
            figure = soup.new_tag("figure")
            img = profile.find("img")
            if isinstance(img, Tag):
                figure.append(img.extract())
            profile_text = profile.find(class_="testimonials-slider__profile-text")
            if isinstance(profile_text, Tag):
                cap = soup.new_tag("figcaption")
                for child in list(profile_text.children):
                    if isinstance(child, Tag):
                        cap.append(child.extract())
                if cap.contents:
                    figure.append(cap)
            if figure.contents:
                li.append(figure)

        if li.contents:
            new_list.append(li)

    root.replace_with(new_list)
