#!/bin/bash
# build_reel_v3.sh — Villarroel v3
# Guion corregido: vertical = tejido microperforado, privacidad, luz, sol tarde
# Audio: Roger real (4s intro) + ElevenLabs continuación (14.7s) = 18.7s total
# Secuencia: Roger hablando → corredero+vertical conjunto → vertical bajado → tejido → desde dentro → cierre

set -e
RAW="/root/instagram-insights/reels/villarroel/raw"
TMP="/root/instagram-insights/reels/villarroel/tmp_v3"
AUDIO_VOZ="/tmp/villarroel_v3_voz.mp3"    # Roger real 4s + ElevenLabs 14.7s
AUDIO_MUSIC="/tmp/musica_bg.mp3"
OUT="/root/instagram-insights/reels/villarroel/villarroel_v3.mp4"

mkdir -p "$TMP"

VF='scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1,fps=30'

echo "=== PASO 1: Clips ==="

# [0-4s] Roger hablando (voz real): "Esto de aquí es un toldo vertical con guías..."
echo "[1/6] IMG_2322 t=0 — Roger explicando (4s)..."
ffmpeg -y -ss 0 -t 4 -i "$RAW/IMG_2322 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg1.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg1.mp4 | cut -f1)"

# [4-7s] Corredero + vertical juntos — el sistema completo
echo "[2/6] IMG_2358 t=3 — Sistema corredero+vertical (3s)..."
ffmpeg -y -ss 3 -t 3 -i "$RAW/IMG_2358 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg2.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg2.mp4 | cut -f1)"

# [7-10s] Vertical bajado (sol tarde, protección lateral)
echo "[3/6] IMG_2350 t=0 — Vertical bajado exterior (3s)..."
ffmpeg -y -ss 0 -t 3 -i "$RAW/IMG_2350 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg3.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg3.mp4 | cut -f1)"

# [10-13s] Macro tejido microperforado (privacidad + luz)
echo "[4/6] IMG_2343 t=0 — Tejido microperforado (3s)..."
ffmpeg -y -ss 0 -t 3 -i "$RAW/IMG_2343 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg4.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg4.mp4 | cut -f1)"

# [13-16s] Vista desde dentro — luminosidad demostrada
echo "[5/6] IMG_2364 t=0 — Vista desde dentro, luz (3s)..."
ffmpeg -y -ss 0 -t 3 -i "$RAW/IMG_2364 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg5.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg5.mp4 | cut -f1)"

# [16-18.7s] Ático completo con muebles — cierre
echo "[6/6] IMG_2373 t=0 — Ático completo cierre (2.7s)..."
ffmpeg -y -ss 0 -t 2.7 -i "$RAW/IMG_2373 2.MOV" -vf "$VF" \
  -c:v libx264 -preset fast -crf 20 -an "$TMP/seg6.mp4" 2>/dev/null
echo "  $(du -sh $TMP/seg6.mp4 | cut -f1)"

echo ""
echo "=== PASO 2: Concat + audio ==="

cat > "$TMP/concat.txt" << EOF
file 'seg1.mp4'
file 'seg2.mp4'
file 'seg3.mp4'
file 'seg4.mp4'
file 'seg5.mp4'
file 'seg6.mp4'
EOF

# Audio: voz (Roger real 0-4s + ElevenLabs 4-18.7s) al 100%
#        música ambient al 20% de fondo
ffmpeg -y \
  -f concat -safe 0 -i "$TMP/concat.txt" \
  -i "$AUDIO_VOZ" \
  -i "$AUDIO_MUSIC" \
  -filter_complex "
    [1:a]volume=1.0[voz];
    [2:a]volume=0.20,atrim=0:18.7[musica];
    [voz][musica]amix=inputs=2:duration=first:dropout_transition=1[audio_out]
  " \
  -map 0:v -map "[audio_out]" \
  -c:v copy \
  -c:a aac -b:a 192k \
  -movflags +faststart \
  -shortest \
  "$OUT" 2>/dev/null

echo ""
echo "=== RESULTADO ==="
ls -lh "$OUT"
ffprobe -v quiet -show_entries stream=width,height,codec_type -show_entries format=duration \
  -of default=noprint_wrappers=1 "$OUT" 2>/dev/null | grep -E "width|height|duration|video|audio"

rm -rf "$TMP"
echo "✔ Listo: $OUT"
