"""ProcessBPMN model — Phase 8: BPMN Process Modeling."""

from __future__ import annotations

from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _

from simorgh.core.models import TimeStampedModel, UUIDModel


class ProcessBPMN(UUIDModel, TimeStampedModel):
    """Stores versioned BPMN XML diagrams for a process definition."""

    process = models.ForeignKey(
        "bpm.ProcessDefinition",
        on_delete=models.CASCADE,
        related_name="bpmn_versions",
        verbose_name=_("Process"),
    )
    version = models.CharField(_("Version"), max_length=50)
    bpmn_xml = models.TextField(_("BPMN XML"), help_text=_("Full BPMN 2.0 XML content."))
    thumbnail_svg = models.TextField(
        _("Thumbnail SVG"),
        blank=True,
        default="",
        help_text=_("Optional SVG preview of the BPMN diagram."),
    )
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="bpmn_versions_created",
        verbose_name=_("Created By"),
    )
    is_current = models.BooleanField(
        _("Is Current"),
        default=False,
        help_text=_("Marks this version as the active BPMN for the process."),
    )

    class Meta:
        app_label = "bpm"
        verbose_name = _("Process BPMN")
        verbose_name_plural = _("Process BPMNs")
        ordering = ["-created_at"]
        indexes = [
            models.Index(fields=["process", "is_current"]),
        ]

    def __str__(self) -> str:
        return f"{self.process.name} — v{self.version}"
