/**
 * Initialize config table with column visibility settings for characterHistory table
 */

import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';
import { config } from '../src/lib/server/db/schema';
import { like } from 'drizzle-orm';

// Load environment variables
const DATABASE_URL = process.env.DATABASE_URL || 'file:local.db';

const client = createClient({ url: DATABASE_URL });
const db = drizzle(client);

// Define the columns in characterHistory table
const columns = [
	'gender',
	'affiliations',
	'haki',
	'bounty',
	'height',
	'origin',
	'devilFruitType',
	'arc',
	'status',
	'age'
] as const;

async function initColumnConfig(): Promise<void> {
	try {
		console.log('Initializing column visibility config...\n');
		
		for (const column of columns) {
			const key = `characterHistory.column.${column}.visible`;
			const value = 'true';
			
			await db.insert(config)
				.values({ key, value })
				.onConflictDoNothing();
			
			console.log(`✓ Added config key: ${key} = ${value}`);
		}
		
		console.log('\n✓ Successfully initialized column visibility config');
		
		// Display all config entries
		const allConfig = await db.select()
			.from(config)
			.where(like(config.key, 'characterHistory.column.%.visible'));
		
		console.log('\nCurrent column visibility config:');
		allConfig.forEach(row => {
			console.log(`  ${row.key}: ${row.value}`);
		});
		
	} catch (error) {
		console.error('Error initializing config:', error);
		process.exit(1);
	} finally {
		client.close();
	}
}

initColumnConfig();
