#!/bin/bash

# PhoneStamp WooCommerce Plugin Translation Helper
# This script helps manage translations for the plugin

PLUGIN_DIR="$(dirname "$0")"
LANGUAGES_DIR="$PLUGIN_DIR/languages"
DOMAIN="phonestamp"

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

echo "PhoneStamp WooCommerce Translation Helper"
echo "========================================"

# Function to extract translatable strings and update POT file
update_pot() {
    echo -e "${YELLOW}Updating translation template (.pot file)...${NC}"
    
    # Use wp-cli if available, otherwise use xgettext
    if command -v wp &> /dev/null; then
        wp i18n make-pot "$PLUGIN_DIR" "$LANGUAGES_DIR/$DOMAIN.pot" --domain="$DOMAIN"
    elif command -v xgettext &> /dev/null; then
        find "$PLUGIN_DIR" -name "*.php" -not -path "*/vendor/*" -not -path "*/node_modules/*" | \
        xgettext --files-from=- \
            --language=PHP \
            --keyword=__ \
            --keyword=_e \
            --keyword=_n:1,2 \
            --keyword=_x:1,2c \
            --keyword=_ex:1,2c \
            --keyword=_nx:4c,1,2 \
            --keyword=esc_attr__ \
            --keyword=esc_attr_e \
            --keyword=esc_attr_x:1,2c \
            --keyword=esc_html__ \
            --keyword=esc_html_e \
            --keyword=esc_html_x:1,2c \
            --sort-output \
            --output="$LANGUAGES_DIR/$DOMAIN.pot"
    else
        echo -e "${RED}Error: Neither wp-cli nor xgettext found. Please install one of them.${NC}"
        exit 1
    fi
    
    echo -e "${GREEN}Translation template updated successfully!${NC}"
}

# Function to compile .po files to .mo files
compile_translations() {
    echo -e "${YELLOW}Compiling translation files...${NC}"
    
    for po_file in "$LANGUAGES_DIR"/*.po; do
        if [[ -f "$po_file" ]]; then
            mo_file="${po_file%.po}.mo"
            if command -v msgfmt &> /dev/null; then
                msgfmt -o "$mo_file" "$po_file"
                echo -e "${GREEN}Compiled: $(basename "$po_file") -> $(basename "$mo_file")${NC}"
            else
                echo -e "${RED}Error: msgfmt not found. Please install gettext tools.${NC}"
                exit 1
            fi
        fi
    done
}

# Function to show available translations
show_translations() {
    echo -e "${YELLOW}Available translations:${NC}"
    for po_file in "$LANGUAGES_DIR"/*.po; do
        if [[ -f "$po_file" ]]; then
            locale=$(basename "$po_file" .po | sed "s/$DOMAIN-//")
            echo "  - $locale"
        fi
    done
}

# Main menu
case "${1:-menu}" in
    "pot"|"update-pot")
        update_pot
        ;;
    "compile")
        compile_translations
        ;;
    "list"|"show")
        show_translations
        ;;
    "all")
        update_pot
        compile_translations
        ;;
    "menu"|*)
        echo "Usage: $0 [command]"
        echo ""
        echo "Commands:"
        echo "  pot, update-pot    Update the translation template (.pot file)"
        echo "  compile            Compile all .po files to .mo files"
        echo "  list, show         Show available translations"
        echo "  all                Update POT and compile all translations"
        echo ""
        show_translations
        ;;
esac