#!/bin/bash

# Build script for RAG-Guard frontend
# This script builds the frontend and copies it to the backend static directory

set -e  # Exit on any error

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

# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FRONTEND_DIR="$SCRIPT_DIR/frontend"
BACKEND_DIR="$SCRIPT_DIR/backend"
STATIC_DIR="$BACKEND_DIR/static"

echo -e "${BLUE}🏗️  RAG-Guard Frontend Build Script${NC}"
echo -e "${BLUE}====================================${NC}"

# Check if frontend directory exists
if [[ ! -d "$FRONTEND_DIR" ]]; then
    echo -e "${RED}❌ Frontend directory not found: $FRONTEND_DIR${NC}"
    exit 1
fi

# Check if package.json exists
if [[ ! -f "$FRONTEND_DIR/package.json" ]]; then
    echo -e "${RED}❌ package.json not found in frontend directory${NC}"
    exit 1
fi

# Navigate to frontend directory
cd "$FRONTEND_DIR"

# Check if node_modules exists, if not install dependencies
if [[ ! -d "node_modules" ]]; then
    echo -e "${YELLOW}📦 Installing frontend dependencies...${NC}"
    npm install
    echo -e "${GREEN}✅ Dependencies installed${NC}"
fi

# Clean previous build
echo -e "${YELLOW}🧹 Cleaning previous build...${NC}"
if [[ -d "dist" ]]; then
    rm -rf dist
fi

# Build the frontend
echo -e "${YELLOW}🔨 Building frontend...${NC}"
npm run build

# Check if build was successful
if [[ ! -d "dist" ]]; then
    echo -e "${RED}❌ Build failed - dist directory not created${NC}"
    exit 1
fi

# Create backend static directory
echo -e "${YELLOW}📂 Preparing backend static directory...${NC}"
mkdir -p "$STATIC_DIR"

# Remove old static files
if [[ -d "$STATIC_DIR" ]]; then
    rm -rf "$STATIC_DIR"/*
fi

# Copy built files to backend static directory
echo -e "${YELLOW}📋 Copying built files to backend...${NC}"
cp -r dist/* "$STATIC_DIR/"

# Verify copy was successful
if [[ ! -f "$STATIC_DIR/index.html" ]]; then
    echo -e "${RED}❌ Copy failed - index.html not found in static directory${NC}"
    exit 1
fi

# Show build summary
echo -e "${GREEN}✅ Build completed successfully!${NC}"
echo
echo -e "${BLUE}📊 Build Summary:${NC}"
echo -e "   Frontend built in: ${FRONTEND_DIR}/dist"
echo -e "   Files copied to:   ${STATIC_DIR}"
echo

# Show file sizes
echo -e "${BLUE}📈 Generated Files:${NC}"
if command -v du >/dev/null 2>&1; then
    cd "$STATIC_DIR"
    echo -e "   index.html:        $(du -h index.html 2>/dev/null | cut -f1 || echo 'N/A')"
    if [[ -d "assets" ]]; then
        echo -e "   assets/:           $(du -sh assets 2>/dev/null | cut -f1 || echo 'N/A')"
    fi
    if [[ -d "models" ]]; then
        echo -e "   models/:           $(du -sh models 2>/dev/null | cut -f1 || echo 'N/A')"
    fi
    echo -e "   Total:             $(du -sh . 2>/dev/null | cut -f1 || echo 'N/A')"
fi

echo
echo -e "${GREEN}🚀 Ready to serve!${NC}"
echo -e "   Start the backend server and visit: ${BLUE}http://localhost:8000${NC}"
echo
echo -e "${YELLOW}💡 Development tip:${NC}"
echo -e "   For development, run frontend dev server separately:"
echo -e "   ${BLUE}cd frontend && npm run dev${NC} (then visit http://localhost:5173)"
echo
