import uuid

from django.db import models

from apps.accounts.models import TimeStampedModel, User


class Goal(TimeStampedModel):
    """A life/financial goal the user is saving toward. The 'simulator' math
    (how much per month to hit the date, when you'll actually arrive at your
    current pace) is derived in the serializer, not stored."""

    class Category(models.TextChoices):
        HOME = 'home', 'Home / Land'
        VEHICLE = 'vehicle', 'Vehicle'
        WEDDING = 'wedding', 'Wedding'
        EDUCATION = 'education', 'Education'
        RETIREMENT = 'retirement', 'Retirement'
        EMERGENCY = 'emergency_fund', 'Emergency Fund'
        TRAVEL = 'travel', 'Travel'
        BUSINESS = 'business', 'Business'
        OTHER = 'other', 'Other'

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='goals')
    name = models.CharField(max_length=255)
    category = models.CharField(max_length=32, choices=Category.choices, default=Category.OTHER)
    target_amount = models.DecimalField(max_digits=14, decimal_places=2)
    current_amount = models.DecimalField(max_digits=14, decimal_places=2, default=0)
    # What the user plans to set aside each month toward this goal.
    monthly_contribution = models.DecimalField(max_digits=14, decimal_places=2, default=0)
    target_date = models.DateField()
    currency = models.CharField(max_length=10, default='KES')

    class Meta:
        ordering = ['target_date', 'created_at']

    def __str__(self):
        return self.name
