/**
 * Provider-agnostic storage facade with filesystem storage.
 * Uses local filesystem for document storage in /app/data/documents.
 */
import type { Readable } from 'stream';
import { FilesystemAdapter } from './adapters/filesystem.adapter';

export type ObjectBody = Buffer | Readable | string;

export interface SignedUrlOptions {
  method: 'GET' | 'PUT';
  expiresSeconds?: number; // default 300s
}

export interface HeadObjectResult {
  size: number;
  etag?: string;
  lastModified?: Date;
  contentType?: string;
  metadata?: Record<string, string>;
}

export interface StorageService {
  putObject(
    bucket: string,
    key: string,
    body: ObjectBody,
    contentType?: string,
    metadata?: Record<string, string>
  ): Promise<void>;

  getObjectStream(bucket: string, key: string): Promise<Readable>;

  deleteObject(bucket: string, key: string): Promise<void>;

  headObject(bucket: string, key: string): Promise<HeadObjectResult>;

  getSignedUrl(bucket: string, key: string, options?: SignedUrlOptions): Promise<string>;
}

// Simple factory — uses filesystem storage
let singleton: StorageService | null = null;
export function getStorageService(): StorageService {
  if (!singleton) {
    singleton = new FilesystemAdapter('/app/data/documents');
  }
  return singleton;
}

