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


# ------------------ Deposits ------------------ #
class USSDDepositInitiations(models.Model):
    STATUSES = (
        ('pending', 'Pending'),
        ('failed', 'Declined'),
        ('processed', 'Processed')
    )
    amount = models.FloatField()
    charge = models.FloatField()
    phone = models.CharField(max_length=15)
    member_number = models.CharField(max_length=50)
    transaction_id = models.CharField(max_length=32)
    organisation_id = models.IntegerField()
    account = models.ForeignKey(SavingAccount, on_delete=models.CASCADE, related_name='saving_account')
    status = models.CharField(max_length=50, choices=STATUSES, default='pending')
    date_added = models.DateTimeField(default=timezone.now)
    last_updated = models.DateTimeField(null=True, blank=True)
    
    class Meta:
        db_table = 'public"."ussd_deposit_initiations'


class USSDDepositLiquidations(models.Model):
    PAYMENT_OPTIONS = (
        ('mobile-money', 'Mobile Money'),
        ('bank', 'Bank'),
    )
    REQUEST_STATUS = (
        ('pending', 'Pending'),
        ('approved', 'Approved'),
        ('canceled', 'Canceled'),
    )

    requested_amount = models.FloatField(default=0)
    approved_amount = models.FloatField(default=0)
    selected_account = models.ForeignKey(OrganisationSubAccount, on_delete=models.CASCADE, related_name='selected_account', null=True, blank=True)
    payment_method = models.CharField(max_length=50, choices=PAYMENT_OPTIONS, default='e-wallet')
    status = models.CharField(max_length=50, choices=REQUEST_STATUS, default='pending')
    organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE, related_name='organisation_liquidation')
    transaction_date = models.DateTimeField(default=timezone.now)
    request_added_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='request_added_by')
    approved_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='liquidation_approved_by', blank=True, null=True)
    request_last_updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='request_last_updated_by', null=True, blank=True)
    comment = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'public"."ussd_deposit_liquidations'


class LiquidationTransactions(models.Model):
    request = models.ForeignKey(USSDDepositLiquidations, on_delete=models.CASCADE, related_name='liquidation_requests')
    transaction = models.ForeignKey(SystemTransactions, on_delete=models.CASCADE, related_name='approved_transactions')
    date_added = models.DateTimeField(default=timezone.now)
    last_updated = models.DateTimeField(default=timezone.now)

    class Meta:
        db_table = 'public"."liquidation_approval_transactions'


# ------------------ Withdrawals ------------------ #
class USSDWithdrawalInitiations(models.Model):
    STATUSES = (
        ('pending', 'Pending'),
        ('failed', 'Declined'),
        ('processed', 'Processed')
    )
    amount = models.FloatField()
    charge = models.FloatField()
    phone = models.CharField(max_length=15)
    member_number = models.CharField(max_length=50)
    transaction_id = models.CharField(max_length=32)
    organisation_id = models.IntegerField()
    account = models.ForeignKey(SavingAccount, on_delete=models.CASCADE, related_name='withdrawal_account')
    status = models.CharField(max_length=50, choices=STATUSES, default='pending')
    date_added = models.DateTimeField(default=timezone.now)
    last_updated = models.DateTimeField(null=True, blank=True)
    
    class Meta:
        db_table = 'public"."ussd_withdrawal_initiations'


class USSDWithdrawalLiquidations(models.Model):
    PAYMENT_OPTIONS = (
        ('mobile-money', 'Mobile Money'),
        ('bank', 'Bank'),
    )
    REQUEST_STATUS = (
        ('pending', 'Pending'),
        ('approved', 'Approved'),
        ('canceled', 'Canceled'),
    )

    requested_amount = models.FloatField(default=0)
    approved_amount = models.FloatField(default=0)
    selected_account = models.ForeignKey(OrganisationSubAccount, on_delete=models.CASCADE, related_name='withdrawal_selected_account', null=True, blank=True)
    payment_method = models.CharField(max_length=50, choices=PAYMENT_OPTIONS, default='e-wallet')
    status = models.CharField(max_length=50, choices=REQUEST_STATUS, default='pending')
    organisation = models.ForeignKey(Organisation, on_delete=models.CASCADE, related_name='withdrawal_organisation')
    transaction_date = models.DateTimeField(default=timezone.now)
    request_added_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='withdrawal_request_added_by')
    approved_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='withdrawal_liquidation_approved_by', blank=True, null=True)
    request_last_updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='withdrawal_request_last_updated_by', null=True, blank=True)
    comment = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'public"."ussd_withdrawal_liquidations'


# ------------------ Mobile Money Service Providers ------------------ #
class MMServiceProvider(models.Model):
    PROVIDER_STATUSES = (
        ('active', 'Active'),
        ('inactive', 'Inactive')
    )
    DEPOSIT_STATUSES = (
        ('active', 'Active'),
        ('inactive', 'Inactive')
    )
    WITHDRAW_STATUSES = (
        ('active', 'Active'),
        ('inactive', 'Inactive')
    )
    
    name = models.CharField(max_length=255)
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=255)
    api_url = models.TextField()
    api_key = models.TextField()
    api_password = models.TextField()
    status = models.CharField(max_length=50, choices=PROVIDER_STATUSES, default='active')
    unique_identifier = models.CharField(max_length=50, blank=True, null=True)
    deposit = models.CharField(max_length=50, choices=DEPOSIT_STATUSES, default='active')
    withdraw = models.CharField(max_length=50, choices=WITHDRAW_STATUSES, default='active')
    
    class Meta:
        db_table = 'public"."mmservice_provider'


class MMServiceProviderTarrif(models.Model):
    TRANSACTION_TYPES = (
        ('deposit_charge', "Deposit Charge"),
        ('withdraw_charge', 'Withdraw Charge'),
    )
    CHARGE_TYPE = (
        ('percentage', "Percentage"),
        ('flat', 'Flat'),
    )
    charge_name = models.CharField(max_length=155)
    transaction_type = models.CharField(max_length=155, choices=TRANSACTION_TYPES)
    charge = models.FloatField(default=0.0)
    min_value = models.FloatField(default=0.0)
    max_value = models.FloatField(default=0.0)
    charge_type = models.CharField(max_length=100, choices=CHARGE_TYPE)
    date_added = models.DateTimeField(default=timezone.now)
    last_updated = models.DateTimeField(default=timezone.now)
    provider = models.ForeignKey(MMServiceProvider, on_delete=models.CASCADE, related_name='mmservice_provider_tarrif_provider')
    tarrif_added_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='tarrif_added_by')
    tarrif_last_updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='tarrif_last_updated_by', null=True, blank=True)
    deleted = models.BooleanField(default=False)

    class Meta:
        db_table = 'public"."mmservice_provider_tarrif'
        
        
# from django.db import models
# from organisations.models import *
# from django.utils import timezone
# from ledgers.models import *
# from savings.models import *

# class USSDDepositInitiations(models.Model):
#     STATUSES = (
#         ('pending', 'Pending'),
#         ('failed', 'Declined'),
#         ('processed', 'Processed')
#     )
#     amount = models.FloatField()
#     charge = models.FloatField()
#     phone =  models.CharField(max_length=15)
#     member_number = models.CharField(max_length=50)
#     transaction_id =  models.CharField(max_length=32)
#     organisation_id = models.IntegerField(max_length=50)
#     account = models.ForeignKey(SavingAccount, on_delete=models.CASCADE, related_name='saving_account')
#     status       =  models.CharField(max_length=50, choices=STATUSES, default='pending')
#     date_added   = models.DateTimeField(default = timezone.now)
#     last_updated = models.DateTimeField(null=True, blank=True)
    
#     class Meta:
#         db_table = 'public\".\"ussd_deposit_initiations'

# class USSDDepositLiquidations(models.Model):
#     PAYMENT_OPTIONS = (
#         ('mobile-money', 'Mobile Money'),
#         ('bank', 'Bank'),
#     )
#     REQUEST_STATUS = (
#     ('pending', 'Pending'),
#     ('approved', 'Approved'),
#     ('canceled', 'Canceled'),
# )
    
#     requested_amount        = models.FloatField(default=0)
#     approved_amount         = models.FloatField(default=0)
#     selected_account        = models.ForeignKey(OrganisationSubAccount, on_delete=models.CASCADE, related_name='selected_account', null=True, blank=True)
#     payment_method          = models.CharField(max_length=50, choices=PAYMENT_OPTIONS, default='e-wallet')
#     status                  = models.CharField(max_length=50, choices=REQUEST_STATUS, default='pending')
#     organisation            = models.ForeignKey(Organisation, on_delete=models.CASCADE, related_name='organisation_liquidation')
#     transaction_date        = models.DateTimeField(default = timezone.now)
#     request_added_by        = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='request_added_by', blank=False, null=False)
#     approved_by             = models.ForeignKey(User, on_delete=models.CASCADE, related_name='liquidation_approved_by', blank=True, null=True)
#     request_last_updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='request_last_updated_by',null=True, blank=True)
#     comment                 = models.CharField(max_length=255, blank=True, null=True)



#     class Meta:
#         db_table = 'public\".\"ussd_deposit_liquidations'

# class LiquidationTransactions(models.Model):
#     request      = models.ForeignKey(USSDDepositLiquidations, on_delete=models.CASCADE, related_name='liquidation_requests', blank=False, null=False)
#     transaction  = models.ForeignKey(SystemTransactions, on_delete=models.CASCADE, related_name='approved_transactions', blank=False, null=False)
#     date_added   = models.DateTimeField(default=timezone.now)
#     last_updated = models.DateTimeField(default=timezone.now)
#     class Meta:
#         db_table     ='public\".\"liquidation_approval_transactions'


# class MMServiceProvider(models.Model):
#     PROVIDER_STATUSES = (
#         ('active', 'Active'),
#         ('inactive', 'Inactive')
#     )
#     DEPOSIT_STATUSES = (
#         ('active', 'Active'),
#         ('inactive', 'Inactive')
#     )

#     WITHDRAW_STATUSES = (
#         ('active', 'Active'),
#         ('inactive', 'Inactive')
#     )
    
#     name              = models.CharField(max_length=255,)
#     username          = models.CharField(max_length=255,)
#     password          = models.CharField(max_length=255,)
#     name              = models.CharField(max_length=255,)
#     api_url           = models.TextField()
#     api_key           = models.TextField()
#     api_password      = models.TextField()
#     status            = models.CharField(max_length=50, choices=PROVIDER_STATUSES, default='inactive')
#     unique_identifier = models.CharField(max_length=50, blank=True, null=True)
#     status            = models.CharField(max_length=50, choices=PROVIDER_STATUSES, default='active')
#     deposit           = models.CharField(max_length=50, choices=DEPOSIT_STATUSES, default='active')
#     withdraw          = models.CharField(max_length=50, choices=WITHDRAW_STATUSES,  default='active')
    
#     class Meta:
#         db_table = 'public\".\"mmservice_provider'

# class MMServiceProviderTarrif(models.Model):
#     TRANSACTION_TYPES = (
#         ('deposit_charge', "Deposit Charge"),
#         ('withdraw_charge', 'Withdraw Charge'),
#     )
#     CHARGE_TYPE = (
#         ('percentage', "Percentage"),
#         ('flat', 'Flat'),
#     )
#     charge_name =  models.CharField(max_length=155)
#     transaction_type  =  models.CharField(max_length=155,choices=TRANSACTION_TYPES)
#     charge    =  models.FloatField(default=0.0)
#     min_value =  models.FloatField(default=0.0)
#     max_value =  models.FloatField(default=0.0)
#     charge_type = models.CharField(max_length=100, choices=CHARGE_TYPE)
#     date_added = models.DateTimeField(default = timezone.now)
#     last_updated = models.DateTimeField(default = timezone.now)
#     provider     =  models.ForeignKey(MMServiceProvider, on_delete=models.CASCADE, related_name='mmservice_provider_tarrif_provider')
#     tarrif_added_by =  models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='tarrif_added_by')
#     tarrif_last_updated_by =  models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='tarrif_last_updated_by',null=True, blank=True)
#     deleted =  models.BooleanField(default=False)

#     class Meta:
#         db_table = 'public\".\"mmservice_provider_tarrif'