#!/usr/bin/env node
/**
 * Setup database script
 * Initializes the SQLite database with the schema
 */

const fs = require('fs');
const path = require('path');
const { initDatabase, getDatabase, closeDatabase } = require('../src/database/connection');
const { initEncryption } = require('../src/utils/encryption');
const crypto = require('crypto');

const DATA_ROOT = process.env.CHAT_DATA_ROOT || path.join(__dirname, '..', '.data');
const DATABASE_PATH = process.env.DATABASE_PATH || path.join(DATA_ROOT, 'shopify_ai.db');
const DATABASE_ENCRYPTION_KEY = process.env.DATABASE_ENCRYPTION_KEY;
const WAL_MODE = process.env.DATABASE_WAL_MODE !== '0' && process.env.DATABASE_WAL_MODE !== 'false';

async function setupDatabase() {
  console.log('🔧 Setting up database...');
  console.log('  Database path:', DATABASE_PATH);
  
  // Ensure data directory exists
  const dataDir = path.dirname(DATABASE_PATH);
  if (!fs.existsSync(dataDir)) {
    fs.mkdirSync(dataDir, { recursive: true });
    console.log('  Created data directory:', dataDir);
  }
  
  // Check if encryption key is provided
  if (!DATABASE_ENCRYPTION_KEY) {
    console.warn('⚠️  WARNING: No DATABASE_ENCRYPTION_KEY found!');
    console.warn('⚠️  Generating a random key for this session (not persistent).');
    console.warn('⚠️  For production, set DATABASE_ENCRYPTION_KEY environment variable.');
    console.warn('⚠️  Generate one with: openssl rand -hex 32');
    const generatedKey = crypto.randomBytes(32).toString('hex');
    process.env.DATABASE_ENCRYPTION_KEY = generatedKey;
    console.log('✅ Generated temporary encryption key');
  } else {
    console.log('✅ Using encryption key from environment');
  }
  
  // Initialize encryption
  try {
    initEncryption(process.env.DATABASE_ENCRYPTION_KEY);
    console.log('✅ Encryption initialized');
  } catch (error) {
    console.error('❌ Failed to initialize encryption:', error.message);
    process.exit(1);
  }
  
  // Initialize database
  try {
    initDatabase(DATABASE_PATH, {
      verbose: false,
      walMode: WAL_MODE
    });
    console.log('✅ Database initialized');
  } catch (error) {
    console.error('❌ Failed to initialize database:', error.message);
    process.exit(1);
  }
  
  // Load and execute schema
  try {
    const schemaPath = path.join(__dirname, '..', 'src', 'database', 'schema.sql');
    const schema = fs.readFileSync(schemaPath, 'utf8');
    
    const db = getDatabase();
    
    // Execute the entire schema as one block
    // SQLite can handle multiple statements with exec()
    db.exec(schema);
    
    console.log('✅ Database schema created');
  } catch (error) {
    console.error('❌ Failed to create schema:', error.message);
    closeDatabase();
    process.exit(1);
  }
  
  // Verify tables
  try {
    const db = getDatabase();
    const tables = db.prepare(`
      SELECT name FROM sqlite_master 
      WHERE type='table' AND name NOT LIKE 'sqlite_%'
      ORDER BY name
    `).all();
    
    console.log('✅ Database tables created:');
    tables.forEach(table => {
      console.log(`  - ${table.name}`);
    });
  } catch (error) {
    console.error('❌ Failed to verify tables:', error.message);
  }
  
  // Close database
  closeDatabase();
  
  console.log('');
  console.log('✅ Database setup complete!');
  console.log('');
  console.log('Next steps:');
  console.log('  1. Run migration: node scripts/migrate-to-database.js');
  console.log('  2. Verify migration: node scripts/verify-migration.js');
  console.log('  3. Switch to database mode: unset USE_JSON_DATABASE');
  console.log('  4. Start server: npm start');
}

// Run setup
setupDatabase().catch(error => {
  console.error('❌ Setup failed:', error);
  process.exit(1);
});
