from django.db import models
from tenant.models import Tenant
from django.core.validators import MinValueValidator
from room.models import Room


class PaymentLog(models.Model):
    """
    Represents a payment log entry.
    """
    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('paid', 'Paid'),
    ]

    LOG_TYPES = [
        ('electricity', 'Electricity'),
        ('rent', 'Rent'),
        ('food', 'Food')
    ]

    amount = models.FloatField(
        help_text="The amount of the payment.",
        validators=[MinValueValidator(0)],  # Ensure amount is positive
    )
    log_type = models.CharField(
        max_length=11,
        choices=LOG_TYPES,
        default='electricity',
        help_text="The type of the entry (electricity/rent/food).",
    )
    status = models.CharField(
        max_length=10,
        choices=STATUS_CHOICES,
        default='pending',
        help_text="The status of the payment (Pending/Paid).",
    )
    remark = models.TextField(
        help_text="A remark or description of the payment."
    )
    tenant = models.ForeignKey(
        Tenant,
        on_delete=models.CASCADE,  # If a tenant is deleted, delete their logs
        related_name="payment_logs",  # Allow access like tenant.payment_logs
        help_text="The tenant associated with this payment.",
    )
    room = models.ForeignKey(
        Room,
        on_delete=models.DO_NOTHING,  # If a room is deleted, delete its logs
        related_name="payment_logs",  # Allow access like room.payment_logs
        help_text="The room associated with this payment.",
    )


    created_at = models.DateTimeField(editable=True, help_text="Timestamp of creation.")
    updated_at = models.DateTimeField(editable=True, help_text="Timestamp of last update.")


    def __str__(self):
        return f"Payment of {self.amount} for Tenant: {self.tenant.name} in Room: {self.room.room_number}" # type: ignore

    class Meta:
        db_table = 'payment_logs' # custom table name

class CalculationDates(models.Model):
    tenant = models.OneToOneField(Tenant, on_delete=models.CASCADE, related_name='calculation_dates')
    last_rent_calculation = models.DateTimeField(null=True, blank=True)
    last_food_calculation = models.DateTimeField(null=True, blank=True)
    last_electricity_calculation = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return f"Calculation dates for {self.tenant.name}"

    class Meta:
        db_table = 'tenant_calculation_dates'
