from django.db import models

from coresite.mixin import AbstractTimeStampModel
from .choices import SupportTicketStatus


class SupportTicket(AbstractTimeStampModel):

    user = models.ForeignKey(
        'core.User',
        on_delete=models.CASCADE,
        related_name="support_tickets"
    )
    description = models.TextField()
    status = models.CharField(
        max_length=20,
        default=SupportTicketStatus.OPEN
    )

    class Meta:
        ordering = ["-created_at"]
        verbose_name = "Support Ticket"
        verbose_name_plural = "Support Tickets"
        indexes = [
            models.Index(fields=["status"]),
            models.Index(fields=["created_at"]),
        ]

    def __str__(self):
        return f"Ticket #{self.id} - {self.user.email} - {self.status}"

import uuid
import os

def upload_path(instance, filename):
    ext = os.path.splitext(filename)[1]
    return f"support/{uuid.uuid4()}{ext}"

class SupportTicketImage(models.Model):
    ticket = models.ForeignKey(
        SupportTicket,
        related_name="images",
        on_delete=models.CASCADE
    )
    image = models.ImageField(upload_to=upload_path)

    original_name = models.CharField(max_length=255)
    content_type = models.CharField(max_length=100)
    size = models.PositiveIntegerField()

    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Support Ticket Image"
        verbose_name_plural = "Support Ticket Images"

    def __str__(self):
        return f"Image for Ticket #{self.ticket.id}"
