from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models


class Testimonial(models.Model):
    """A client review, optionally tied to a specific portfolio project."""

    client_name = models.CharField(max_length=150)
    client_role = models.CharField(max_length=150, blank=True)
    company = models.CharField(max_length=150, blank=True)
    photo = models.ImageField(upload_to='testimonials/', blank=True, null=True)
    quote = models.TextField()
    rating = models.PositiveSmallIntegerField(
        default=5, validators=[MinValueValidator(1), MaxValueValidator(5)],
    )
    project = models.ForeignKey(
        'portfolio.Project', null=True, blank=True,
        on_delete=models.SET_NULL, related_name='testimonials',
    )

    is_featured = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    order = models.PositiveSmallIntegerField(default=0)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['order', '-created_at']

    def __str__(self):
        return f'{self.client_name} ({self.rating}★)'
