import os
import uuid
from datetime import date

from django.utils.text import slugify


def upload_generic(module: str, original_filename: str) -> str:
    """Generic dated path: ``{module}/{year}/{month}/{uuid}{ext}`` under MEDIA_ROOT."""
    today = date.today()
    _, ext = os.path.splitext(original_filename)
    ext = ext.lower()
    uid = uuid.uuid4().hex
    return os.path.join(module, str(today.year), f"{today.month:02d}", f"{uid}{ext}")


def _safe_filename(original: str) -> str:
    _, ext = os.path.splitext(original)
    stem = slugify(os.path.splitext(os.path.basename(original))[0])[:60] or "file"
    uid = uuid.uuid4().hex[:8]
    return f"{stem}_{uid}{ext.lower()}"


def _dated_path(module: str, sub: str, filename: str) -> str:
    """Path relative to MEDIA_ROOT (do not prefix MEDIA_ROOT name)."""
    today = date.today()
    safe = _safe_filename(filename)
    return os.path.join(module, sub, str(today.year), f"{today.month:02d}", safe)


def upload_to_grants(instance, filename):
    return _dated_path("rims", "grants", filename)


def upload_to_projects(instance, filename):
    return _dated_path("rims", "projects", filename)


def upload_to_scholarships(instance, filename):
    return _dated_path("rims", "scholarships", filename)


def upload_to_finance(instance, filename):
    return _dated_path("rims", "finance", filename)


def upload_to_operations(instance, filename):
    return _dated_path("rims", "operations", filename)


def upload_to_course_content(instance, filename):
    return _dated_path("rep", "content", filename)


def upload_to_scorm(instance, filename):
    return _dated_path("rep", "scorm", filename)


def upload_to_live_captions(instance, filename):
    return _dated_path("rep", "live-captions", filename)


def upload_to_video_originals(instance, filename):
    return _dated_path("rep", "videos/originals", filename)


def upload_to_video_variants(instance, filename):
    return _dated_path("rep", "videos/hls", filename)


def upload_to_video_captions(instance, filename):
    return _dated_path("rep", "videos/captions", filename)


def upload_to_video_posters(instance, filename):
    return _dated_path("rep", "videos/posters", filename)


def upload_to_h5p(instance, filename):
    return _dated_path("rep", "h5p", filename)


def upload_to_cv(instance, filename):
    return _dated_path("rep", "cv", filename)


def upload_to_repository(instance, filename):
    return _dated_path("repository", "documents", filename)


def upload_to_repository_thumbnails(instance, filename):
    return _dated_path("repository", "thumbnails", filename)


def upload_to_mel_reports(instance, filename):
    return _dated_path("mel", "reports", filename)


def upload_to_mel_surveys(instance, filename):
    return _dated_path("mel", "surveys", filename)


def upload_to_mel_evidence(instance, filename):
    """G21 — generic destination for polymorphic EvidenceAttachment files."""
    return _dated_path("mel", "evidence", filename)


def upload_to_alumni_photos(instance, filename):
    return _dated_path("alumni", "photos", filename)


def upload_to_alumni_certificates(instance, filename):
    return _dated_path("alumni", "certificates", filename)


def upload_to_smehub_onboarding(instance, filename):
    return _dated_path("smehub", "onboarding", filename)


def upload_to_smehub_marketplace(instance, filename):
    return _dated_path("smehub", "marketplace", filename)


def upload_to_smehub_investment(instance, filename):
    return _dated_path("smehub", "investment", filename)


def upload_to_smehub_incubation(instance, filename):
    return _dated_path("smehub", "incubation", filename)


def upload_to_smehub_linkage(instance, filename):
    return _dated_path("smehub", "linkage", filename)


def upload_to_smehub_showcasing(instance, filename):
    return _dated_path("smehub", "showcasing", filename)


def upload_to_smehub_certificates(instance, filename):
    return _dated_path("smehub", "certificates", filename)
