import test from 'node:test'
import assert from 'node:assert/strict'

import { checkStoredFileExists, probeObjectStorageHealth } from '../src/lite/file-storage.mjs'

test('object storage probe reports local compatibility when S3 is not configured', async () => {
  const previousNodeEnv = process.env.NODE_ENV
  const previousDriver = process.env.OBJECT_STORAGE_DRIVER
  const previousEndpoint = process.env.S3_ENDPOINT
  const previousAccessKey = process.env.S3_ACCESS_KEY_ID
  const previousSecret = process.env.S3_SECRET_ACCESS_KEY

  process.env.NODE_ENV = 'development'
  delete process.env.OBJECT_STORAGE_DRIVER
  delete process.env.S3_ENDPOINT
  delete process.env.S3_ACCESS_KEY_ID
  delete process.env.S3_SECRET_ACCESS_KEY

  try {
    const result = await probeObjectStorageHealth()
    assert.equal(result.status, 'ok')
    assert.equal(result.configured, false)
    assert.equal(result.driver, 'local')
    assert.equal(result.required, false)
  } finally {
    if (previousNodeEnv === undefined) delete process.env.NODE_ENV
    else process.env.NODE_ENV = previousNodeEnv
    if (previousDriver === undefined) delete process.env.OBJECT_STORAGE_DRIVER
    else process.env.OBJECT_STORAGE_DRIVER = previousDriver
    if (previousEndpoint === undefined) delete process.env.S3_ENDPOINT
    else process.env.S3_ENDPOINT = previousEndpoint
    if (previousAccessKey === undefined) delete process.env.S3_ACCESS_KEY_ID
    else process.env.S3_ACCESS_KEY_ID = previousAccessKey
    if (previousSecret === undefined) delete process.env.S3_SECRET_ACCESS_KEY
    else process.env.S3_SECRET_ACCESS_KEY = previousSecret
  }
})

test('object storage probe errors when production requires S3 without config', async () => {
  const previousNodeEnv = process.env.NODE_ENV
  const previousDriver = process.env.OBJECT_STORAGE_DRIVER
  const previousEndpoint = process.env.S3_ENDPOINT
  const previousAccessKey = process.env.S3_ACCESS_KEY_ID
  const previousSecret = process.env.S3_SECRET_ACCESS_KEY

  process.env.NODE_ENV = 'production'
  delete process.env.OBJECT_STORAGE_DRIVER
  delete process.env.S3_ENDPOINT
  delete process.env.S3_ACCESS_KEY_ID
  delete process.env.S3_SECRET_ACCESS_KEY

  try {
    const result = await probeObjectStorageHealth()
    assert.equal(result.status, 'error')
    assert.equal(result.configured, false)
    assert.equal(result.driver, 'local')
    assert.equal(result.required, true)
  } finally {
    if (previousNodeEnv === undefined) delete process.env.NODE_ENV
    else process.env.NODE_ENV = previousNodeEnv
    if (previousDriver === undefined) delete process.env.OBJECT_STORAGE_DRIVER
    else process.env.OBJECT_STORAGE_DRIVER = previousDriver
    if (previousEndpoint === undefined) delete process.env.S3_ENDPOINT
    else process.env.S3_ENDPOINT = previousEndpoint
    if (previousAccessKey === undefined) delete process.env.S3_ACCESS_KEY_ID
    else process.env.S3_ACCESS_KEY_ID = previousAccessKey
    if (previousSecret === undefined) delete process.env.S3_SECRET_ACCESS_KEY
    else process.env.S3_SECRET_ACCESS_KEY = previousSecret
  }
})

test('stored file existence check rejects missing local objects', async () => {
  await assert.rejects(
    () => checkStoredFileExists({
      storageDriver: 'local',
      storagePath: '/tmp/dlbdi-missing-file-for-unit-test',
    }),
    /ENOENT/,
  )
})
