import { defineConfig, services } from '@adonisjs/drive'
import type { InferDriveDisks } from '@adonisjs/drive/types'
import env from '#start/env'

const driveConfig = defineConfig({
  default: env.get('DRIVE_DISK', 's3'),

  /**
   * Stockage local (filesystem) — utilisé en fallback si MinIO indisponible.
   * Bucket par défaut : storage/uploads (ignoré par git).
   */
  services: {
    fs: services.fs({
      location: 'storage/uploads',
      visibility: 'private',
    }),

    /**
     * MinIO via le driver S3 (S3-compatible).
     */
    s3: services.s3({
      credentials: {
        accessKeyId: env.get('S3_ACCESS_KEY', ''),
        secretAccessKey: env.get('S3_SECRET_KEY', ''),
      },
      endpoint: env.get('S3_ENDPOINT'),
      region: env.get('S3_REGION', 'fr-par'),
      bucket: env.get('S3_BUCKET', 'rubis-invoices'),
      forcePathStyle: env.get('S3_FORCE_PATH_STYLE', true),
      visibility: 'private',
    }),
  },
})

export default driveConfig

declare module '@adonisjs/drive/types' {
  export interface DriveDisks extends InferDriveDisks<typeof driveConfig> {}
}
