from django.db import models
from common.models.base import TimeStampedModel
from job_companies.models.job_company import JobCompanies
from django.core.validators import MinValueValidator, MaxValueValidator
from django.conf import settings
from service.models.services import Services

class JobCompanyReview(TimeStampedModel):
    company = models.ForeignKey(JobCompanies, on_delete=models.CASCADE, related_name='job_company_review')
    review_star = models.PositiveIntegerField(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_company_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_company_review_image_or_video_not_both'
            )
        ]
    
    def soft_delete(self):
        if self.is_active:
            self.is_active = False
            self.save(update_fields=['is_active'])
