import { ok } from '@atcute/client';
import { mainSchema as putRecordSchema } from '@atcute/atproto/types/repo/putRecord';
import type { Nsid, RecordKey } from '@atcute/lexicons/syntax';
import type { OAuthUserAgent } from '@atcute/oauth-browser-client';
import { createClient } from '$lib/auth/agent';
import type { ProfileRecord } from './records';

const PROFILE_COLLECTION = 'sh.tangled.actor.profile' as Nsid;

// the profile record lives at the fixed rkey `self`; put upserts it.
export const putProfile = async (agent: OAuthUserAgent, record: ProfileRecord): Promise<void> => {
	const rpc = createClient(agent);
	await ok(
		rpc.call(putRecordSchema, {
			input: {
				repo: agent.sub,
				collection: PROFILE_COLLECTION,
				rkey: 'self' as RecordKey,
				record
			}
		})
	);
};
