import { z } from 'zod';

export const storageConfigSchema = z.object({
  S3_ENDPOINT: z.string().url(),
  S3_REGION: z.string().min(1).default('us-east-1'),
  S3_BUCKET: z.string().min(1),
  S3_ACCESS_KEY: z.string().min(1),
  S3_SECRET_KEY: z.string().min(1),
  S3_FORCE_PATH_STYLE: z
    .string()
    .optional()
    .transform((value) => value === 'true' || value === '1'),
});

export type StorageConfig = z.infer<typeof storageConfigSchema>;

export function loadStorageConfig(
  env: Record<string, string | undefined> = process.env,
): StorageConfig {
  return storageConfigSchema.parse({
    S3_ENDPOINT: env['S3_ENDPOINT'],
    S3_REGION: env['S3_REGION'],
    S3_BUCKET: env['S3_BUCKET'],
    S3_ACCESS_KEY: env['S3_ACCESS_KEY'],
    S3_SECRET_KEY: env['S3_SECRET_KEY'],
    S3_FORCE_PATH_STYLE: env['S3_FORCE_PATH_STYLE'],
  });
}
