#!/bin/bash

# Development script to inspect build.rs generated files
# This script copies the latest generated files from the build output to a development directory
# for easy inspection and debugging.

set -e

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${BLUE}🔍 Build.rs Generated Files Inspector${NC}"
echo ""

# Find the project root
if [[ ! -f "Cargo.toml" ]]; then
    echo -e "${RED}❌ Error: Must be run from project root (where Cargo.toml is located)${NC}"
    exit 1
fi

# Development directory paths
SHARED_DEV_DIR="crates/shared/generated"
SHARED_MANIFEST_DIR="crates/shared"

# Generated file names
GENERATED_FILES=(
    "generated_content_types.rs"
    "generated_routes.rs"
    "resource_registry.rs"
    "embedded_routes.toml"
)

echo -e "${BLUE}📁 Development directory: ${SHARED_DEV_DIR}${NC}"
echo ""

# Ensure development directory exists
mkdir -p "${SHARED_DEV_DIR}"

# Find the latest build output directory
echo -e "${BLUE}🔍 Finding latest build output...${NC}"
LATEST_SHARED_BUILD=$(find target -name "shared-*" -type d -path "*/build/*" | head -1)

if [[ -z "${LATEST_SHARED_BUILD}" ]]; then
    echo -e "${YELLOW}⚠️  No build output found. Running cargo check first...${NC}"
    cargo check
    echo ""

    # Try again
    LATEST_SHARED_BUILD=$(find target -name "shared-*" -type d -path "*/build/*" | head -1)

    if [[ -z "${LATEST_SHARED_BUILD}" ]]; then
        echo -e "${RED}❌ Error: Could not find build output after running cargo check${NC}"
        exit 1
    fi
fi

BUILD_OUT_DIR="${LATEST_SHARED_BUILD}/out"
echo -e "${GREEN}✅ Found build output: ${BUILD_OUT_DIR}${NC}"
echo ""

# Check if build output directory exists and has generated files
if [[ ! -d "${BUILD_OUT_DIR}" ]]; then
    echo -e "${RED}❌ Error: Build output directory does not exist: ${BUILD_OUT_DIR}${NC}"
    exit 1
fi

# Copy generated files to development directory
echo -e "${BLUE}📋 Copying generated files to development directory...${NC}"
echo ""

copied_count=0
missing_count=0

for file in "${GENERATED_FILES[@]}"; do
    src_file="${BUILD_OUT_DIR}/${file}"
    dest_file="${SHARED_DEV_DIR}/${file}"

    if [[ -f "${src_file}" ]]; then
        cp "${src_file}" "${dest_file}"
        size=$(stat -f%z "${dest_file}" 2>/dev/null || stat -c%s "${dest_file}" 2>/dev/null || echo "unknown")
        echo -e "${GREEN}✅ ${file} (${size} bytes)${NC}"
        ((copied_count++))
    else
        echo -e "${YELLOW}⚠️  ${file} (not found)${NC}"
        ((missing_count++))
    fi
done

echo ""

# Show summary
if [[ ${copied_count} -gt 0 ]]; then
    echo -e "${GREEN}🎉 Successfully copied ${copied_count} generated files${NC}"
else
    echo -e "${RED}❌ No files were copied${NC}"
fi

if [[ ${missing_count} -gt 0 ]]; then
    echo -e "${YELLOW}⚠️  ${missing_count} files were not found in build output${NC}"
fi

echo ""

# Show file locations and quick access commands
echo -e "${BLUE}📍 Generated files locations:${NC}"
echo -e "  Development: ${PWD}/${SHARED_DEV_DIR}/"
echo -e "  Build output: ${PWD}/${BUILD_OUT_DIR}/"
echo ""

echo -e "${BLUE}🔧 Quick access commands:${NC}"
echo -e "  View content types: ${YELLOW}cat ${SHARED_DEV_DIR}/generated_content_types.rs${NC}"
echo -e "  View routes:        ${YELLOW}cat ${SHARED_DEV_DIR}/generated_routes.rs${NC}"
echo -e "  View resources:     ${YELLOW}cat ${SHARED_DEV_DIR}/resource_registry.rs${NC}"
echo -e "  View embedded TOML: ${YELLOW}cat ${SHARED_DEV_DIR}/embedded_routes.toml${NC}"
echo ""

echo -e "${BLUE}💡 Tips:${NC}"
echo -e "  - Files are automatically copied during debug builds"
echo -e "  - Set ${YELLOW}RUSTELO_DEV_GENERATED=true${NC} to force copying in release builds"
echo -e "  - Run ${YELLOW}just inspect-generated${NC} to refresh these files"
echo -e "  - See ${YELLOW}${SHARED_DEV_DIR}/README.md${NC} for more information"
echo ""
