from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model

from django.utils import timezone
from datetime import datetime, timedelta

from uncloud_pay.models import *
from uncloud_vm.models import *

import sys

def vm_price_2020(cpu=1, ram=2, v6only=False):
    if v6only:
        discount = 9
    else:
        discount = 0

    return cpu*3 + ram*4 - discount

def disk_price_2020(size_in_gb, disk_type):
    if disk_type == VMDiskType.CEPH_SSD:
        price = 3.5/10
    elif disk_type == VMDiskType.CEPH_HDD:
        price = 1.5/100
    else:
        raise Exception("not yet defined price")

    return size_in_gb * price

class Command(BaseCommand):
    help = 'Adding VMs / creating orders for user'

    def add_arguments(self, parser):
        parser.add_argument('--username', type=str, required=True)

    def handle(self, *args, **options):
        user = get_user_model().objects.get(username=options['username'])

        addr, created = BillingAddress.objects.get_or_create(
            owner=user,
            active=True,
            defaults={'organization': 'Undefined organisation',
                      'name': 'Undefined name',
                      'street': 'Undefined Street',
                      'city': 'Undefined city',
                      'postal_code': '8750',
                      'country': 'CH',
                      'active': True
            }
        )

        # 25206 + SSD
        vm25206 = VMProduct.objects.create(name="one-25206", cores=1, ram_in_gb=4, owner=user)
        vm25206.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))

        # vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206, owner=user, size_in_gb=30)
        # vm25206_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))

        # change 1
        vm25206.cores = 2
        vm25206.ram_in_gb = 8
        vm25206.save()
        vm25206.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))

        sys.exit(0)

        # change 2
        # vm25206_ssd.size_in_gb = 50
        # vm25206_ssd.save()
        # vm25206_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))

        # 25206 done.

        # 25615
        vm25615 = VMProduct.objects.create(name="one-25615", cores=1, ram_in_gb=4, owner=user)
        vm25615.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))

        # Change 2020-04-17
        vm25615.cores = 2
        vm25615.ram_in_gb = 8
        vm25615.save()
        vm25615.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))

        # vm25615_ssd = VMDiskProduct(vm=vm25615, owner=user, size_in_gb=30)
        # vm25615_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
        # vm25615_ssd.save()

        vm25208 = VMProduct.objects.create(name="one-25208", cores=1, ram_in_gb=4, owner=user)
        vm25208.create_order_at(timezone.make_aware(datetime.datetime(2020,3,5)))

        vm25208.cores = 2
        vm25208.ram_in_gb = 8
        vm25208.save()
        vm25208.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))

        Bill.create_next_bills_for_user(user, ending_date=end_of_month(timezone.make_aware(datetime.datetime(2020,7,31))))

        sys.exit(0)


        vm25615_ssd.size_in_gb = 50
        vm25615_ssd.save()
        vm25615_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))



        vm25208_ssd = VMDiskProduct.objects.create(vm=vm25208,
                                    owner=user,
                                    size_in_gb=30)



        vm25208_ssd.size_in_gb = 50
        vm25208_ssd.save()
        vm25208_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))


        # 25207
        vm25207 = VMProduct.objects.create(name="OpenNebula 25207",
                            cores=1,
                            ram_in_gb=4,
                            owner=user)

        vm25207_ssd = VMDiskProduct.objects.create(vm=vm25207,
                                    owner=user,
                                    size_in_gb=30)

        vm25207_ssd.size_in_gb = 50
        vm25207_ssd.save()
        vm25207_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))


        vm25207.cores = 2
        vm25207.ram_in_gb = 8
        vm25207.save()
        vm25207.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,6,19)))


        # FIXES: check starting times (they are slightly different)
        # add vm 25236
