from django.conf import settings
from django.db import models
from cloudinary.models import CloudinaryField

from users.models import Company


class EmployeeSection(models.Model):
    class SectionClass(models.TextChoices):
        PERSONAL = 'Personal', 'Personal'
        EMPLOYMENT = 'Employment', 'Employment'

    class Status(models.TextChoices):
        ACTIVE = 'Active', 'Active'
        INACTIVE = 'Inactive', 'Inactive'

    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='employee_sections')
    section_class = models.CharField(max_length=20, choices=SectionClass.choices)
    parent_id = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        related_name='child_sections',
        null=True,
        blank=True,
    )
    name = models.CharField(max_length=255)
    status = models.CharField(max_length=20, choices=Status.choices, default=Status.ACTIVE)
    is_visible = models.BooleanField(default=True)
    date_created = models.DateTimeField(auto_now_add=True)
    is_system_default = models.BooleanField(default=False)
    last_updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['company', 'section_class', 'name']

    def __str__(self):
        return f'{self.company} - {self.name}'


class SectionField(models.Model):
    class DataType(models.TextChoices):
        TEXT = 'text', 'Text'
        EMAIL = 'email', 'Email'
        DATE = 'date', 'Date'
        TIME = 'time', 'Time'
        DATETIME = 'datetime', 'DateTime'
        NUMBER = 'number', 'Number'
        TEXTAREA = 'textarea', 'Textarea'
        SELECT = 'select', 'Select'
        RADIO = 'radio', 'Radio'
        CHECKBOX = 'checkbox', 'Checkbox'
        FILE = 'file', 'File'

    class Status(models.TextChoices):
        ACTIVE = 'Active', 'Active'
        INACTIVE = 'Inactive', 'Inactive'

    employee_section = models.ForeignKey(EmployeeSection, on_delete=models.CASCADE, related_name='section_fields')
    name = models.CharField(max_length=255)
    label = models.CharField(max_length=255)
    placeholder = models.CharField(max_length=255, blank=True, null=True)
    max_length = models.IntegerField(blank=True, null=True)
    is_required = models.BooleanField(default=False)
    data_type = models.CharField(max_length=20, choices=DataType.choices)
    is_system_default = models.BooleanField(default=False)
    allow_file = models.BooleanField(blank=True, null=True)
    default_value = models.TextField(blank=True, null=True)
    is_hidden = models.BooleanField(default=False)
    status = models.CharField(max_length=20, choices=Status.choices, default=Status.ACTIVE)
    date_added = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['employee_section', 'name']

    def __str__(self):
        return f'{self.employee_section} - {self.name}'


class FieldOption(models.Model):
    class Status(models.TextChoices):
        ACTIVE = 'Active', 'Active'
        INACTIVE = 'Inactive', 'Inactive'

    section_field = models.ForeignKey(SectionField, on_delete=models.CASCADE, related_name='field_options')
    name = models.CharField(max_length=255)
    label = models.CharField(max_length=255)
    value = models.CharField(max_length=255)
    max_length = models.IntegerField(blank=True, null=True)
    status = models.CharField(max_length=20, choices=Status.choices, default=Status.ACTIVE)
    date_added = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['section_field', 'name']

    def __str__(self):
        return f'{self.section_field} - {self.label}'


class EmployeeSectionResponse(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='section_responses')
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='section_responses')
    section_field = models.ForeignKey(SectionField, on_delete=models.CASCADE, related_name='responses')
    response_value = models.TextField(blank=True, null=True)
    uploaded_file = CloudinaryField('file', folder='employee-section-responses/files', blank=True, null=True)
    date_added = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['company', 'user', 'section_field']
        unique_together = ('user', 'company', 'section_field')

    def __str__(self):
        return f'{self.user} - {self.section_field}'