#!/bin/bash
# Copy required binary assets from the main kotlin project.
set -e
BASE_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(cd "$BASE_DIR/.." && pwd)"

SRC_KOTLIN="$ROOT_DIR/kotlin"

# TensorFlow Lite model
mkdir -p "$BASE_DIR/app/src/main/assets"
if [ ! -f "$BASE_DIR/app/src/main/assets/mobile_facenet.tflite" ]; then
    cp "$SRC_KOTLIN/app/src/main/assets/mobile_facenet.tflite" "$BASE_DIR/app/src/main/assets/"
    echo "Copied mobile_facenet.tflite"
fi

# Logo image
mkdir -p "$BASE_DIR/app/src/main/res/drawable"
if [ ! -f "$BASE_DIR/app/src/main/res/drawable/logo.png" ]; then
    cp "$SRC_KOTLIN/app/src/main/res/drawable/logo.png" "$BASE_DIR/app/src/main/res/drawable/"
    echo "Copied logo.png"
fi

# Additional icons used by the dynamic forms modules
icons=(
    ic_attendance.png
    ic_dashboard.png
    ic_time_clock.png
    ic_location.png
    ic_approval.png
    ic_adjustment.png
    ic_summary.png
    ic_table_list.png
)

for icon in "${icons[@]}"; do
    if [ ! -f "$BASE_DIR/app/src/main/res/drawable/$icon" ]; then
        if [ -f "$SRC_KOTLIN/app/src/main/res/drawable/$icon" ]; then
            cp "$SRC_KOTLIN/app/src/main/res/drawable/$icon" "$BASE_DIR/app/src/main/res/drawable/" &&
            echo "Copied $icon"
        else
            echo "Warning: $icon not found in $SRC_KOTLIN/app/src/main/res/drawable. Download it before running this script." >&2
        fi
    fi
done

# Gradle wrapper JAR
mkdir -p "$BASE_DIR/gradle/wrapper"
if [ ! -f "$BASE_DIR/gradle/wrapper/gradle-wrapper.jar" ]; then
    cp "$SRC_KOTLIN/gradle/wrapper/gradle-wrapper.jar" "$BASE_DIR/gradle/wrapper/"
    echo "Copied gradle-wrapper.jar"
fi

echo "Assets prepared. Run ./gradlew assembleDebug to build the app."
