from django.core.management.base import BaseCommand
from app.models import *


class Command(BaseCommand):
    help = 'Add test data'

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

    def handle(self, *args, **options):
        # Add CHF as currency
        currency, created = Currency.objects.get_or_create(defaults=
                                       {
                                           "slug": "CHF",
                                           "name": "Swiss Franc",
                                           "short_name": "CHF"
                                       })

        # Add standard timeframes
        for timeframe in [ (3600, "1 hour", "1-hour"),
                       (86400, "1 day", "1-day"),
                       (7*86400, "7 days", "7-days"),
                       (30*86400, "30 days", "30-days"),
                       (365*86400, "365 days", "365 days") ]:
            TimeFrame.objects.get_or_create(slug=timeframe[2],
                                        defaults=
                                        {
                                            "name": timeframe[1],
                                            "seconds": timeframe[0]
                                        })

        tf_30d = TimeFrame.objects.get(slug='30-days')

        # Add typical prices per timeframe
        for ppt in [
                ("1-day", 1, currency),
                ("1-day", 2, currency),
                ("30-days",  2, currency), # HDD 100 GB
                ("30-days",  3, currency), # CPU
                ("30-days",  3.5, currency), # SSD Storage
                ("30-days",  4, currency), # RAM
                ("30-days", 10, currency), # Nextcloud
                ("30-days", 15, currency), # Gitea
                ("30-days", 35, currency), # Matrix
                ("30-days", 29, currency),
                ("30-days", 55, currency) ]:
            tf = TimeFrame.objects.get(slug=ppt[0])

            PricePerTime.objects.get_or_create(timeframe=tf,
                                               value=ppt[1],
                                        defaults=
                                        {
                                            "currency": currency
                                        })

        # Add typical resources
        for res in [
                # slug name description min max step-size price per 30days
                ("cpu-1", "CPU", "Core(s)", None, None, 0.5, 3),
                ("cpu-min-max", "CPU", "Core(s)", 1, 20, 0.5, 3),
                ("ram-1", "RAM", "GB", None, None, 0.5, 4),
                ("ram-min-max", "RAM", "GB", 1, 200, 0.5, 4),
                ("storage-db", "Database-Storage", "GB", 10, None, 10, 3.5),
                ("storage-ssd", "SSD-Storage", "GB", 10, None, 10, 3.5),
                ("storage-hdd", "HDD-Storage", "GB", 100, None, 100, 2),
                ("matrix-maintenance", "Matrix Maintenance Fee", "", 1, 1, None, 35),
                ("nextcloud-maintenance", "Nextcloud Maintenance Fee", "", 1, 1, None, 10),
                ("gitea-maintenance", "Gitea Maintenance Fee", "", 1, 1, None, 15),

                ]:
            this_res, created = Resource.objects.get_or_create(slug=res[0],
                                        defaults=
                                        {
                                            "name": res[1],
                                            "unit": res[2],
                                            "minimum_units": res[3],
                                            "maximum_units": res[4],
                                            "step_size": res[5]
                                        })
            # If price is given, assign it
            if res[6]:
                ppt = PricePerTime.objects.get(timeframe=tf_30d, value=res[6])
                this_res.price_per_time.add(ppt)


        # Link resources to prices per time frame

            # Link to PPT -- later
            # for ppt_res in res[5]:
            #     ppt = PricePerTime.objects.get(

        # Add test products
        for product in [
                ("matrix", "Matrix"),
                ("nextcloud", "Nextcloud"),
                ("gitea", "Gitea") ]:
            p, created = Product.objects.get_or_create(slug=product[0],
                                          defaults = { "name": product[1] })

            for req_res in [ "cpu-min-max",
                             "ram-min-max",
                             "storage-db",
                             "storage-hdd" ]:
                print(f"Adding {req_res} to {p}")
                p.resources.add(Resource.objects.get(slug=req_res))

            p.resources.add(Resource.objects.get(slug=f"{product[0]}-maintenance"))
            # Every test product can be bought for the 30d timeframe
            p.timeframes.add(tf_30d)
