from django.db import models


class OwnerMapping(models.Model):
    """
    责任人映射。

    首版以 Django Admin 作为手工维护入口，字段口径与通知载荷保持一致。
    """

    owner_role = models.CharField(max_length=100, db_index=True)
    owner_name = models.CharField(max_length=100)
    department = models.CharField(max_length=100, blank=True)
    chapter_scope = models.CharField(max_length=100, blank=True)
    risk_scope = models.CharField(max_length=255, blank=True)
    feishu_user_id = models.CharField(max_length=100, blank=True)
    feishu_open_id = models.CharField(max_length=100, blank=True)
    feishu_name = models.CharField(max_length=100, blank=True)
    notify_enabled = models.BooleanField(default=True)
    is_active = models.BooleanField(default=True, db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["owner_role", "id"]

    def __str__(self) -> str:
        return f"{self.owner_role}-{self.owner_name}"


class FeishuNotifyConfig(models.Model):
    """
    飞书通知配置。
    """

    NOTIFY_REASON_COMPLETED = "task_completed"
    NOTIFY_REASON_FAILED = "task_failed"
    NOTIFY_REASON_CHOICES = [
        (NOTIFY_REASON_COMPLETED, "task_completed"),
        (NOTIFY_REASON_FAILED, "task_failed"),
    ]

    config_name = models.CharField(max_length=100)
    notify_reason = models.CharField(max_length=32, choices=NOTIFY_REASON_CHOICES, db_index=True)
    channel = models.CharField(max_length=100, blank=True)
    message_template = models.CharField(max_length=255, blank=True)
    status = models.CharField(max_length=32, blank=True)
    is_active = models.BooleanField(default=True, db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["notify_reason", "id"]

    def __str__(self) -> str:
        return self.config_name


class WordTemplateMapping(models.Model):
    """
    Word 模板与字段映射摘要。
    """

    template_name = models.CharField(max_length=100)
    output_type = models.CharField(max_length=100, default="registration_word_export_report")
    version = models.CharField(max_length=50, blank=True)
    placeholder_count = models.PositiveIntegerField(default=0)
    status = models.CharField(max_length=32, blank=True)
    field_mapping_summary = models.CharField(max_length=255, blank=True)
    is_active = models.BooleanField(default=True, db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["template_name", "id"]

    def __str__(self) -> str:
        return self.template_name
