#!/bin/bash

# LittleShop Portainer Deployment Script
# Usage: ./deploy.sh

set -e

echo "🚀 LittleShop Deployment Preparation for Portainer"
echo "================================================="

# Configuration
PORTAINER_HOST="10.0.0.51"
PORTAINER_USER="sysadmin"

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

# Check if .env file exists
if [ ! -f .env ]; then
    echo -e "${YELLOW}📝 Creating .env file from template...${NC}"
    cp .env.example .env
    echo -e "${GREEN}✅ Created .env file${NC}"
    echo ""
    echo -e "${BLUE}📋 Please edit .env file with your configuration:${NC}"
    echo "- JWT_SECRET_KEY: Strong secret key for JWT tokens"
    echo "- BTCPAY_SERVER_URL: Your BTCPay Server URL (optional)"
    echo "- BTCPAY_STORE_ID: Your BTCPay Store ID (optional)" 
    echo "- BTCPAY_API_KEY: Your BTCPay API key (optional)"
    echo "- BTCPAY_WEBHOOK_SECRET: Your BTCPay webhook secret (optional)"
    echo ""
else
    echo -e "${GREEN}✅ .env file already exists${NC}"
fi

echo -e "${YELLOW}🔨 Testing Docker build...${NC}"
if command -v docker > /dev/null 2>&1; then
    if docker build -t littleshop:test . > /dev/null 2>&1; then
        echo -e "${GREEN}✅ Docker build successful${NC}"
        docker rmi littleshop:test > /dev/null 2>&1
    else
        echo -e "${RED}❌ Docker build failed${NC}"
        exit 1
    fi
else
    echo -e "${YELLOW}⚠️  Docker not found locally - will build on target server${NC}"
fi

echo ""
echo -e "${BLUE}📋 Manual Deployment Instructions for Portainer:${NC}"
echo "=============================================="
echo ""
echo "1. 🌐 Access Portainer:"
echo "   URL: http://10.0.0.51:9000"
echo "   User: sysadmin"
echo "   Pass: Phenom12#."
echo ""
echo "2. 📊 Create New Stack:"
echo "   - Go to Stacks → Add stack"
echo "   - Name: littleshop"
echo "   - Build method: Repository"
echo "   - Repository URL: (your git repo or upload files)"
echo ""
echo "3. 🔧 Environment Variables:"
echo "   Add these variables (get values from .env file):"
echo "   - JWT_SECRET_KEY"
echo "   - BTCPAY_SERVER_URL (optional)"
echo "   - BTCPAY_STORE_ID (optional)"
echo "   - BTCPAY_API_KEY (optional)"
echo "   - BTCPAY_WEBHOOK_SECRET (optional)"
echo ""
echo "4. 🚀 Deploy:"
echo "   - Copy docker-compose.yml content to web editor"
echo "   - Click 'Deploy the stack'"
echo ""
echo "5. ✅ Verify Deployment:"
echo "   - Visit: https://littleshop.silverlabs.uk"
echo "   - Admin: https://littleshop.silverlabs.uk/Admin"
echo "   - Login: admin / admin"
echo "   - ⚠️  CHANGE PASSWORD IMMEDIATELY!"
echo ""
echo -e "${GREEN}🎯 Target Configuration:${NC}"
echo "- Portainer: ${PORTAINER_HOST}"
echo "- Hostname: littleshop.silverlabs.uk"
echo "- SSL: Let's Encrypt via Traefik"
echo "- Admin Panel: /Admin"
echo ""
echo -e "${GREEN}✨ Ready for Portainer deployment!${NC}"