import uuid
from django.conf import settings
from django.db import models
class Contribution(models.Model):
 class Purpose(models.TextChoices): OFFERTORY='OFFERTORY','Offertory'; DONATION='DONATION','Donation'; TITHE='TITHE','Tithe'; MISSIONS='MISSIONS','Missions'; WELFARE='WELFARE','Welfare'; PROJECT='PROJECT','Kingdom project'; OTHER='OTHER','Other'
 class Status(models.TextChoices): PENDING='PENDING','Pending'; PROCESSING='PROCESSING','Processing'; SUCCESS='SUCCESS','Successful'; FAILED='FAILED','Failed'; CANCELLED='CANCELLED','Cancelled'; REFUNDED='REFUNDED','Refunded'
 reference=models.CharField(max_length=40,unique=True,editable=False);member=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.PROTECT,related_name='contributions');campus=models.ForeignKey('core.Campus',null=True,blank=True,on_delete=models.SET_NULL);purpose=models.CharField(max_length=20,choices=Purpose.choices);amount=models.DecimalField(max_digits=12,decimal_places=2);currency=models.CharField(max_length=3,default='GHS');anonymous=models.BooleanField(default=False);note=models.CharField(max_length=255,blank=True);provider=models.CharField(max_length=30);provider_reference=models.CharField(max_length=100,blank=True);authorization_url=models.URLField(blank=True);status=models.CharField(max_length=15,choices=Status.choices,default=Status.PENDING);created_at=models.DateTimeField(auto_now_add=True);paid_at=models.DateTimeField(null=True,blank=True);updated_at=models.DateTimeField(auto_now=True);provider_payload=models.JSONField(default=dict,blank=True)
 class Meta:ordering=['-created_at'];indexes=[models.Index(fields=['status','created_at'])]
 def save(self,*args,**kwargs):
  if not self.reference:self.reference=f'HSM-{uuid.uuid4().hex[:16].upper()}'
  super().save(*args,**kwargs)
 def __str__(self):return self.reference
