"""
Currency Models — Platform Core

مدل‌های ارز و نرخ تبدیل برای استفاده تمام ماژول‌ها.
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
from apps.core.tenant.models import TenantAwareModel


class Currency(TenantAwareModel):
    """
    مدل ارز.

    هر Tenant می‌تواند لیست ارزهای مورد استفاده‌اش را تعریف کند.
    """

    code = models.CharField(
        _("کد ارز"),
        max_length=10,
        help_text="ISO 4217 — مثلاً IRR, USD, EUR",
    )
    name = models.CharField(_("نام ارز"), max_length=100)
    name_en = models.CharField(
        _("نام انگلیسی"), max_length=100, blank=True
    )
    symbol = models.CharField(
        _("نماد"), max_length=10, blank=True,
        help_text="مثلاً ﷼, $, €",
    )
    decimal_places = models.PositiveSmallIntegerField(
        _("تعداد اعشار"), default=0,
        help_text="تعداد رقم اعشار برای این ارز",
    )
    is_base = models.BooleanField(
        _("ارز پایه"), default=False,
        help_text="آیا ارز پایه Tenant است؟",
    )
    is_active = models.BooleanField(
        _("فعال"), default=True, db_index=True
    )

    # Audit
    created_at = models.DateTimeField(_("تاریخ ایجاد"), auto_now_add=True)
    updated_at = models.DateTimeField(_("تاریخ بروزرسانی"), auto_now=True)

    class Meta:
        db_table = "currencies"
        verbose_name = _("ارز")
        verbose_name_plural = _("ارزها")
        unique_together = [["tenant", "code"]]
        ordering = ["-is_base", "code"]
        indexes = [
            models.Index(fields=["tenant", "is_active"]),
        ]

    def __str__(self):
        return f"{self.code} — {self.name}"


class ExchangeRate(TenantAwareModel):
    """
    نرخ تبدیل بین دو ارز.
    """

    from_currency = models.ForeignKey(
        Currency,
        on_delete=models.CASCADE,
        related_name="rates_from",
        verbose_name=_("ارز مبدأ"),
    )
    to_currency = models.ForeignKey(
        Currency,
        on_delete=models.CASCADE,
        related_name="rates_to",
        verbose_name=_("ارز مقصد"),
    )
    rate = models.DecimalField(
        _("نرخ تبدیل"),
        max_digits=18,
        decimal_places=6,
    )
    effective_date = models.DateField(
        _("تاریخ اعتبار"), db_index=True
    )
    source = models.CharField(
        _("منبع"), max_length=100, blank=True,
        help_text="منبع نرخ — مثلاً بانک مرکزی، سنا، …",
    )

    # Audit
    created_at = models.DateTimeField(_("تاریخ ایجاد"), auto_now_add=True)
    created_by = models.UUIDField(
        _("ایجاد توسط"), null=True, blank=True
    )

    class Meta:
        db_table = "exchange_rates"
        verbose_name = _("نرخ تبدیل")
        verbose_name_plural = _("نرخ‌های تبدیل")
        unique_together = [
            ["tenant", "from_currency", "to_currency", "effective_date"]
        ]
        ordering = ["-effective_date"]
        indexes = [
            models.Index(
                fields=["tenant", "from_currency", "to_currency", "effective_date"]
            ),
        ]

    def __str__(self):
        return (
            f"{self.from_currency.code}→{self.to_currency.code} "
            f"= {self.rate} ({self.effective_date})"
        )
