import logging

from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page

from mkdocs_zettelkasten.plugin.adapters.backlinks_to_page import (
    adapt_backlinks_to_page,
)
from mkdocs_zettelkasten.plugin.adapters.page_links_to_zettels import (
    adapt_page_links_to_zettels,
)
from mkdocs_zettelkasten.plugin.adapters.page_ref import get_page_ref
from mkdocs_zettelkasten.plugin.adapters.page_title import adapt_page_title
from mkdocs_zettelkasten.plugin.adapters.prev_next_page import get_prev_next_page
from mkdocs_zettelkasten.plugin.adapters.transclusion import adapt_transclusion
from mkdocs_zettelkasten.plugin.services.zettel_service import ZettelService

logger = logging.getLogger(
    __name__.replace("mkdocs_zettelkasten.plugin.", "mkdocs.plugins.zettelkasten.")
)


class PageTransformer:
    """
    Applies all Zettelkasten-specific transformations to page markdown.
    """

    def transform(
        self,
        markdown: str,
        page: Page,
        config: MkDocsConfig,
        files: Files,
        zettel_service: ZettelService,
    ) -> str:
        """
        Apply all adapters to the markdown and update references.
        """
        logger.debug("Started %s transformations", page.file.src_path)
        page = zettel_service.add_zettel_to_page(page)
        markdown = adapt_page_title(markdown, page, page.meta.get("zettel"))
        markdown = adapt_transclusion(
            markdown,
            zettel_service.get_zettel_by_partial_path,
            site_url=config["site_url"],
            file_suffix=zettel_service.file_suffix,
            strip_heading=config.get("extra", {}).get(
                "transclusion_strip_heading", True
            ),
        )
        markdown = adapt_page_links_to_zettels(
            markdown,
            page,
            config,
            files,
            zettel_service.get_zettel_by_partial_path,
            file_suffix=zettel_service.file_suffix,
        )
        processed_md, page.meta["ref"] = get_page_ref(markdown, page, config)
        page.previous_page, page.next_page = get_prev_next_page(
            page,
            files,
            zettel_service.get_zettels(),
            file_suffix=zettel_service.file_suffix,
        )
        adapt_backlinks_to_page(
            page,
            zettel_service.backlinks,
            zettel_service.get_zettel_by_partial_path,
        )
        logger.debug("Finished %s transformations", page.file.src_path)

        return processed_md
