import uuid

from django.db import models
from uncloud_pay.models import Product, RecurringPeriod, AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS
from uncloud_vm.models import VMProduct, VMDiskImageProduct
from django.core.validators import MinValueValidator

class MatrixServiceProduct(Product):
    monthly_managment_fee = 20

    description = "Managed Matrix HomeServer"

    # Specific to Matrix-as-a-Service
    vm = models.ForeignKey(
            VMProduct, on_delete=models.CASCADE
            )
    domain = models.CharField(max_length=255, default='domain.tld')

    # Default recurring price is PER_MONT, see Product class.
    def recurring_price(self, recurring_period=RecurringPeriod.PER_30D):
        return self.monthly_managment_fee

    @staticmethod
    def base_image():
        # TODO: find a way to safely reference debian 10 image.
        return VMDiskImageProduct.objects.get(uuid="93e564c5-adb3-4741-941f-718f76075f02")

    @staticmethod
    def allowed_recurring_periods():
        return list(filter(
            lambda pair: pair[0] in [RecurringPeriod.PER_30D],
            RecurringPeriod.choices))

    @property
    def one_time_price(self):
        return 30

class GenericServiceProduct(Product):
    custom_description = models.TextField()
    custom_recurring_price = models.DecimalField(default=0.0,
            max_digits=AMOUNT_MAX_DIGITS,
            decimal_places=AMOUNT_DECIMALS,
            validators=[MinValueValidator(0)])
    custom_one_time_price = models.DecimalField(default=0.0,
            max_digits=AMOUNT_MAX_DIGITS,
            decimal_places=AMOUNT_DECIMALS,
            validators=[MinValueValidator(0)])

    @property
    def recurring_price(self):
        # FIXME: handle recurring_period somehow.
        return self.custom_recurring_price

    @property
    def description(self):
        return self.custom_description

    @property
    def one_time_price(self):
        return self.custom_one_time_price

    @staticmethod
    def allowed_recurring_periods():
        return RecurringPeriod.choices