from django.db import models
from django.conf import settings

User = settings.AUTH_USER_MODEL


# ================= TENANT PROFILE =================
class TenantProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    unit = models.ForeignKey(
        "properties.Unit",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="tenants"
    )

    id_number = models.CharField(max_length=50)
    id_image = models.ImageField(upload_to='ids/', null=True, blank=True)
    emergency_contact = models.CharField(max_length=15, blank=True)
    mpesa_number = models.CharField(max_length=15, blank=True)

    occupants = models.IntegerField(default=1)

    def __str__(self):
        return self.user.username if self.user else "TenantProfile"


# ================= TENANT APPLICATION =================
class TenantApplication(models.Model):
    STATUS_CHOICES = (
        ('pending', 'Pending'),
        ('approved', 'Approved'),
        ('rejected', 'Rejected'),
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE)

    unit = models.ForeignKey(
        "properties.Unit",
        on_delete=models.CASCADE,
        null=True,
        blank=True
    )

    full_name = models.CharField(max_length=255)
    id_number = models.CharField(max_length=50)
    id_image = models.ImageField(upload_to='ids/')
    emergency_contact = models.CharField(max_length=15)
    mpesa_number = models.CharField(max_length=15)
    occupants = models.IntegerField(default=1)

    inspection_notes = models.TextField(blank=True)
    inspection_images = models.ImageField(upload_to='inspections/', blank=True, null=True)

    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
    submitted_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.full_name} - {self.status}"