from django.db import models
from django.contrib.auth.models import User

class ProjectCategory(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    
    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = "Project Categories"

class Project(models.Model):
    STATUS = (
        ('draft', 'Draft'),
        ('published', 'Published'),
        ('archived', 'Archived'),
    )
    
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    category = models.ForeignKey(ProjectCategory, on_delete=models.SET_NULL, null=True, blank=True)
    
    # Project Details
    client_name = models.CharField(max_length=200)
    project_date = models.DateField()
    project_url = models.URLField(blank=True, help_text="Live project URL")
    github_url = models.URLField(blank=True, help_text="GitHub repository URL")
    
    # Content
    description = models.TextField()
    challenge = models.TextField(blank=True, help_text="The challenge faced")
    solution = models.TextField(blank=True, help_text="How we solved it")
    results = models.TextField(blank=True, help_text="The results achieved")
    technologies = models.TextField(blank=True, help_text="Technologies used, comma separated")
    
    # Images
    cover_image = models.ImageField(upload_to='projects/cover/', blank=True, null=True)
    image1 = models.ImageField(upload_to='projects/gallery/', blank=True, null=True)
    image2 = models.ImageField(upload_to='projects/gallery/', blank=True, null=True)
    image3 = models.ImageField(upload_to='projects/gallery/', blank=True, null=True)
    
    # Meta
    status = models.CharField(max_length=20, choices=STATUS, default='draft')
    featured = models.BooleanField(default=False, help_text="Display on homepage")
    display_on_website = models.BooleanField(default=True)
    order = models.PositiveIntegerField(default=0)
    
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    # SEO
    meta_title = models.CharField(max_length=200, blank=True)
    meta_description = models.TextField(blank=True)
    
    def __str__(self):
        return self.title
    
    class Meta:
        ordering = ['order', '-created_at']
        verbose_name_plural = "Projects"


class Testimonial(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='testimonials')
    client_name = models.CharField(max_length=100)
    client_designation = models.CharField(max_length=100, blank=True)
    client_company = models.CharField(max_length=100, blank=True)
    client_image = models.ImageField(upload_to='testimonials/', blank=True, null=True)
    content = models.TextField()
    rating = models.PositiveIntegerField(default=5, choices=[(i, i) for i in range(1, 6)])
    display_on_website = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return f"{self.client_name} - {self.project.title}"