import {
  createMintelDirectusClient,
  ensureDirectusAuthenticated,
} from "@mintel/next-utils";
import { updateSettings } from "@directus/sdk";

const client = createMintelDirectusClient();

async function setupBranding() {
  const prjName = process.env.PROJECT_NAME || "Mintel.me";
  const prjColor = process.env.PROJECT_COLOR || "#ff00ff";

  console.log(`🎨 Refining Directus Branding for ${prjName}...`);
  await ensureDirectusAuthenticated(client);

  const cssInjection = `
  <style>
      @import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;500;600;700&display=swap');
      
      body, .v-app { font-family: 'Outfit', sans-serif !important; }
      
      .public-view .v-card { 
          backdrop-filter: blur(20px);
          background: rgba(255, 255, 255, 0.9) !important;
          border-radius: 32px !important; 
          box-shadow: 0 50px 100px -20px rgba(0, 0, 0, 0.4) !important;
          border: 1px solid rgba(255, 255, 255, 0.3) !important;
      }

      .v-navigation-drawer { background: #000c24 !important; }
      
      .v-list-item--active {
          color: ${prjColor} !important;
          background: rgba(255, 0, 255, 0.1) !important;
      }
  </style>
  <div style="font-family: 'Outfit', sans-serif; text-align: center; margin-top: 24px;">
      <p style="color: rgba(255,255,255,0.6); font-size: 11px; letter-spacing: 2px; margin-bottom: 4px; font-weight: 600; text-transform: uppercase;">Mintel Infrastructure Engine</p>
      <h1 style="color: #ffffff; font-size: 20px; font-weight: 700; margin: 0; letter-spacing: -0.5px;">${prjName.toUpperCase()} <span style="color: ${prjColor};">SYNC.</span></h1>
  </div>
  `;

  try {
    await client.request(
      updateSettings({
        project_name: prjName,
        project_color: prjColor,
        public_note: cssInjection,
        module_bar_background: "#00081a",
        theme_light_overrides: {
          primary: prjColor,
          borderRadius: "12px",
          navigationBackground: "#000c24",
          navigationForeground: "#ffffff",
          moduleBarBackground: "#00081a",
        },
      } as any),
    );
    console.log("✨ Branding applied!");
  } catch (error) {
    console.error("❌ Error during bootstrap:", error);
  }
}

setupBranding()
  .then(() => {
    process.exit(0);
  })
  .catch((err) => {
    console.error("🚨 Fatal bootstrap error:", err);
    process.exit(1);
  });
