import datetime
import json

from django.test import TestCase
from django.contrib.auth import get_user_model
from django.utils import timezone

from .models import VMInstance
from uncloud_pay.models import Order, PricingPlan, BillingAddress, Product, RecurringPeriod


vm_product_config = {
    'features': {
        'cores':
        { 'min': 1,
          'max': 48
         },
        'ram_gb':
        { 'min': 2,
          'max': 200
         },
    },
}

class VMInstanceTestCase(TestCase):

    def setUp(self):
        RecurringPeriod.populate_db_defaults()
        self.user = get_user_model().objects.create(
            username='random_user',
            email='jane.random@domain.tld')
        self.config = json.dumps({
                            'cores': 1,
                            'memory': 2,
                            'storage': 100,
                            'homeserver_domain': '',
                            'webclient_domain': '',
                            'matrix_domain': '',
                        })
        self.pricing_plan = PricingPlan.objects.create(name="PricingSample", set_up_fees=35, cores_unit_price=3, 
                                                        ram_unit_price=4, storage_unit_price=0.02)
        self.ba = BillingAddress.objects.create(
            owner=self.user,
            organization = 'Test org',
            street="unknown",
            city="unknown",
            postal_code="somewhere else",
            active=True)

        self.product = Product.objects.create(name="Testproduct",
                                   description="Only for testing",
                                   config=vm_product_config)
        self.default_recurring_period = RecurringPeriod.objects.get(name="Per 30 days")
        self.product.recurring_periods.add(self.default_recurring_period,
                                through_defaults= { 'is_default': True })

    def test_create_matrix_vm(self):
        order = Order.objects.create(owner=self.user,
                        recurring_period=self.default_recurring_period,
                        billing_address=self.ba,
                        pricing_plan = self.pricing_plan, 
                        product=self.product,
                        config=self.config)
        instances = VMInstance.objects.filter(order=order)
        self.assertEqual(len(instances), 1)


