"""b64 이미지 → NAS SMB 경로 저장 → /media/image URL 반환."""
from __future__ import annotations

import base64
import os
import uuid

IMAGE_MEDIA_ROOT = os.getenv("IMAGE_MEDIA_ROOT", "/mnt/nas/webpage/data/image")
IMAGE_MEDIA_URL_PREFIX = os.getenv("IMAGE_MEDIA_URL_PREFIX", "/media/image")


def save_b64_png(task_id: str, b64_data: str) -> str:
    os.makedirs(IMAGE_MEDIA_ROOT, exist_ok=True)
    fname = f"{task_id}-{uuid.uuid4().hex[:8]}.png"
    path = os.path.join(IMAGE_MEDIA_ROOT, fname)
    with open(path, "wb") as f:
        f.write(base64.b64decode(b64_data))
    return f"{IMAGE_MEDIA_URL_PREFIX}/{fname}"
