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

const client = createMintelDirectusClient();

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

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

  const cssInjection = `
  <style>
      @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
      body, .v-app { font-family: 'Inter', sans-serif !important; }
      .public-view .v-card { 
          backdrop-filter: blur(20px);
          border-radius: 32px !important; 
          box-shadow: 0 50px 100px -20px rgba(0, 0, 0, 0.4) !important;
      }
      .v-navigation-drawer { background: #000c24 !important; }
  </style>
  <div style="font-family: 'Inter', sans-serif; text-align: center; margin-top: 24px;">
      <p style="color: rgba(255,255,255,0.7); font-size: 14px; margin-bottom: 4px; font-weight: 500;">MINTEL INFRASTRUCTURE ENGINE</p>
      <h1 style="color: #ffffff; font-size: 18px; font-weight: 700; margin: 0;">${prjName.toUpperCase()} <span style="color: ${prjColor};">RELIABILITY.</span></h1>
  </div>
  `;

  try {
    await client.request(
      updateSettings({
        project_name: prjName,
        project_color: prjColor,
        public_note: cssInjection,
        theme_light_overrides: {
          primary: prjColor,
          borderRadius: "16px",
          navigationBackground: "#000c24",
          navigationForeground: "#ffffff",
        },
      } as any),
    );
    console.log("✨ Branding applied!");

    try {
      await createCollectionAndFields();
      console.log("🏗️ Schema alignment complete!");
    } catch (error) {
      console.error("❌ Error aligning schema:", error);
    }
  } catch (error) {
    console.error("❌ Error setting up branding:", error);
  }
}

async function createCollectionAndFields() {
  const collectionName = "contact_submissions";

  try {
    await client.request(
      createCollection({
        collection: collectionName,
        schema: {},
        meta: {
          icon: "contact_mail",
          display_template: "{{name}} <{{email}}>",
        },
      } as any),
    );

    // Add ID field
    await client.request(
      createField(collectionName, {
        field: "id",
        type: "integer",
        meta: { hidden: true },
        schema: { is_primary_key: true, has_auto_increment: true },
      }),
    );
    console.log(`✅ Collection ${collectionName} created.`);
  } catch (e: any) {
    console.log(`ℹ️ Collection ${collectionName} status: ${e.message}`);
  }

  const safeAddField = async (field: string, type: string, meta: any = {}) => {
    try {
      await client.request(createField(collectionName, { field, type, meta }));
      console.log(`✅ Field ${field} added.`);
    } catch (e: any) {
      // Ignore if exists
    }
  };

  await safeAddField("name", "string", { interface: "input" });
  await safeAddField("email", "string", { interface: "input" });
  await safeAddField("company", "string", { interface: "input" });
  await safeAddField("message", "text", { interface: "textarea" });
  await safeAddField("date_created", "timestamp", {
    interface: "datetime",
    special: ["date-created"],
  });
}

setupBranding();
