from django.db import models
from common.models.base import TimeStampedModel
from job_companies.models.job_company import JobCompanies
from job_category_list.models.job_category import JobCategory
from django.core.validators import MinValueValidator, MaxValueValidator
from job_sub_category.models.job_sub_categories import JobSubCategories
from api.property_api.city.models.city import City

class JobType(models.IntegerChoices):
    FULL_TIME = 1
    PART_TIME = 2
    INTERNSHIP = 3

class Jobs(TimeStampedModel):
    company = models.ForeignKey(JobCompanies,on_delete=models.CASCADE,related_name="jobs_of_company")
    country = models.CharField(max_length=255,null=True, blank=True)
    state = models.CharField(max_length=255,null=True, blank=True)
    city = models.ForeignKey(City,on_delete=models.SET_NULL,blank=True,null=True)
    title = models.CharField(max_length=100)
    type = models.IntegerField(choices=JobType)
    salary_min = models.PositiveIntegerField()
    salary_max = models.PositiveIntegerField()
    location = models.CharField(max_length=50)
    is_featured = models.BooleanField()
    description = models.TextField()
    requirements = models.TextField()
    desirable = models.TextField()
    benefits = models.TextField()
    category = models.ForeignKey(JobCategory, on_delete=models.CASCADE, related_name="company_category",blank=True,null=True)
    # icon_url = models.ImageField(upload_to="jobs") #todo
    icon_url = models.ImageField(upload_to="jobs")
    caption = models.CharField(max_length=50,blank=True,default=None)
    is_flag = models.BooleanField(default=False)
    contact_number = models.CharField(max_length=15,blank=True,null=True)
    sub_category = models.ForeignKey(JobSubCategories,on_delete=models.SET_NULL,related_name="sub_categories",blank=True,null=True)

    class Meta:
        ordering = ['id']
        verbose_name_plural = 'job_company_gallery'

    def __str__(self):
        return self.title
    
    def soft_delete(self):
        if self.is_active:
            self.is_active = False
            self.save(update_fields=['is_active'])


class JobReview(TimeStampedModel):
    job = models.ForeignKey(Jobs, on_delete=models.CASCADE, related_name='jobs_reviews')
    review_star = models.PositiveIntegerField(blank=True, validators=[MinValueValidator(1), MaxValueValidator(5)])
    message = models.TextField()
    review_image = models.CharField(max_length=255, blank=True, null=True, help_text="Cloudinary public_id for image")
    review_video = models.CharField(max_length=255, blank=True, null=True, help_text="Cloudinary public_id for video")
    video_duration = models.PositiveIntegerField(blank=True, null=True, help_text="Video duration in seconds")
    is_flagged_review = models.BooleanField(default=False, blank=True)

    class Meta:
        ordering = ['-created_at']
        verbose_name_plural = 'job_reviews'
        constraints = [
            models.CheckConstraint(
                check=(
                    models.Q(review_image__isnull=False, review_video__isnull=True) |
                    models.Q(review_image__isnull=True, review_video__isnull=False) |
                    models.Q(review_image__isnull=True, review_video__isnull=True)
                ),
                name='job_review_image_or_video_not_both'
            )
        ]


class JobContactInformation(TimeStampedModel):
    job = models.ForeignKey(
        Jobs,
        on_delete=models.CASCADE,
        related_name='job_contact_information'
    )
    first_name = models.CharField(max_length=100)    
    last_name = models.CharField(max_length=100)
    contact = models.CharField(max_length=15)
    message = models.TextField()
    email = models.EmailField()
    district = models.CharField(max_length=50)   
