import { createClient, SupabaseClient } from '@supabase/supabase-js';
const SUPABASE_URL = 'https://supabase.optihost.pro';
const SUPABASE_ANON_KEY = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc2MTUwNDEyMCwiZXhwIjo0OTE3MTc3NzIwLCJyb2xlIjoiYW5vbiJ9.C4vBY34QJYZatdT-H0W7yJGJMuf6lqIdzAypystFsPo';
const SUPABASE_SERVICE_KEY = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc2MTUwNDEyMCwiZXhwIjo0OTE3MTc3NzIwLCJyb2xlIjoic2VydmljZV9yb2xlIn0.5ZmcIfyFQhA6SxjGIcWetGId951s2Wih5qvonr6Kthk';
export class SupabaseManager {
private anonClient: SupabaseClient;
private serviceClient: SupabaseClient;
constructor() {
this.anonClient = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
this.serviceClient = createClient(SUPABASE_URL, SUPABASE_SERVICE_KEY, {
auth: {
autoRefreshToken: false,
persistSession: false
}
});
}
/**
* Get the anonymous client for regular operations
*/
getAnonClient(): SupabaseClient {
return this.anonClient;
}
/**
* Get the service client for admin operations (seeding, etc.)
*/
getServiceClient(): SupabaseClient {
return this.serviceClient;
}
/**
* Test database connection
*/
async testConnection(): Promise<boolean> {
try {
const { data, error } = await this.anonClient
.from('profiles')
.select('count', { count: 'exact', head: true });
if (error) {
console.error('Connection test failed:', error);
return false;
}
console.log('✅ Database connection successful');
return true;
} catch (error) {
console.error('Connection test error:', error);
return false;
}
}
/**
* Check if required tables exist
*/
async checkTables(): Promise<{ exists: string[]; missing: string[] }> {
const requiredTables = [
'design_styles',
'design_palettes',
'style_palette_compatibility'
];
const exists: string[] = [];
const missing: string[] = [];
for (const table of requiredTables) {
try {
const { error } = await this.anonClient
.from(table)
.select('count', { count: 'exact', head: true });
if (error) {
missing.push(table);
} else {
exists.push(table);
}
} catch {
missing.push(table);
}
}
return { exists, missing };
}
/**
* Log an operation with timestamp
*/
log(message: string, level: 'info' | 'warn' | 'error' = 'info'): void {
const timestamp = new Date().toISOString();
const prefix = level === 'error' ? '❌' : level === 'warn' ? '⚠️' : '🔵';
console.log(`${prefix} [${timestamp}] ${message}`);
}
}
// Singleton instance
export const supabase = new SupabaseManager();