interface CustomerConfig { customer_id: string; // UUID v7 - auto-generated display_name: string; // REQUIRED - friendly name for UI notes?: string; is_active: boolean; // Controls if customer appears in dropdowns/lists created_at: string; updated_at: string; // Environment info is tracked in backups themselves, not customer config environments: string[]; // List of environments this customer has data in (derived from backups) } interface CustomerConfigStore { configs: CustomerConfig[]; lastUpdated: string; } const STORAGE_KEY = 'backup-manager-customer-configs'; export class CustomerConfigService { private static configs: Map = new Map(); private static initialized = false; static async initialize(): Promise { if (this.initialized) return; try { const stored = localStorage.getItem(STORAGE_KEY); if (stored) { const data: CustomerConfigStore = JSON.parse(stored); data.configs.forEach(config => { this.configs.set(config.customer_id, config); }); } this.initialized = true; } catch (_error) { console.error('Failed to load customer configs:', error); } } static async save(): Promise { try { const data: CustomerConfigStore = { configs: Array.from(this.configs.values()), lastUpdated: new Date().toISOString() }; localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); } catch (_error) { console.error('Failed to save customer configs:', error); throw error; } } static getConfigs(): CustomerConfig[] { return Array.from(this.configs.values()).sort((a, b) => a.display_name.localeCompare(b.display_name) ); } static getConfig(customerId: string): CustomerConfig | null { return this.configs.get(customerId) || null; } static getDisplayName(customerId: string): string { const config = this.configs.get(customerId); if (config) { return config.display_name; } // Generate friendly display name from customer ID if (customerId === 'customer-unassigned-catchall-000') { return 'Unassigned Personas'; } // Convert customer-123-abc to "Customer 123 ABC" return customerId .replace(/^customer-/, 'Customer ') .replace(/-/g, ' ') .split(' ') .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(' '); } static async setConfig(config: Omit & { customer_id?: string }): Promise { const now = new Date().toISOString(); const customerId = config.customer_id || this.generateCustomerId(); const existing = this.configs.get(customerId); if (!config.display_name?.trim()) { throw new Error('Display name is required'); } const fullConfig: CustomerConfig = { ...config, customer_id: customerId, display_name: config.display_name.trim(), environments: config.environments || [], created_at: existing?.created_at || now, updated_at: now }; this.configs.set(customerId, fullConfig); await this.save(); } private static generateCustomerId(): string { // Generate UUID v7 for better sorting and database performance return crypto.randomUUID(); // Browser-compatible fallback to v4, but we'll use v7 in production } static async deleteConfig(customerId: string): Promise { const deleted = this.configs.delete(customerId); if (deleted) { await this.save(); } return deleted; } static async importFromBackups(backups: {customer_id: string; customer_name: string; metadata?: {environment?: string}}[]): Promise { const customerEnvironments = new Map>(); // Group environments by customer for (const backup of backups) { if (!customerEnvironments.has(backup.customer_id)) { customerEnvironments.set(backup.customer_id, new Set()); } if (backup.metadata?.environment) { customerEnvironments.get(backup.customer_id)!.add(backup.metadata.environment); } } const updates: Promise[] = []; for (const backup of backups) { if (!this.configs.has(backup.customer_id)) { const environments = Array.from(customerEnvironments.get(backup.customer_id) || []); updates.push(this.setConfig({ customer_id: backup.customer_id, display_name: backup.customer_name, environments, is_active: true })); } else { // Update environments for existing customer const existing = this.configs.get(backup.customer_id)!; const allEnvironments = new Set([...existing.environments, ...Array.from(customerEnvironments.get(backup.customer_id) || [])]); updates.push(this.setConfig({ ...existing, environments: Array.from(allEnvironments) })); } } await Promise.all(updates); } static getConfigsByEnvironment(environment: string): CustomerConfig[] { return Array.from(this.configs.values()) .filter(config => config.environments.includes(environment)) .sort((a, b) => a.display_name.localeCompare(b.display_name)); } static getEnvironmentMismatches(): { customer: CustomerConfig; missingFromLower: string[]; presentInHigher: string[]; }[] { const _envOrder = ['testing', 'staging', 'production']; const mismatches: { customer: CustomerConfig; missingFromLower: string[]; presentInHigher: string[]; }[] = []; for (const config of this.configs.values()) { const missingFromLower: string[] = []; const presentInHigher: string[] = []; // Check for environment progression violations const hasProduction = config.environments.includes('production'); const hasStaging = config.environments.includes('staging'); const hasTesting = config.environments.includes('testing'); if (hasProduction && !hasStaging) { missingFromLower.push('staging'); } if (hasProduction && !hasTesting) { missingFromLower.push('testing'); } if (hasStaging && !hasTesting) { missingFromLower.push('testing'); } if (missingFromLower.length > 0 || presentInHigher.length > 0) { mismatches.push({ customer: config, missingFromLower, presentInHigher }); } } return mismatches; } } export type { CustomerConfig };