from django.db import models
from django.utils import timezone
from django.conf import settings
from organisations.models import *
from ledgers.models import SystemTransactions

class LicenseSubscription(models.Model):
    LICENSE_PERIOD_TYPE = (
        ('d', 'Days'),
        ('m', 'Months'),
        ('w', 'Weeks'),
        ('bw','Bi-Weeks'),
        ('q', 'Quarterly'),
        ('y', 'Years'),
        )
    LICENSE_PACKAGES = (
       ('platinum','Platinum'),
       ('gold','Gold'),
       ('bronze','Bronze'),
       ('silver','Silver'),
    )
    CALCULATION_TYPES = (
       ('annual-flat','Annual Flat Fee'),
       ('active-members','Calculate By Active Members')
    )
    STATUSES = (
        ('active','Active'),
        ('inactive','Inactive'),
    )
    PAYMENT_OPTIONS = (
        ('e-payment', 'E Payment'),
        ('bank', 'Bank'),
        ('cash', 'Cash'),
        ('mobile','Mobile Money')
    )
    organisation            = models.ForeignKey(Organisation,on_delete=models.CASCADE, related_name='org_license_sub',default=2)
    subcription_date        = models.DateTimeField(default = timezone.now)
    period_type             = models.CharField(max_length=255, choices=LICENSE_PERIOD_TYPE, default='d')
    package                 = models.CharField(max_length=255, choices=LICENSE_PACKAGES, default='platinum')
    calculation_types       = models.CharField(max_length=255, choices=CALCULATION_TYPES, default='annual-flat')
    grace_period            = models.IntegerField(blank=True,null=True)
    period                  = models.IntegerField(blank=True,null=True)
    amount                  = models.FloatField(null=True,default=0)
    license_added_by        = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='license_sub_added_by')
    license_last_updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='license_sub_last_updated_by',null=True, blank=True)
    record_date             = models.DateTimeField(default = timezone.now)
    date_added              = models.DateTimeField(default = timezone.now)
    last_updated            = models.DateTimeField(default = timezone.now)
    parent_org              = models.ForeignKey(Organisation,on_delete=models.CASCADE, related_name='subscription_org')
    grace_period_type       = models.CharField(max_length=255, choices=LICENSE_PERIOD_TYPE, default='d')
    status                  = models.CharField(max_length=255, choices=STATUSES,default='active')
    force_inactive          = models.BooleanField(default=False)
    payment_method          = models.CharField(max_length=255, choices=PAYMENT_OPTIONS, default='cash')
    selected_account        = models.CharField(max_length=255,blank=True,null=True)
    revised_members         = models.IntegerField(null=True,default=0)
    revised_amount          = models.FloatField(null=True,default=0)
    transaction             = models.ForeignKey(SystemTransactions,on_delete=models.CASCADE,related_name='system_license_transactions')
    start_date                = models.DateTimeField(default = timezone.now) 
    end_date                = models.DateTimeField(default = timezone.now) 

    class Meta:
        db_table = 'public\".\"license_subscription'


class LicensePayments(models.Model):
    PAYMENT_OPTIONS = (
        ('e-payment', 'E Payment'),
        ('bank', 'Bank'),
        ('cash', 'Cash'),
    )
    license_subscription = models.ForeignKey(LicenseSubscription, on_delete=models.CASCADE, related_name='license_subscription')
    amount_paid          = models.FloatField(null=False,default=0)
    remaining_balance    = models.FloatField(null=False,default=0)
    payment_method       = models.CharField(max_length=255, choices=PAYMENT_OPTIONS, default='cash')
    selected_account     = models.CharField(max_length=255,blank=True,null=True)
    record_date          = models.DateTimeField(default = timezone.now)
    license_date         = models.DateTimeField(default = timezone.now)
    date_added           = models.DateTimeField(default = timezone.now)
    payment_added_by     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='payment_added_by')
    transaction          = models.ForeignKey(SystemTransactions,on_delete=models.CASCADE,related_name='system_license_payments')


