#!/bin/bash

# TimeSafari Migration Time Tracker
# Usage: ./time-migration.sh ComponentName.vue

if [ $# -eq 0 ]; then
    echo "Usage: $0 ComponentName.vue"
    exit 1
fi

COMPONENT=$1
TIMESTAMP=$(date +"%Y-%m-%d %H:%M")
LOG_FILE="migration-times.log"

# Create log file header if it doesn't exist
if [ ! -f "$LOG_FILE" ]; then
    echo "Component,Start Time,End Time,Duration (minutes),Complexity,Issues,Commit" > "$LOG_FILE"
fi

# Check if this is start or end
if [ "$2" = "start" ]; then
    echo "⏱️  Starting migration: $COMPONENT at $TIMESTAMP"
    echo "$COMPONENT,$TIMESTAMP,,,,,," >> "$LOG_FILE"
    echo "Run: ./time-migration.sh $COMPONENT end"
elif [ "$2" = "end" ]; then
    # Calculate duration and update log
    START_TIME=$(grep "^$COMPONENT," "$LOG_FILE" | tail -1 | cut -d',' -f2)
    if [ -z "$START_TIME" ]; then
        echo "❌ No start time found for $COMPONENT"
        exit 1
    fi
    
    # Calculate duration in minutes
    START_EPOCH=$(date -d "$START_TIME" +%s)
    END_EPOCH=$(date +%s)
    DURATION=$(( (END_EPOCH - START_EPOCH) / 60 ))
    
    # Get latest commit hash
    COMMIT=$(git log --oneline -1 | cut -d' ' -f1)
    
    echo "⏱️  Completed migration: $COMPONENT"
    echo "   Duration: $DURATION minutes"
    echo "   Commit: $COMMIT"
    
    # Update log file
    sed -i "s/^$COMPONENT,$START_TIME,,,,,,$/$COMPONENT,$START_TIME,$TIMESTAMP,$DURATION,TBD,None,$COMMIT/" "$LOG_FILE"
    
    # Show summary
    echo "📊 Migration Summary:"
    echo "   Component: $COMPONENT"
    echo "   Start: $START_TIME"
    echo "   End: $TIMESTAMP"
    echo "   Duration: $DURATION minutes"
    echo "   Commit: $COMMIT"
else
    echo "Usage: $0 ComponentName.vue [start|end]"
    echo "Example: $0 MyComponent.vue start"
    echo "         $0 MyComponent.vue end"
fi 