import xml.etree.ElementTree as ET
from datetime import datetime, timezone

# IUCLID 6 XML namespaces
NS_PLATFORM_CONTAINER = "http://iuclid6.echa.europa.eu/namespaces/platform-container/v2"
NS_PLATFORM_METADATA = "http://iuclid6.echa.europa.eu/namespaces/platform-metadata/v1"
NS_PLATFORM_FIELDS = "http://iuclid6.echa.europa.eu/namespaces/platform-fields/v1"
NS_XLINK = "http://www.w3.org/1999/xlink"

# Register namespace prefixes for clean output
ET.register_namespace("i6c", NS_PLATFORM_CONTAINER)
ET.register_namespace("i6m", NS_PLATFORM_METADATA)
ET.register_namespace("i6", NS_PLATFORM_FIELDS)
ET.register_namespace("xlink", NS_XLINK)

IUCLID_VERSION = "6.0.0"
DEFINITION_VERSION = "10.0"
CREATION_TOOL = "enviPath"


def _tag(ns: str, local: str) -> str:
    return f"{{{ns}}}{local}"


def _sub(parent: ET.Element, ns: str, local: str, text: str | None = None) -> ET.Element:
    """Create a sub-element under parent. Only sets text if not None."""
    elem = ET.SubElement(parent, _tag(ns, local))
    if text is not None:
        elem.text = str(text)
    return elem


def _sub_if(parent: ET.Element, ns: str, local: str, text: str | None = None) -> ET.Element | None:
    """Create a sub-element only when text is not None."""
    if text is None:
        return None
    return _sub(parent, ns, local, text)


def build_platform_metadata(
    document_key: str,
    document_type: str,
    name: str,
    document_sub_type: str | None = None,
    parent_document_key: str | None = None,
    order_in_section_no: int | None = None,
) -> ET.Element:
    """Build the <i6c:PlatformMetadata> element for an i6d document."""
    pm = ET.Element(_tag(NS_PLATFORM_CONTAINER, "PlatformMetadata"))

    now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")

    _sub(pm, NS_PLATFORM_METADATA, "iuclidVersion", IUCLID_VERSION)
    _sub(pm, NS_PLATFORM_METADATA, "documentKey", document_key)
    _sub(pm, NS_PLATFORM_METADATA, "documentType", document_type)
    _sub(pm, NS_PLATFORM_METADATA, "definitionVersion", DEFINITION_VERSION)
    _sub(pm, NS_PLATFORM_METADATA, "creationDate", now)
    _sub(pm, NS_PLATFORM_METADATA, "lastModificationDate", now)
    _sub(pm, NS_PLATFORM_METADATA, "name", name)
    if document_sub_type:
        _sub(pm, NS_PLATFORM_METADATA, "documentSubType", document_sub_type)
    if parent_document_key:
        _sub(pm, NS_PLATFORM_METADATA, "parentDocumentKey", parent_document_key)
    if order_in_section_no is not None:
        _sub(pm, NS_PLATFORM_METADATA, "orderInSectionNo", str(order_in_section_no))
    _sub(pm, NS_PLATFORM_METADATA, "i5Origin", "false")
    _sub(pm, NS_PLATFORM_METADATA, "creationTool", CREATION_TOOL)

    return pm


def build_document(
    document_key: str,
    document_type: str,
    name: str,
    content_element: ET.Element,
    document_sub_type: str | None = None,
    parent_document_key: str | None = None,
    order_in_section_no: int | None = None,
) -> str:
    """Build a complete i6d document XML string."""
    root = ET.Element(_tag(NS_PLATFORM_CONTAINER, "Document"))

    pm = build_platform_metadata(
        document_key=document_key,
        document_type=document_type,
        name=name,
        document_sub_type=document_sub_type,
        parent_document_key=parent_document_key,
        order_in_section_no=order_in_section_no,
    )
    root.append(pm)

    content_wrapper = _sub(root, NS_PLATFORM_CONTAINER, "Content")
    content_wrapper.append(content_element)

    _sub(root, NS_PLATFORM_CONTAINER, "Attachments")
    _sub(root, NS_PLATFORM_CONTAINER, "ModificationHistory")

    return ET.tostring(root, encoding="unicode", xml_declaration=True)


def document_key(uuid) -> str:
    """Format a UUID as an IUCLID document key (uuid/0 for raw data)."""
    return f"{uuid}/0"
