from django.db import models
from django.conf import settings
from vontor_cz.models import SoftDeleteModel


class DownloaderRecord(SoftDeleteModel):
    # --- Source ---
    url = models.URLField()
    title = models.CharField(max_length=500, blank=True, default='')
    platform = models.CharField(max_length=100, blank=True, default='', help_text="e.g. youtube, tiktok, vimeo")

    # --- User (optional — anonymous downloads allowed) ---
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='downloads',
    )

    # --- Media info ---
    format = models.CharField(max_length=50)
    video_quality = models.IntegerField(null=True, blank=True, help_text="Video height in pixels (e.g. 1080). Null for audio-only.")
    is_audio_only = models.BooleanField(default=False)
    length_of_media = models.IntegerField(null=True, blank=True, help_text="Length of media in seconds")
    file_size = models.BigIntegerField(null=True, blank=True, help_text="File size in bytes")

    # --- Performance ---
    processing_time = models.FloatField(null=True, blank=True, help_text="Server-side processing time in seconds")

    # --- Status ---
    success = models.BooleanField(default=True)
    error_message = models.TextField(blank=True, default='')

    # --- Timing ---
    download_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-download_time']

    def __str__(self):
        return f"{self.platform or 'unknown'} | {self.title or self.url} | {self.download_time:%Y-%m-%d}"


