from django.db import models
from django.conf import settings
from django.utils import timezone
from admissions.models import Enrollment


class Payment(models.Model):
    TERM_1 = 'term_1'
    TERM_2 = 'term_2'
    TERM_3 = 'term_3'
    TERM_CHOICES = [
        (TERM_1, 'Term 1'),
        (TERM_2, 'Term 2'),
        (TERM_3, 'Term 3'),
    ]

    TUITION = 'tuition'
    PRACTICAL = 'practical'
    ASSESSMENT = 'assessment'
    EXAM = 'exam'
    ADMISSION = 'admission'
    OTHER = 'other'
    PAYMENT_TYPE_CHOICES = [
        (TUITION, 'Tuition Fee'),
        (PRACTICAL, 'Practical / Project Fee'),
        (ASSESSMENT, 'Assessment Fee'),
        (EXAM, 'Exam Fee'),
        (ADMISSION, 'Admission Fee'),
        (OTHER, 'Other'),
    ]

    MOBILE_MONEY = 'mobile_money'
    BANK = 'bank'
    CASH = 'cash'
    ONLINE = 'online'
    PAYMENT_METHOD_CHOICES = [
        (MOBILE_MONEY, 'Mobile Money'),
        (BANK, 'Bank Transfer'),
        (CASH, 'Cash'),
        (ONLINE, 'Online (Flutterwave)'),
        (OTHER, 'Other'),
    ]

    enrollment = models.ForeignKey(Enrollment, on_delete=models.CASCADE, related_name='payments')
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    payment_type = models.CharField(max_length=20, choices=PAYMENT_TYPE_CHOICES)
    term = models.CharField(max_length=10, choices=TERM_CHOICES)
    academic_year = models.CharField(max_length=9, help_text='e.g. 2024/2025')
    payment_method = models.CharField(max_length=20, choices=PAYMENT_METHOD_CHOICES, default=MOBILE_MONEY)
    reference = models.CharField(max_length=100, blank=True, help_text='Transaction ID, bank slip no., receipt no., etc.')
    payment_date = models.DateField()
    notes = models.TextField(blank=True)
    recorded_by = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.SET_NULL,
        null=True, blank=True, related_name='recorded_payments'
    )
    # Flutterwave online payment fields
    flw_tx_ref = models.CharField(max_length=100, unique=True, null=True, blank=True)
    flw_transaction_id = models.CharField(max_length=100, blank=True)

    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-payment_date', '-created_at']

    def __str__(self):
        return (
            f"{self.enrollment.student.get_full_name()} — "
            f"{self.get_payment_type_display()} {self.get_term_display()} "
            f"{self.academic_year} — UGX {self.amount:,.0f}"
        )


class Donation(models.Model):
    PENDING = 'pending'
    COMPLETED = 'completed'
    FAILED = 'failed'
    STATUS_CHOICES = [
        (PENDING, 'Pending'),
        (COMPLETED, 'Completed'),
        (FAILED, 'Failed'),
    ]

    name = models.CharField(max_length=200)
    email = models.EmailField()
    phone = models.CharField(max_length=30, blank=True)
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    message = models.TextField(blank=True)
    flw_tx_ref = models.CharField(max_length=100, unique=True)
    flw_transaction_id = models.CharField(max_length=100, blank=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default=PENDING)
    created_at = models.DateTimeField(auto_now_add=True)
    completed_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.name} — UGX {self.amount:,.0f} ({self.get_status_display()})"
