#!/bin/bash

echo "======================================"
echo "DIAGNÓSTICO: Arquivo Faltando Após Update"
echo "======================================"
echo ""

# 1. Ver último update
echo "1. ÚLTIMO UPDATE NO BANCO:"
echo "--------------------------------------"
cd /var/www/gestorstream/backend
php artisan tinker --execute="
\$update = \App\Models\SystemUpdate::orderBy('id', 'desc')->first();
if (\$update) {
    echo 'ID: ' . \$update->id . PHP_EOL;
    echo 'Versão: ' . \$update->version . PHP_EOL;
    echo 'Status: ' . \$update->status . PHP_EOL;
    echo 'Iniciado: ' . \$update->started_at . PHP_EOL;
    echo 'Concluído: ' . \$update->completed_at . PHP_EOL;
}
"
echo ""

# 2. Ver releases baixados
echo "2. RELEASES BAIXADOS (últimos 5):"
echo "--------------------------------------"
ls -lht /var/www/gestorstream/backend/storage/app/private/updates/*.zip 2>/dev/null | head -5
echo ""

# 3. Ver conteúdo do último release baixado
echo "3. CONTEÚDO DO ÚLTIMO RELEASE:"
echo "--------------------------------------"
LAST_ZIP=$(ls -t /var/www/gestorstream/backend/storage/app/private/updates/*.zip 2>/dev/null | head -1)
if [ -f "$LAST_ZIP" ]; then
    echo "Arquivo: $LAST_ZIP"
    echo ""
    echo "Arquivos no ZIP (primeiros 50):"
    unzip -l "$LAST_ZIP" | head -50
    echo ""
    echo "Total de arquivos no ZIP:"
    unzip -l "$LAST_ZIP" | tail -1
else
    echo "Nenhum release encontrado!"
fi
echo ""

# 4. Ver versão atual do sistema
echo "4. VERSÃO ATUAL DO SISTEMA:"
echo "--------------------------------------"
cat /var/www/gestorstream/VERSION 2>/dev/null || echo "Arquivo VERSION não encontrado"
echo ""

# 5. Git status atual
echo "5. GIT STATUS ATUAL:"
echo "--------------------------------------"
cd /var/www/gestorstream
git log --oneline -5
echo ""
git status --short
echo ""

# 6. Verificar se arquivo específico existe
echo "6. PROCURAR ARQUIVO ESPECÍFICO:"
echo "--------------------------------------"
echo "Qual arquivo você está procurando?"
echo "Execute: find /var/www/gestorstream -name 'NOME_DO_ARQUIVO' -type f"
echo ""

echo "======================================"
echo "DIAGNÓSTICO COMPLETO!"
echo "======================================"
