from django.db import models
from django.utils import timezone

# Create your models here.
from django.db import models
from Accounts.models import UserProfile
from Device.models import Devices

class OTPVerification(models.Model):
    email = models.EmailField(null=False)  # Store the email as a plain CharField
    otp_code = models.CharField(max_length=6)  # OTP code field
    second_otp = models.CharField(max_length=6, blank=True, null=True)  # Second OTP code field
    created_at = models.DateTimeField(default=timezone.now)
    is_valid = models.BooleanField(default=True)
    user_profile = models.ForeignKey(UserProfile, on_delete=models.CASCADE, null=True, blank=True)
  

    
    

    def is_expired(self):
        # Set your expiration time here, e.g., 10 minutes
        expiration_time = timezone.timedelta(minutes=10)
        return timezone.now() > self.created_at + expiration_time
    
    def __str__(self):
        return f"OTP for {self.email} - {self.otp_code}"


class CheckDevice(models.Model):
    device = models.ForeignKey(Devices, on_delete=models.CASCADE)  # Foreign key to Devices
    
    user_profile = models.ForeignKey(UserProfile, on_delete=models.CASCADE)  # ForeignKey to UserProfile # Reference to UserProfile
    registered_at = models.DateTimeField(auto_now_add=True)


    @property
    def mac_address(self):
        return self.device.mac_address  # Access the MAC address from the related Devices instance

    @property
    def unique_id(self):
        return self.device.unique_id  # Access the unique ID from the related Devices instance
    
    @property
    def user_id(self):
        return self.user_profile.user.id
    

    def __str__(self):
        return f"Device {self.unique_id} for {self.user_profile.email} (User ID: {self.user_profile.user.id})"
    


class DeviceDetails(models.Model):
    device = models.ForeignKey(Devices, on_delete=models.CASCADE)  # Link to Devices model

    @property
    def used_by(self):
        return self.device.used_by  # Accessing used_by from Devices

    @property
    def mac_address(self):
        return self.device.mac_address  # Accessing mac_address from Devices

    @property
    def unique_id(self):
        return self.device.unique_id  # Accessing unique_id from Devices

    def __str__(self):
        return f"Details of Device {self.device}"