from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from geography.models import Location  # استيراد الموقع

class SoftDeleteManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(deleted_at__isnull=True)


class EstablishmentType(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name="الاسم")
    code = models.CharField(max_length=20, unique=True, verbose_name="الكود")
    description = models.TextField(blank=True, null=True, verbose_name="الوصف")

    # حقول التدقيق
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    deleted_at = models.DateTimeField(null=True, blank=True)
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='esttype_created')
    updated_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='esttype_updated')
    deleted_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='esttype_deleted')

    objects = SoftDeleteManager()
    all_objects = models.Manager()

    def delete(self, using=None, keep_parents=False):
        self.deleted_at = timezone.now()
        self.save()

    def hard_delete(self):
        super().delete()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "نوع المنشأة"
        verbose_name_plural = "أنواع المنشآت"
        indexes = [
            models.Index(fields=['deleted_at']),
            models.Index(fields=['name']),
        ]


class Establishment(models.Model):
    name_ar = models.CharField(max_length=200, verbose_name="الاسم بالعربية")
    name_en = models.CharField(max_length=200, blank=True, null=True, verbose_name="الاسم بالإنجليزية")
    establishment_type = models.ForeignKey(
        EstablishmentType,
        on_delete=models.PROTECT,
        related_name='establishments',
        verbose_name="نوع المنشأة"
    )
    location = models.ForeignKey(Location, on_delete=models.PROTECT, related_name='establishments', verbose_name="الموقع")
    license_number = models.CharField(max_length=50, unique=True, blank=True, null=True, verbose_name="رقم الترخيص")
    owner_name = models.CharField(max_length=100, blank=True, null=True, verbose_name="اسم المالك")
    phone = models.CharField(max_length=20, blank=True, null=True, verbose_name="الهاتف")
    address = models.TextField(blank=True, null=True, verbose_name="العنوان التفصيلي")

    # حقول التدقيق
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    deleted_at = models.DateTimeField(null=True, blank=True)
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='establishment_created')
    updated_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='establishment_updated')
    deleted_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='establishment_deleted')

    objects = SoftDeleteManager()
    all_objects = models.Manager()

    def delete(self, using=None, keep_parents=False):
        self.deleted_at = timezone.now()
        self.save()

    def hard_delete(self):
        super().delete()

    def __str__(self):
        return self.name_ar

    class Meta:
        verbose_name = "منشأة"
        verbose_name_plural = "المنشآت"
        indexes = [
            models.Index(fields=['deleted_at']),
            models.Index(fields=['name_ar']),
            models.Index(fields=['license_number']),
        ]