/**
 * Uploads docs/logo/nexumi-banner.png as the bot user banner (and optional bio)
 * via Discord API.
 *
 * Usage (from repo root):
 *   pnpm --filter @nexumi/bot profile:banner
 *
 * Requires BOT_TOKEN in the environment (or root/.env / apps/bot/.env).
 */
import { readFileSync, existsSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { config as loadEnv } from 'dotenv';

const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(here, '../../..');

for (const candidate of [resolve(repoRoot, '.env'), resolve(here, '../.env')]) {
  if (existsSync(candidate)) {
    loadEnv({ path: candidate });
  }
}

const token = process.env.BOT_TOKEN;
if (!token) {
  console.error('BOT_TOKEN is missing.');
  process.exit(1);
}

const bannerPath = resolve(repoRoot, 'docs/logo/nexumi-banner.png');
if (!existsSync(bannerPath)) {
  console.error(`Banner not found: ${bannerPath}`);
  process.exit(1);
}

const png = readFileSync(bannerPath);
const bannerDataUri = `data:image/png;base64,${png.toString('base64')}`;

const bio =
  'Moderation, Community & Integrationen — in einem Bot.\n' +
  'Dashboard: https://dashboard.nexumi.de\n' +
  'nexumi.de';

const response = await fetch('https://discord.com/api/v10/users/@me', {
  method: 'PATCH',
  headers: {
    Authorization: `Bot ${token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ banner: bannerDataUri, bio })
});

const body = await response.text();
if (!response.ok) {
  console.error(`Failed (${response.status}): ${body}`);
  process.exit(1);
}

console.log('Bot profile updated successfully.');
console.log(body);
