#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-require-imports */

/**
 * Non-interactive setup script for Docker builds.
 * Generates prompts.config.ts from environment variables.
 * 
 * Usage: BRAND_NAME="My App" node scripts/docker-setup.js
 * 
 * Environment Variables:
 *   BRAND_NAME        - App name (default: "My Prompt Library")
 *   BRAND_DESCRIPTION - App description
 *   BRAND_LOGO        - Logo path (default: "/logo.svg")
 *   BRAND_LOGO_DARK   - Dark mode logo path
 *   BRAND_FAVICON     - Favicon path
 *   BRAND_COLOR       - Primary color hex (default: "#6366f1")
 *   THEME_RADIUS      - Border radius: none|sm|md|lg (default: "sm")
 *   THEME_VARIANT     - UI variant: default|flat|brutal (default: "default")
 *   THEME_DENSITY     - Spacing: compact|default|comfortable (default: "default")
 *   AUTH_PROVIDERS    - Comma-separated: github,google,credentials (default: "credentials")
 *   ALLOW_REGISTRATION - Allow public registration: true|false (default: "true")
 *   LOCALES           - Comma-separated locales (default: "en")
 *   DEFAULT_LOCALE    - Default locale (default: "en")
 *   FEATURE_PRIVATE_PROMPTS   - Enable private prompts (default: "true")
 *   FEATURE_CHANGE_REQUESTS   - Enable change requests (default: "true")
 *   FEATURE_CATEGORIES        - Enable categories (default: "true")
 *   FEATURE_TAGS              - Enable tags (default: "true")
 *   FEATURE_COMMENTS          - Enable comments (default: "true")
 *   FEATURE_AI_SEARCH         - Enable AI search (default: "false")
 *   FEATURE_AI_GENERATION     - Enable AI generation (default: "false")
 *   FEATURE_MCP               - Enable MCP (default: "false")
 */

const fs = require('fs');
const path = require('path');

const CONFIG_FILE = path.join(__dirname, '..', 'prompts.config.ts');

function env(key, defaultValue) {
  return process.env[key] || defaultValue;
}

function envBool(key, defaultValue) {
  const val = process.env[key];
  if (val === undefined) return defaultValue;
  return val.toLowerCase() === 'true' || val === '1';
}

function envArray(key, defaultValue) {
  const val = process.env[key];
  if (!val) return defaultValue;
  return val.split(',').map(s => s.trim()).filter(Boolean);
}

function generateConfig(config) {
  return `import { defineConfig } from "@/lib/config";

// Docker build configuration - generated by docker-setup.js
const useCloneBranding = true;

export default defineConfig({
  // Branding - your organization's identity
  branding: {
    name: "${config.branding.name}",
    logo: "${config.branding.logo}",
    logoDark: "${config.branding.logoDark}",
    favicon: "${config.branding.favicon}",
    description: "${config.branding.description}",
  },

  // Theme - design system configuration
  theme: {
    radius: "${config.theme.radius}",
    variant: "${config.theme.variant}",
    density: "${config.theme.density}",
    colors: {
      primary: "${config.theme.primaryColor}",
    },
  },

  // Authentication plugins
  auth: {
    providers: [${config.auth.providers.map(p => `"${p}"`).join(', ')}],
    allowRegistration: ${config.auth.allowRegistration},
  },

  // Internationalization
  i18n: {
    locales: [${config.i18n.locales.map(l => `"${l}"`).join(', ')}],
    defaultLocale: "${config.i18n.defaultLocale}",
  },

  // Features
  features: {
    privatePrompts: ${config.features.privatePrompts},
    changeRequests: ${config.features.changeRequests},
    categories: ${config.features.categories},
    tags: ${config.features.tags},
    comments: ${config.features.comments},
    aiSearch: ${config.features.aiSearch},
    aiGeneration: ${config.features.aiGeneration},
    mcp: ${config.features.mcp},
  },

  // Homepage customization (clone branding mode)
  homepage: {
    useCloneBranding,
    achievements: {
      enabled: false,
    },
    sponsors: {
      enabled: false,
      items: [],
    },
  },
});
`;
}

function main() {
  console.log('🔧 Docker Setup: Generating prompts.config.ts...');

  const config = {
    branding: {
      name: env('BRAND_NAME', 'My Prompt Library'),
      description: env('BRAND_DESCRIPTION', 'Collect, organize, and share AI prompts'),
      logo: env('BRAND_LOGO', '/logo.svg'),
      logoDark: env('BRAND_LOGO_DARK', env('BRAND_LOGO', '/logo.svg')),
      favicon: env('BRAND_FAVICON', '/logo.svg'),
    },
    theme: {
      primaryColor: env('BRAND_COLOR', '#6366f1'),
      radius: env('THEME_RADIUS', 'sm'),
      variant: env('THEME_VARIANT', 'default'),
      density: env('THEME_DENSITY', 'default'),
    },
    auth: {
      providers: envArray('AUTH_PROVIDERS', ['credentials']),
      allowRegistration: envBool('ALLOW_REGISTRATION', true),
    },
    i18n: {
      locales: envArray('LOCALES', ['en']),
      defaultLocale: env('DEFAULT_LOCALE', 'en'),
    },
    features: {
      privatePrompts: envBool('FEATURE_PRIVATE_PROMPTS', true),
      changeRequests: envBool('FEATURE_CHANGE_REQUESTS', true),
      categories: envBool('FEATURE_CATEGORIES', true),
      tags: envBool('FEATURE_TAGS', true),
      comments: envBool('FEATURE_COMMENTS', true),
      aiSearch: envBool('FEATURE_AI_SEARCH', false),
      aiGeneration: envBool('FEATURE_AI_GENERATION', false),
      mcp: envBool('FEATURE_MCP', false),
    },
  };

  // Log configuration
  console.log('');
  console.log('  Brand:      ', config.branding.name);
  console.log('  Color:      ', config.theme.primaryColor);
  console.log('  Auth:       ', config.auth.providers.join(', '));
  console.log('  Locales:    ', config.i18n.locales.join(', '));
  console.log('');

  // Generate and write config
  const content = generateConfig(config);
  fs.writeFileSync(CONFIG_FILE, content);

  console.log('✅ Generated prompts.config.ts');
}

main();
