import os
import uuid

from django.conf import settings
from django.core.files.storage import FileSystemStorage


class ProjectAttachmentStorage(FileSystemStorage):
    def __init__(self):
        super().__init__(location=settings.PROJECT_UPLOAD_ROOT)


project_attachment_storage = ProjectAttachmentStorage()


def project_attachment_upload_to(instance, _filename):
    ext = os.path.splitext(instance.original_filename)[1].lower()
    if not ext:
        ext = ".bin"
    subdir = str(instance.brief_id) if instance.brief_id else "pending"
    return f"{subdir}/{uuid.uuid4().hex}{ext}"
