"""
Deposit models for the crypto trading platform.
"""
from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class DepositStatus(models.TextChoices):
    PENDING = 'PENDING', 'Pending'
    APPROVED = 'APPROVED', 'Approved'
    REJECTED = 'REJECTED', 'Rejected'


class PaymentMethod(models.TextChoices):
    BITCOIN = 'BITCOIN', 'Bitcoin'
    ETHEREUM = 'ETHEREUM', 'Ethereum'
    BINANCE_COIN = 'BINANCE_COIN', 'Binance Coin'
    USDT_TRC20 = 'USDT_TRC20', 'USDT (TRC20)'
    USDT_ERC20 = 'USDT_ERC20', 'USDT (ERC20)'
    BANK_TRANSFER = 'BANK_TRANSFER', 'Bank Transfer'
    OTHER = 'OTHER', 'Other'


class Deposit(models.Model):
    """
    Model to store deposit requests from users.
    Users submit proof of payment and admin verifies.
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='deposits')
    amount = models.DecimalField(max_digits=15, decimal_places=2)
    payment_method = models.CharField(max_length=50, choices=PaymentMethod.choices)
    transaction_id = models.CharField(max_length=200, blank=True)
    wallet_address = models.CharField(max_length=200, blank=True)
    screenshot = models.ImageField(upload_to='deposit_screenshots/', null=True, blank=True)
    notes = models.TextField(blank=True)
    status = models.CharField(max_length=20, choices=DepositStatus.choices, default=DepositStatus.PENDING)
    
    # Admin fields
    verified_by = models.ForeignKey(
        User, 
        on_delete=models.SET_NULL, 
        null=True, 
        blank=True,
        related_name='verified_deposits'
    )
    verification_notes = models.TextField(blank=True)
    verified_at = models.DateTimeField(null=True, blank=True)
    
    # Timestamps
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        verbose_name = 'Deposit'
        verbose_name_plural = 'Deposits'
        ordering = ['-created_at']
    
    def __str__(self):
        return f"Deposit #{self.id} - {self.user.username} - {self.amount}"


class PaymentInfo(models.Model):
    """
    Model to store payment information for deposits.
    Admin can manage crypto wallet addresses.
    """
    payment_method = models.CharField(max_length=50, choices=PaymentMethod.choices)
    wallet_address = models.CharField(max_length=200)
    qr_code = models.ImageField(upload_to='payment_qr_codes/', null=True, blank=True)
    instructions = models.TextField(blank=True)
    is_active = models.BooleanField(default=True)
    minimum_amount = models.DecimalField(max_digits=15, decimal_places=2, default=10.00)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        verbose_name = 'Payment Information'
        verbose_name_plural = 'Payment Information'
    
    def __str__(self):
        return f"{self.get_payment_method_display()} - {self.wallet_address[:20]}..."
