from django.db import models
from django.utils.translation import gettext_lazy as _
from uuid import uuid4
from subscriptions.models import SubscriptionTransaction
from django.contrib.auth import get_user_model
class SubPlan(models.Model):
    id = models.UUIDField(
        default=uuid4,
        editable=False,
        primary_key=True,
        verbose_name='ID',
    )
    plan_name=models.CharField(max_length=100)
    plan_description=models.CharField(max_length=500)

    def __str__(self):
        return self.plan_name

class CostPlan(models.Model):
    id = models.UUIDField(
        default=uuid4,
        editable=False,
        primary_key=True,
        verbose_name='ID',
    )
    subplan = models.OneToOneField(
        SubPlan,
        on_delete=models.CASCADE,
        related_name="costs"
    )
    # post= models.IntegerField()
    cost = models.DecimalField(
        blank=True,
        decimal_places=2,
        help_text=_('the cost of the total posts SubcriptionPlan'),
        max_digits=19,
        null=True,
    )
    off = models.FloatField(blank=True,null=True,help_text=_("eg: -2.0%% off"))

    def __str__(self):
        return str(self.subplan)+str(self.cost)

class UserSub(models.Model):
    id = models.UUIDField(
        default=uuid4,
        editable=False,
        primary_key=True,
        verbose_name='ID',
    )
    user = models.ForeignKey(
        get_user_model(),
        help_text=_('the user this subscription applies to'),
        null=True,
        on_delete=models.CASCADE,
       
    )
    subscription = models.ForeignKey(
        CostPlan,
        help_text=_('the plan costs and billing frequency for this user'),
        null=True,
        on_delete=models.CASCADE,
           )
    # allowed_posts= models.IntegerField()
  
    
    # def __str__(self):
    #     return self.user

class SubTransaction(models.Model):
   
    """Details for a subscription plan billing."""
    id = models.UUIDField(
        default=uuid4,
        editable=False,
        primary_key=True,
        verbose_name='ID',
    )
    user = models.ForeignKey(
        get_user_model(),
        help_text=_('the user that this subscription was billed for'),
        null=True,
        on_delete=models.SET_NULL,
   
    )
    subscription = models.ForeignKey(
        CostPlan,
        help_text=_('the plan costs that were billed'),
        null=True,
        on_delete=models.SET_NULL,
          )
    date_transaction = models.DateTimeField(
        help_text=_('the datetime the transaction was billed'),
        verbose_name='transaction date',
    )
    amount = models.DecimalField(
        blank=True,
        decimal_places=4,
        help_text=_('how much was billed for the user'),
        max_digits=19,
        null=True,
    )
    stripe_session_id = models.CharField(max_length=500,null=True,blank=True,unique=True)
    payment_status = models.CharField(max_length=100,null=True,blank=True) 
    stripe_customer_id = models.CharField(max_length=250,null=True,blank=True)
    class Meta:
        ordering = ('-date_transaction', 'user',)
