#!/bin/bash
#===============================================================================
# Storage Reorganization Script
# 
# Purpose: Reorganize existing files in MinIO to match storage contract structure
# Run: kubectl exec -n geocrop pod/geocrop-worker-XXXXX -- /bin/sh -c "$(cat reorganize.sh)"
#===============================================================================

set -euo pipefail

# Setup mc alias
mc alias set local http://minio:9000 minioadmin minioadmin123

echo "=== Starting Storage Reorganization ==="

# 1. Reorganize geocrop-baselines
echo "1. Reorganizing geocrop-baselines..."

# List and move Agreement files
for obj in $(mc ls local/geocrop-baselines/dw/zim/summer/ 2>/dev/null | grep "DW_Zim_Agreement" | sed 's/.*STANDARD //'); do
  season=$(echo "$obj" | sed 's/DW_Zim_Agreement_\(...._....\).*/\1/')
  mc cp "local/geocrop-baselines/dw/zim/summer/$obj" "local/geocrop-baselines/dw/zim/summer/$season/agreement/$obj" 2>/dev/null || true
  mc rm "local/geocrop-baselines/dw/zim/summer/$obj" 2>/dev/null || true
done

# Note: For HighestConf and Mode files, they need to be uploaded separately

# 2. Reorganize geocrop-datasets
echo "2. Reorganizing geocrop-datasets..."

# Move CSV files to datasets/zimbabwe-full/v1/data/
for obj in $(mc ls local/geocrop-datasets/ 2>/dev/null | grep "Zimbabwe_Full_Augmented" | sed 's/.*STANDARD //'); do
  mc cp "local/geocrop-datasets/$obj" "local/geocrop-datasets/datasets/zimbabwe-full/v1/data/$obj" 2>/dev/null || true
  mc rm "local/geocrop-datasets/$obj" 2>/dev/null || true
done

# 3. Reorganize geocrop-models  
echo "3. Reorganizing geocrop-models..."

# Create model version directory
mc mb local/geocrop-models/models/xgboost-crop/v1 2>/dev/null || true

# Move model files - rename to standard names
mc cp local/geocrop-models/Zimbabwe_XGBoost_Model.pkl local/geocrop-models/models/xgboost-crop/v1/model.joblib 2>/dev/null || true
mc rm local/geocrop-models/Zimbabwe_XGBoost_Model.pkl 2>/dev/null || true

# Add other models as needed...

echo "=== Reorganization Complete ==="
