#!/bin/bash

# Noble Collectable — Automated Store Setup Script
# Installs and configures all plugins, theme, and products
# Run from project root: bash setup-store.sh

set -e  # Exit on any error

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║  Noble Collectable — Automated Setup                          ║"
echo "║  Installing plugins, theme, and products...                  ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

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

# Function to run WordPress CLI commands inside Docker
wp_cli() {
    docker exec noblecollectable_wp wp "$@" --allow-root
}

# Function to print status
print_status() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_step() {
    echo -e "${YELLOW}▶ $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# Check if Docker containers are running
print_step "Checking Docker containers..."
if ! docker ps | grep -q noblecollectable_wp; then
    print_error "WordPress container not running. Start with: docker-compose up -d"
    exit 1
fi
print_status "Containers are running"
echo ""

# Install WP-CLI if not already installed
print_step "Installing WP-CLI (WordPress command-line interface)..."
docker exec noblecollectable_wp sh -c "cd /tmp && curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod +x wp-cli.phar && mv wp-cli.phar /usr/local/bin/wp" 2>&1 | grep -v "^  " || true
docker exec noblecollectable_wp wp --version --allow-root 2>&1 | head -1
print_status "WP-CLI ready"
echo ""

# Install WooCommerce
print_step "Installing WooCommerce..."
wp_cli plugin install woocommerce --activate
print_status "WooCommerce installed and activated"
echo ""

# Install Polylang (bilingual)
print_step "Installing Polylang (French/English support)..."
wp_cli plugin install polylang --activate
print_status "Polylang installed and activated"
echo ""

# Install Stripe for WooCommerce
print_step "Installing Stripe for WooCommerce..."
wp_cli plugin install woo-stripe-payment --activate
print_status "Stripe plugin installed and activated"
echo ""

# Install WP Smush (image optimization)
print_step "Installing WP Smush (image optimization)..."
wp_cli plugin install wp-smushit --activate
print_status "WP Smush installed and activated"
echo ""

# Activate Noble Collectable theme
print_step "Activating Noble Collectable theme..."
wp_cli theme activate noblecollectable-child
print_status "Noble Collectable theme activated"
echo ""

# Activate Noble Collectable Setup plugin
print_step "Activating Noble Collectable Setup plugin..."
wp_cli plugin activate noblecollectable-setup
print_status "Noble Collectable Setup plugin activated"
echo ""

# Configure WooCommerce basic settings
print_step "Configuring WooCommerce..."
wp_cli woocommerce setting set general woocommerce_store_address "London, UK"
wp_cli woocommerce setting set general woocommerce_currency EUR
wp_cli woocommerce setting set products woocommerce_enable_reviews yes
print_status "WooCommerce configured"
echo ""

# Set up shop page
print_step "Setting up shop page..."
SHOP_PAGE_ID=$(wp_cli post list --post_type=page --s="shop" --field=ID --format=ids | awk '{print $1}')
if [ -z "$SHOP_PAGE_ID" ]; then
    SHOP_PAGE_ID=$(wp_cli post create --post_type=page --post_title="Shop" --post_status=publish --porcelain)
fi
wp_cli woocommerce setting set general woocommerce_shop_page_id "$SHOP_PAGE_ID"
print_status "Shop page configured"
echo ""

# Create shipping zones
print_step "Setting up shipping zones..."
wp_cli woocommerce shipping_zone_create --name="EU Standard" --zones=AT,BE,BG,HR,CY,CZ,DK,EE,FI,FR,DE,GR,HU,IE,IT,LV,LT,LU,MT,NL,PL,PT,RO,SK,SI,ES,SE 2>/dev/null || true
wp_cli woocommerce shipping_zone_create --name="Worldwide" --zones="" 2>/dev/null || true
print_status "Shipping zones configured"
echo ""

# Set permalink structure
print_step "Configuring permalinks..."
wp_cli rewrite structure '/%postname%/' --hard
print_status "Permalinks configured"
echo ""

# Flush cache/rewrite rules
print_step "Flushing rewrite rules..."
wp_cli rewrite flush --hard
print_status "Rewrite rules flushed"
echo ""

# Final status
echo "╔════════════════════════════════════════════════════════════════╗"
echo -e "${GREEN}║  ✅ SETUP COMPLETE!                                            ║${NC}"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
echo "Your store is ready! Visit:"
echo ""
echo "  🛍️  Shop: http://localhost:8000/shop"
echo "  ⚙️  Admin: http://localhost:8000/wp-admin"
echo ""
echo "Next steps:"
echo "  1. View your 10 sample products at the shop"
echo "  2. Test add-to-cart & checkout flow"
echo "  3. Use Stripe test card: 4242 4242 4242 4242"
echo ""
echo "Documentation:"
echo "  • README.md — Overview"
echo "  • SETUP.md — Setup guide"
echo "  • docs/ADMIN_GUIDE.md — Client how-to"
echo ""
