from django.db import models
import uuid

class Ticket(models.Model):
    STATUS_CHOICES = [
        ('open', 'Open'),
        ('in_progress', 'In Progress'),
        ('closed', 'Closed'),
    ]

    PRIORITY_CHOICES = [
        ('low', 'Low'),
        ('medium', 'Medium'),
        ('high', 'High'),
    ]

    subject = models.CharField(max_length=255)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='open')
    priority = models.CharField(max_length=20, choices=PRIORITY_CHOICES, default='medium')
    requester_email = models.EmailField()
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.subject

class Client(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=100, verbose_name="Nome")
    surname = models.CharField(max_length=100, verbose_name="Cognome")
    email = models.EmailField(unique=True, verbose_name="Email")
    phone = models.CharField(max_length=20, blank=True, null=True, verbose_name="Telefono")
    address = models.CharField(max_length=255, blank=True, null=True, verbose_name="Indirizzo")
    fiscal_code = models.CharField(max_length=16, blank=True, null=True, verbose_name="Codice Fiscale")
    vat_number = models.CharField(max_length=11, blank=True, null=True, verbose_name="Partita IVA")
    generated_password = models.CharField(max_length=128, help_text="Password generata per il cliente (visibile in chiaro)")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "Cliente"
        verbose_name_plural = "Clienti"

    def __str__(self):
        return f"{self.name} {self.surname}"

class Portfolio(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=100, verbose_name="Nome Portafoglio")
    description = models.TextField(blank=True, null=True, verbose_name="Descrizione")
    client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='portfolios', verbose_name="Cliente")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "Portafoglio"
        verbose_name_plural = "Portafogli"

    def __str__(self):
        return f"{self.name} ({self.client})"

class Activity(models.Model):
    STATUS_CHOICES = [
        ('todo', 'To Do'),
        ('in_progress', 'In Progress'),
        ('done', 'Done'),
    ]

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=200, verbose_name="Titolo")
    description = models.TextField(blank=True, null=True, verbose_name="Descrizione")
    client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='activities', verbose_name="Cliente")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='todo', verbose_name="Stato")
    activity_date = models.DateField(verbose_name="Data Attività")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "Attività"
        verbose_name_plural = "Attività"
        ordering = ['-activity_date']

    def __str__(self):
        return self.title

def client_directory_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/documents/<client_id>/<filename>
    return f'documents/{instance.client.id}/{filename}'

class Document(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=200, verbose_name="Titolo")
    description = models.TextField(blank=True, null=True, verbose_name="Descrizione")
    client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='documents', verbose_name="Cliente")
    file = models.FileField(upload_to=client_directory_path, verbose_name="File")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "Documento"
        verbose_name_plural = "Documenti"
        ordering = ['-created_at']

    def __str__(self):
        return self.title
