#!/bin/bash

set -e

SERVICE_NAME=$1

# Source OS helper for cross-platform commands
source "$(dirname "$0")/os-helper.sh"

# EN: Validate arguments
# VI: Xác thực tham số
if [ -z "$SERVICE_NAME" ]; then
    echo "Usage: $0 <service-name>"
    echo "Example: $0 payment-service"
    exit 1
fi

SERVICE_DIR="services/$SERVICE_NAME"

# EN: Check if service directory already exists
# VI: Kiểm tra xem thư mục service đã tồn tại chưa
if [ -d "$SERVICE_DIR" ]; then
    echo "❌ Service $SERVICE_NAME already exists"
    exit 1
fi

echo "📦 Creating new service: $SERVICE_NAME..."

# EN: Copy template
# VI: Copy template
cp -r services/_template "$SERVICE_DIR"

# EN: Update package.json
# VI: Cập nhật package.json
cd "$SERVICE_DIR"
run_sed "s/@goodgo\/service-template/@goodgo\/$SERVICE_NAME/g" package.json

# EN: Update .env.example
# VI: Cập nhật .env.example
run_sed "s/SERVICE_NAME=service-name/SERVICE_NAME=$SERVICE_NAME/g" .env.example

# EN: Update Dockerfile port if needed
# VI: Cập nhật port Dockerfile nếu cần
# (Keep default 5000, user can change later)

cd ../..

echo "✅ Service $SERVICE_NAME created!"
echo ""
echo "Next steps:"
echo "1. Update $SERVICE_DIR/package.json if needed"
echo "2. Update $SERVICE_DIR/.env.example with service-specific configs"
echo "3. Implement your service logic in $SERVICE_DIR/src/"
echo "4. Add Prisma schema if needed: $SERVICE_DIR/prisma/schema.prisma"
