from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
import uuid

class Wallet(models.Model):
    # user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='wallet')
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='wallet')
    is_active = models.BooleanField(default=False)
    balance = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)

    def __str__(self):
        return f"{self.user.username}'s Wallet"

class Transaction(models.Model):
    class TransactionTypes(models.TextChoices):
        CREDIT = 'credit', 'Credit'
        DEBIT = 'debit', 'Debit'
        TRANSFER = 'transfer', 'Transfer'
        REFUND = 'refund', 'Refund'

    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('success', 'Success'),
        ('failed', 'Failed'),
    ]

    transaction_id = models.UUIDField(
        default=uuid.uuid4,
        editable=False,
        unique=True
    )
    wallet = models.ForeignKey(Wallet, on_delete=models.CASCADE, related_name='transactions')
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    timestamp = models.DateTimeField(auto_now_add=True)
    transaction_type = models.CharField(max_length=20, choices=TransactionTypes.choices)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending')
    description = models.TextField(blank=True, null=True)

    class Meta:
        indexes = [
            models.Index(fields=['wallet', 'timestamp']),
        ]

    def __str__(self):
        return f'{self.transaction_type} of ₹{self.amount} - {self.status}'
    

class AuditLog(models.Model):
    ACTION_CHOICES = [
        ('wallet_activated', 'Wallet Activated'),
        ('money_added', 'Money Added'),
        ('money_withdrawn', 'Money Withdrawn'),
        ('money_transfer', 'Money Transfer'),
        ('wallet_failed', 'Wallet Operation Failed'),
    ]

    # user = models.ForeignKey(User, on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    action = models.CharField(max_length=50, choices=ACTION_CHOICES)
    message = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True, db_index=True)

    def __str__(self):
        return f"{self.user.username} - {self.action} - {self.timestamp.strftime('%Y-%m-%d %H:%M:%S')}"
    

class Notification(models.Model):
    # user = models.ForeignKey(User, on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    message = models.TextField()
    is_read = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return f"Notification for {self.user.username} - {self.title}"
