const { createClient } = require("@supabase/supabase-js")

const supabase = createClient(
  process.env.SUPABASE_URL,
  process.env.SUPABASE_KEY
)

/**
 * @param {Object} options
 * @param {string} options.bucket - supabase bucket adı
 * @param {string} options.path - storage içi path
 * @param {Buffer} options.fileBuffer - file buffer
 * @param {string} options.contentType
 */
async function uploadImage({
  bucket,
  path,
  fileBuffer,
  contentType,
}) {
  const { error } = await supabase.storage
    .from(bucket)
    .upload(path, fileBuffer, {
      contentType,
      upsert: true,
    })

  if (error) {
    throw new Error(error.message)
  }

  const { data } = supabase.storage
    .from(bucket)
    .getPublicUrl(path)

  return data.publicUrl
}

module.exports = { uploadImage }
