from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import User
from skyonnadmin.models import InternalTeam

class CustomUserManager(BaseUserManager):
    def create_user(self, phone, password=None, **extra_fields):
        if not phone:
            raise ValueError('The Phone number must be set')
        user = self.model(phone=phone, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, phone, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        return self.create_user(phone, password, **extra_fields)


class CustomUser(AbstractBaseUser):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    phone = models.CharField(max_length=15, unique=True)
    phone2 = models.CharField(max_length=15, blank=True, null=True)
    company_email = models.EmailField(null=True)
    personal_email = models.EmailField(null=True)
    address = models.TextField(null=True)
    work_location = models.CharField(max_length=100, null=True)
    picture = models.ImageField(upload_to='profile_pics/', null=True)
    role = models.CharField(max_length=20)
    user_id = models.CharField(max_length=10, unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    manager_assigned = models.CharField(max_length=100, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    objects = CustomUserManager()

    USERNAME_FIELD = 'phone'


# class JobPosting(models.Model):
#     client = models.CharField(max_length=100)
#     spoc = models.CharField(max_length=100, blank=True, null=True)
#     start_date = models.DateField()
#     budget_max = models.DecimalField(max_digits=10, decimal_places=2, null=True)
#     budget_min = models.DecimalField(max_digits=10, decimal_places=2, null=True)
#     job_description = models.TextField()
#     special_instructions = models.CharField(max_length=200, blank=True, null=True)
#     job_id = models.CharField(max_length=20)
#     spoc_2 = models.CharField(max_length=100, blank=True, null=True)
#     close_date = models.DateField()
#     location = models.TextField()  # Changed from CharField to TextField
#     no_of_posting = models.IntegerField()
#     job_type = models.CharField(max_length=20)
#     header = models.CharField(max_length=200)
#     experience_in_years = models.IntegerField()
#
#     def set_locations(self, location_list):
#         self.location = ",".join(location_list)
#
#     def get_locations(self):
#         return self.location.split(",")
#
#     def __str__(self):
#         return self.job_id

class JobPosting(models.Model):
    Client = models.CharField(max_length=100, null=True, blank=True)
    JobID = models.CharField(max_length=100,null=True)
    Location = models.CharField(max_length=255,null=True)
    SPOC = models.CharField(max_length=255,null=True)
    SPOC2 = models.CharField(max_length=255, blank=True, null=True)
    NoOfPosting = models.IntegerField(null=True)
    StartDate = models.DateField(null=True)
    CloseDate = models.DateField(null=True)

    BudgetMin = models.CharField(max_length=20,null=True)
    timePeriod = models.CharField(max_length=20,null=True,blank=True)
    minAmount = models.CharField(max_length=20,null=True)
    maxAmount = models.CharField(max_length=20,null=True)

    Qualification = models.CharField(max_length=100,null=True)
    Header = models.CharField(max_length=255,null=True)
    JobDescription = models.TextField(null=True)
    Experience_in_Yrs = models.CharField(max_length=50,null=True)
    SpecialInstructions = models.TextField(blank=True, null=True)
    JD = models.FileField(upload_to='JD/', null= True)
    slice_JD = models.CharField(max_length=255,null=True)


# class SPOC(models.Model):
#     name = models.CharField(max_length=255,null=True)
#
#     def __str__(self):
#         return self.name

class Message(models.Model):
    message_id = models.CharField(unique=True,max_length=255, null=True)
    sender = models.ForeignKey(InternalTeam, related_name='sent_messages',to_field='Email1', on_delete=models.CASCADE)
    recipient = models.ManyToManyField(InternalTeam, related_name='received_messages')
    subject = models.CharField(max_length=255)
    body = models.TextField()
    is_read = models.BooleanField(default=False)
    sent_at = models.DateTimeField(auto_now_add=True)
    sender_fullname = models.CharField(max_length=255,null=True)
    recipient_fullname = models.CharField(max_length=255,null=True)
    reply_to = models.ForeignKey('self',null=True,blank=True,on_delete=models.CASCADE,related_name='replies', to_field='message_id')
    is_trashed = models.BooleanField(default=False)
    attachment = models.FileField(upload_to='attachment/',null=True,blank=True)
    seen_time = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return f'{self.message_id}'

    class Meta:
        ordering = ['-sent_at']

class MessageStatus(models.Model):
    message = models.ForeignKey(Message, on_delete=models.CASCADE, to_field='message_id', related_name='status')
    user = models.ForeignKey(InternalTeam, on_delete=models.CASCADE, to_field='Email1', null=True)
    is_read = models.BooleanField(default=False)
    seen_time = models.DateTimeField(null=True, blank=True)

class FileDownload(models.Model):
    # Reference to the message containing the attachment
    message = models.ForeignKey(Message, on_delete=models.CASCADE,to_field='message_id', related_name='downloads')
    user = models.ForeignKey(InternalTeam, on_delete=models.CASCADE,to_field='Email1',null=True)
    first_downloaded = models.DateTimeField(auto_now_add=True)
    last_downloaded = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"Download details for message_id: {self.message.message_id}"