from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ValidationError
import uuid




from django.db import models

class Frontend(models.Model):
    flow_title = models.CharField(max_length=255)
    flow_description = models.TextField()
    switch_description = models.TextField()
    sign_in_description = models.TextField()
    register_description = models.TextField()
    forgot_description = models.TextField()
    reset_description = models.TextField()
    authenticated_route = models.CharField(max_length=255)
    account_type = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.flow_title

    class Meta:
        verbose_name_plural = "Frontend"


class ServiceType(models.Model):
    name = models.CharField(max_length=255, unique=True)
    description = models.TextField()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Service Types"

class Identity(models.Model):
    AUTH='auth'
    name = models.CharField(max_length=255)
    description = models.TextField()
    TYPE_CHOICES = [
        (AUTH, 'auth'),
    ]

    # service_type = models.CharField(
    #     max_length=4,
    #     choices=TYPE_CHOICES,
    #     default=AUTH,
    #     editable=False
    # )
    service_type = models.ForeignKey(
        'ServiceType',
        on_delete=models.PROTECT,
        editable=False
    )
    frontend = models.ForeignKey(
        'Frontend',
        on_delete=models.PROTECT,
        editable=False
    )

    enable_admin = models.BooleanField(default=False, verbose_name="Enable Admin")

    def save(self, *args, **kwargs):
        if Identity.objects.exists() and not self.pk:
            raise ValidationError("Only one Identity instance is allowed.")
        super().save(*args, **kwargs)

    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = "Identity"