// Módulo de Over-The-Air (OTA) Bundle Updater para o APK Android e PWA export class OtaUpdater { constructor(options = {}) { this.serverUrl = options.serverUrl || window.location.origin || 'https://remote.winicomw.ddnsfree.com'; this.currentVersion = localStorage.getItem('maestri_ota_version') || '1.0.0'; this.currentBuildId = localStorage.getItem('maestri_ota_build_id') || 'initial'; } isNativeApp() { return typeof window.Capacitor !== 'undefined' && window.Capacitor.isNativePlatform(); } async checkForUpdates() { try { console.log(`[OTA] Checando atualizações em ${this.serverUrl}/api/updates/latest...`); const res = await fetch(`${this.serverUrl}/api/updates/latest`, { cache: 'no-store', headers: { 'Cache-Control': 'no-cache' } }); if (!res.ok) { console.warn(`[OTA] Falha ao consultar atualizações: HTTP ${res.status}`); return { hasUpdate: false }; } const manifest = await res.json(); console.log(`[OTA] Versão local: ${this.currentVersion} (${this.currentBuildId}) | Servidor: ${manifest.version} (${manifest.buildId})`); const hasUpdate = manifest.buildId !== this.currentBuildId || manifest.version !== this.currentVersion; if (hasUpdate) { return { hasUpdate: true, manifest }; } return { hasUpdate: false }; } catch (err) { console.error('[OTA] Erro ao verificar atualizações no servidor:', err.message); return { hasUpdate: false, error: err.message }; } } async applyUpdate(manifest) { console.log(`[OTA] Baixando e aplicando novo bundle v${manifest.version}...`); localStorage.setItem('maestri_ota_version', manifest.version); localStorage.setItem('maestri_ota_build_id', manifest.buildId); // Se estiver em PWA ou WebView local, recarrega a página para puxar os novos assets if (window.location) { setTimeout(() => { window.location.reload(); }, 1000); } } }