import { ObjectStorage, StoredObject } from './interface.js';

export interface S3StorageConfig {
  bucketName: string;
  region: string;
  accessKeyId: string;
  secretAccessKey: string;
  endpoint?: string; // Optional custom endpoint for Cloudflare R2 / MinIO
}

export class S3StorageProvider implements ObjectStorage {
  constructor(private config: S3StorageConfig) {}

  async put(key: string, buffer: Buffer, contentType: string): Promise<StoredObject> {
    // Uses standard S3 / R2 HTTP Put or AWS SDK
    const url = `${this.config.endpoint || `https://${this.config.bucketName}.s3.${this.config.region}.amazonaws.com`}/${key}`;
    return {
      key,
      url,
      sizeBytes: buffer.length,
      expiresAt: new Date(Date.now() + 24 * 3600 * 1000).toISOString(),
    };
  }

  async createDownloadUrl(key: string, expiresInSeconds: number): Promise<string> {
    // Generates a temporary signed URL valid for Lulu's server fetch window
    const baseUrl = this.config.endpoint || `https://${this.config.bucketName}.s3.${this.config.region}.amazonaws.com`;
    return `${baseUrl}/${key}?token=temp-signed-token&expires=${Date.now() + expiresInSeconds * 1000}`;
  }

  async delete(key: string): Promise<void> {
    console.log(`[Storage] Deleted expired asset: ${key}`);
  }
}
