from django.db import models
from django.db import models
from django.utils import timezone
from datetime import timedelta

class FormSubmissionLog(models.Model):
    ip_address = models.GenericIPAddressField()
    submission_time = models.DateTimeField(auto_now_add=True)
    
    @classmethod
    def get_submission_count(cls, ip_address, hours=24):
        time_threshold = timezone.now() - timedelta(hours=hours)
        return cls.objects.filter(
            ip_address=ip_address,
            submission_time__gte=time_threshold
        ).count()
    
    @classmethod
    def can_submit(cls, ip_address, max_submissions=2, hours=24):
        count = cls.get_submission_count(ip_address, hours)
        return count < max_submissions


class Contact(models.Model):
    fullname = models.CharField(max_length=255 , verbose_name="نام و نام خانوادگی")
    email = models.EmailField()
    options = models.CharField(max_length=255)
    phone = models.CharField(max_length=255 ,blank=True)
    tozihat = models.TextField()

    def __str__(self):
        return self.fullname