#!/bin/bash
# ============================================================================
# ERP GENERIC - LOAD SEEDS SCRIPT
# ============================================================================
# Description: Loads seed data for the specified environment
# Usage: ./scripts/load-seeds.sh [dev|prod]
# ============================================================================

set -e

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

# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DATABASE_DIR="$(dirname "$SCRIPT_DIR")"
SEEDS_DIR="$DATABASE_DIR/seeds"

# Environment (default: dev)
ENV="${1:-dev}"

# Load environment variables
if [ -f "$DATABASE_DIR/.env" ]; then
    source "$DATABASE_DIR/.env"
elif [ -f "$DATABASE_DIR/.env.example" ]; then
    source "$DATABASE_DIR/.env.example"
fi

# Default values
POSTGRES_HOST="${POSTGRES_HOST:-localhost}"
POSTGRES_PORT="${POSTGRES_PORT:-5432}"
POSTGRES_DB="${POSTGRES_DB:-erp_generic}"
POSTGRES_USER="${POSTGRES_USER:-erp_admin}"
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-erp_secret_2024}"

# Connection string
export PGPASSWORD="$POSTGRES_PASSWORD"
PSQL_CMD="psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER -d $POSTGRES_DB"

echo -e "${BLUE}============================================${NC}"
echo -e "${BLUE}   ERP GENERIC - LOAD SEED DATA${NC}"
echo -e "${BLUE}============================================${NC}"
echo ""
echo -e "Environment: ${GREEN}$ENV${NC}"
echo -e "Database:    ${GREEN}$POSTGRES_DB${NC}"
echo ""

# Check if seeds directory exists
SEED_ENV_DIR="$SEEDS_DIR/$ENV"
if [ ! -d "$SEED_ENV_DIR" ]; then
    echo -e "${RED}Error: Seeds directory not found: $SEED_ENV_DIR${NC}"
    echo "Available environments:"
    ls -1 "$SEEDS_DIR" 2>/dev/null || echo "  (none)"
    exit 1
fi

# Check if there are SQL files
SEED_FILES=($(find "$SEED_ENV_DIR" -name "*.sql" -type f | sort))

if [ ${#SEED_FILES[@]} -eq 0 ]; then
    echo -e "${YELLOW}No seed files found in $SEED_ENV_DIR${NC}"
    echo "Create seed files with format: XX-description.sql"
    exit 0
fi

echo -e "${BLUE}Loading ${#SEED_FILES[@]} seed file(s)...${NC}"
echo ""

TOTAL=${#SEED_FILES[@]}
CURRENT=0
FAILED=0

for seed_file in "${SEED_FILES[@]}"; do
    CURRENT=$((CURRENT + 1))
    filename=$(basename "$seed_file")

    echo -e "  [${CURRENT}/${TOTAL}] Loading ${YELLOW}$filename${NC}..."

    if $PSQL_CMD -f "$seed_file" > /dev/null 2>&1; then
        echo -e "  [${CURRENT}/${TOTAL}] ${GREEN}$filename loaded successfully${NC}"
    else
        echo -e "  [${CURRENT}/${TOTAL}] ${RED}Error loading $filename${NC}"
        FAILED=$((FAILED + 1))
    fi
done

echo ""
if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}============================================${NC}"
    echo -e "${GREEN}   ALL SEEDS LOADED SUCCESSFULLY!${NC}"
    echo -e "${GREEN}============================================${NC}"
else
    echo -e "${YELLOW}============================================${NC}"
    echo -e "${YELLOW}   SEEDS LOADED WITH $FAILED ERRORS${NC}"
    echo -e "${YELLOW}============================================${NC}"
fi
echo ""
