import { getSpiceDbConfig } from "./authz/config.js";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { v1 } from "@authzed/authzed-node";

//#region src/auth/spicedb-schema.ts
async function writeSpiceDbSchema(options) {
	const config = getSpiceDbConfig();
	const { endpoint = config.endpoint, token = config.token, schemaPath = resolve(import.meta.dirname, "../../spicedb/schema.zed"), maxRetries = 30 } = options ?? {};
	const schema = readFileSync(schemaPath, "utf-8");
	const client = v1.NewClient(token, endpoint, config.tlsEnabled ? v1.ClientSecurity.SECURE : v1.ClientSecurity.INSECURE_PLAINTEXT_CREDENTIALS);
	let lastError;
	for (let attempt = 1; attempt <= maxRetries; attempt++) try {
		await client.promises.writeSchema(v1.WriteSchemaRequest.create({ schema }));
		return;
	} catch (error) {
		lastError = error;
		if (attempt < maxRetries) await new Promise((r) => setTimeout(r, 1e3));
	}
	throw new Error(`Failed to write SpiceDB schema after ${maxRetries} attempts: ${lastError?.message}`);
}

//#endregion
export { writeSpiceDbSchema };