#!/bin/bash
# Generate CSV files from GCS v2 assets
# Usage: bash commands/generate_csvs.sh

set -euo pipefail

BASE_URL="https://storage.googleapis.com/mixio_public_assets/temp-uploads/maharana_pratap_trailer/v2"
GCS_BASE="gs://mixio_public_assets/temp-uploads/maharana_pratap_trailer/v2"
OUTPUT_DIR="maharana_pratap_trailer/assets"

echo "=== Generating storyboard_act1.csv (images from Act_1) ==="

# CSV header
echo "Scene Name,Shot Name,Filename,GCS URL" > "${OUTPUT_DIR}/storyboard_act1.csv"

# List all image files from Act_1 on GCS
gsutil ls -r "${GCS_BASE}/Act_1/**" 2>/dev/null | while read -r line; do
  # Skip directory markers
  [[ "$line" == */ ]] && continue
  [[ "$line" == *":$" ]] && continue
  [[ "$line" == *":" ]] && continue
  
  # Only include image files
  ext="${line##*.}"
  ext_lower=$(echo "$ext" | tr '[:upper:]' '[:lower:]')
  case "$ext_lower" in
    png|jpg|jpeg|webp|gif|bmp|tiff) ;;
    *) continue ;;
  esac
  
  # Extract relative path after Act_1/
  rel_path="${line#${GCS_BASE}/Act_1/}"
  
  # Parse scene name (first directory component)
  scene_name=$(echo "$rel_path" | cut -d'/' -f1)
  
  # Parse shot name (second directory component, or "root" if file is directly in scene)
  remaining="${rel_path#${scene_name}/}"
  if [[ "$remaining" == *"/"* ]]; then
    shot_name=$(echo "$remaining" | cut -d'/' -f1)
    filename=$(basename "$remaining")
  else
    shot_name=""
    filename="$remaining"
  fi
  
  # Build public URL
  public_url="${BASE_URL}/Act_1/${rel_path}"
  
  echo "\"${scene_name}\",\"${shot_name}\",\"${filename}\",\"${public_url}\""
done >> "${OUTPUT_DIR}/storyboard_act1.csv"

echo "  -> Written to ${OUTPUT_DIR}/storyboard_act1.csv"


echo "=== Generating videos_act1.csv (videos from Act_1) ==="

echo "Scene Name,Shot Name,Filename,GCS URL" > "${OUTPUT_DIR}/videos_act1.csv"

gsutil ls -r "${GCS_BASE}/Act_1/**" 2>/dev/null | while read -r line; do
  [[ "$line" == */ ]] && continue
  [[ "$line" == *":$" ]] && continue
  [[ "$line" == *":" ]] && continue
  
  ext="${line##*.}"
  ext_lower=$(echo "$ext" | tr '[:upper:]' '[:lower:]')
  case "$ext_lower" in
    mp4|mov|avi|mkv|webm) ;;
    *) continue ;;
  esac
  
  rel_path="${line#${GCS_BASE}/Act_1/}"
  scene_name=$(echo "$rel_path" | cut -d'/' -f1)
  
  remaining="${rel_path#${scene_name}/}"
  if [[ "$remaining" == *"/"* ]]; then
    shot_name=$(echo "$remaining" | cut -d'/' -f1)
    filename=$(basename "$remaining")
  else
    shot_name=""
    filename="$remaining"
  fi
  
  public_url="${BASE_URL}/Act_1/${rel_path}"
  
  echo "\"${scene_name}\",\"${shot_name}\",\"${filename}\",\"${public_url}\""
done >> "${OUTPUT_DIR}/videos_act1.csv"

echo "  -> Written to ${OUTPUT_DIR}/videos_act1.csv"


echo "=== Generating references.csv (images from references/characters,locations,props) ==="

echo "Category,Subcategory,Filename,GCS URL" > "${OUTPUT_DIR}/references.csv"

for category in characters locations props; do
  gsutil ls -r "${GCS_BASE}/references/${category}/**" 2>/dev/null | while read -r line; do
    [[ "$line" == */ ]] && continue
    [[ "$line" == *":$" ]] && continue
    [[ "$line" == *":" ]] && continue
    
    ext="${line##*.}"
    ext_lower=$(echo "$ext" | tr '[:upper:]' '[:lower:]')
    case "$ext_lower" in
      png|jpg|jpeg|webp|gif|bmp|tiff) ;;
      *) continue ;;
    esac
    
    rel_path="${line#${GCS_BASE}/references/${category}/}"
    
    # Subcategory is the first directory component
    subcategory=$(echo "$rel_path" | cut -d'/' -f1)
    filename=$(basename "$rel_path")
    
    public_url="${BASE_URL}/references/${category}/${rel_path}"
    
    echo "\"${category}\",\"${subcategory}\",\"${filename}\",\"${public_url}\""
  done
done >> "${OUTPUT_DIR}/references.csv"

echo "  -> Written to ${OUTPUT_DIR}/references.csv"

echo ""
echo "=== Done! ==="
echo "Files generated:"
echo "  1. ${OUTPUT_DIR}/storyboard_act1.csv"
echo "  2. ${OUTPUT_DIR}/videos_act1.csv"
echo "  3. ${OUTPUT_DIR}/references.csv"
