from django.db import models
from django.contrib.auth import get_user_model
from courses.models import Course

User = get_user_model()

class Order(models.Model):
    STATUS_CHOICES = [
        ("PENDING", "Pendiente"),
        ("PAID", "Pagado"),
        ("FAILED", "Fallido"),
    ]

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="orders", verbose_name="Usuario")
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name="orders", verbose_name="Curso")
    amount = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Monto (PEN)")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="PENDING", verbose_name="Estado")
    transaction_id = models.CharField(max_length=100, blank=True, null=True, verbose_name="ID de Transacción (Niubiz)")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="Fecha de Creación")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="Última Actualización")

    class Meta:
        verbose_name = "Orden de Compra"
        verbose_name_plural = "Órdenes de Compra"
        ordering = ["-created_at"]

    def __str__(self):
        return f"Orden #{self.id} - {self.user.username} - {self.course.title}"
