const Minio = require('minio');

const client = new Minio.Client({
  endPoint:  process.env.MINIO_ENDPOINT || 'localhost',
  port:      parseInt(process.env.MINIO_PORT || '9000'),
  useSSL:    process.env.MINIO_USE_SSL === 'true',
  accessKey: process.env.MINIO_ACCESS_KEY,
  secretKey: process.env.MINIO_SECRET_KEY,
});

const BUCKET = process.env.MINIO_BUCKET || 'platform';

async function ensureBucket() {
  const exists = await client.bucketExists(BUCKET);
  if (!exists) {
    await client.makeBucket(BUCKET);
    console.log(`MinIO bucket "${BUCKET}" created`);
  }
}

// 파일 URL — 플랫폼 백엔드 프록시를 통해 서빙 (CORS 문제 없음)
// objectName: "images/uuid.webp" 형식
function publicUrl(objectName) {
  const base = (process.env.BASE_URL || 'http://localhost:3200').replace(/\/$/, '');
  return `${base}/api/files/${objectName}`;
}

module.exports = { client, BUCKET, ensureBucket, publicUrl };
