import { env } from "cloudflare:workers";
import type { D1DatabaseLike } from "./d1.ts";

export interface AppBindings {
  DB: D1DatabaseLike;
  STORAGE: R2Bucket;
}

export function getBindings(): AppBindings {
  if (!env.DB || !env.STORAGE) {
    throw new Error(
      "Cloudflare bindings are unavailable. Configure DB and STORAGE before using persistence.",
    );
  }
  return {
    DB: env.DB as unknown as D1DatabaseLike,
    STORAGE: env.STORAGE,
  };
}
