#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════
# EN: Seed demo data for all GoodGo Multi-Vertical POS services.
# VI: Seed dữ liệu demo cho tất cả services GoodGo POS đa ngành.
#
# Default account: hongochai10@icloud.com / Velik@2026
# Creates: 5 shops (Café, Restaurant, Karaoke, Spa, Beauty)
#          Products, Categories, Tables, Rooms, Resources, Appointments
# ═══════════════════════════════════════════════════════════════════════

set -e

# ─── Service URLs ───
IAM_URL="http://localhost:5001"
MERCHANT_URL="http://localhost:5005"
CATALOG_URL="http://localhost:5016"
ORDER_URL="http://localhost:5017"
INVENTORY_URL="http://localhost:5018"
FNB_URL="http://localhost:5019"
BOOKING_URL="http://localhost:5020"

# ─── Colors ───
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m'

log_step() { echo -e "${CYAN}▸ $1${NC}"; }
log_ok()   { echo -e "${GREEN}✓ $1${NC}"; }
log_warn() { echo -e "${YELLOW}⚠ $1${NC}"; }
log_err()  { echo -e "${RED}✗ $1${NC}"; }
log_head() { echo -e "\n${YELLOW}═══ $1 ═══${NC}"; }

# ═══════════════════════════════════════════════════════════════════════
# STEP 1: Register User + Get Token
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 1: Register User & Get Token"

log_step "Registering user hongochai10@icloud.com..."
REG_RESP=$(curl -s -w "\n%{http_code}" -X POST "${IAM_URL}/api/v1/auth/register" \
  -H 'Content-Type: application/json' \
  -d '{"email":"hongochai10@icloud.com","password":"Velik@2026","firstName":"Ho Ngoc","lastName":"Hai"}')
REG_CODE=$(echo "$REG_RESP" | tail -1)

if [ "$REG_CODE" = "201" ]; then
  log_ok "User registered successfully"
elif [ "$REG_CODE" = "409" ]; then
  log_warn "User already exists (409) — using existing account"
else
  log_warn "Registration returned HTTP $REG_CODE — continuing"
fi

log_step "Getting access token..."
TOKEN_RESP=$(curl -s -X POST "${IAM_URL}/connect/token" \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'grant_type=password&client_id=password-client&client_secret=password-client-secret&username=hongochai10@icloud.com&password=Velik@2026&scope=api')
TOKEN=$(echo "$TOKEN_RESP" | python3 -c "import sys,json; print(json.load(sys.stdin).get('access_token',''))" 2>/dev/null || echo "")

if [ -z "$TOKEN" ]; then
  log_err "Failed to get token. Response: $TOKEN_RESP"
  exit 1
fi
log_ok "Token obtained (${#TOKEN} chars)"

# ═══════════════════════════════════════════════════════════════════════
# STEP 2: Register Merchant
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 2: Register Merchant"

log_step "Registering merchant..."
MERCH_RESP=$(curl -s -w "\n%{http_code}" -X POST "${MERCHANT_URL}/api/v1/merchants/register" \
  -H 'Content-Type: application/json' \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"businessName":"GoodGo Demo Business","type":"Individual"}')
MERCH_CODE=$(echo "$MERCH_RESP" | tail -1)

if [ "$MERCH_CODE" = "201" ] || [ "$MERCH_CODE" = "200" ]; then
  log_ok "Merchant registered"
else
  log_warn "Merchant returned HTTP $MERCH_CODE (may already exist)"
fi

# ═══════════════════════════════════════════════════════════════════════
# STEP 3: Create 5 Shops
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 3: Create 5 Shops"

# Function to create shop and extract ID
create_shop() {
  local name="$1" slug="$2" category="$3" desc="$4" phone="$5" email="$6"
  local resp_body
  resp_body=$(curl -s -X POST "${MERCHANT_URL}/api/v1/shops" \
    -H 'Content-Type: application/json' \
    -H "Authorization: Bearer $TOKEN" \
    -d "{\"name\":\"$name\",\"slug\":\"$slug\",\"type\":\"PhysicalOnly\",\"category\":\"$category\",\"description\":\"$desc\",\"phone\":\"$phone\",\"email\":\"$email\"}")
  # Extract shop ID from response
  local shop_id
  shop_id=$(echo "$resp_body" | python3 -c "
import sys, json
try:
    d = json.load(sys.stdin)
    if isinstance(d, dict):
        for key in ['data', 'Data']:
            if key in d and isinstance(d[key], dict):
                for k in ['shopId', 'shop_id', 'id', 'Id']:
                    if k in d[key]: print(d[key][k]); exit()
        for k in ['shopId', 'shop_id', 'id', 'Id']:
            if k in d: print(d[k]); exit()
except: pass
" 2>/dev/null)
  if [ -n "$shop_id" ]; then
    log_ok "Shop '$name' → $shop_id"
  else
    log_warn "Shop '$name' failed — response: $(echo "$resp_body" | head -c 200)"
  fi
  # Return ONLY the ID via a temp file to avoid stdout pollution
  echo "$shop_id"
}

SHOP_CAFE=$(create_shop "Cà Phê GoodGo" "ca-phe-goodgo" "Cafe" "Quán cà phê đặc sản Việt Nam" "0901234501" "cafe@goodgo.vn" 2>/dev/null)
# Remove any non-UUID chars (color codes etc)
SHOP_CAFE=$(echo "$SHOP_CAFE" | grep -oE '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | head -1)
log_ok "Café: $SHOP_CAFE"

SHOP_RESTAURANT=$(create_shop "Nhà Hàng GoodGo" "nha-hang-goodgo" "Restaurant" "Nhà hàng ẩm thực Việt hiện đại" "0901234502" "restaurant@goodgo.vn" 2>/dev/null)
SHOP_RESTAURANT=$(echo "$SHOP_RESTAURANT" | grep -oE '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | head -1)
log_ok "Restaurant: $SHOP_RESTAURANT"

SHOP_KARAOKE=$(create_shop "Karaoke GoodGo" "karaoke-goodgo" "Karaoke" "Karaoke VIP - Hệ thống phòng hiện đại" "0901234503" "karaoke@goodgo.vn" 2>/dev/null)
SHOP_KARAOKE=$(echo "$SHOP_KARAOKE" | grep -oE '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | head -1)
log_ok "Karaoke: $SHOP_KARAOKE"

SHOP_SPA=$(create_shop "Spa GoodGo" "spa-goodgo" "Spa" "Spa và Wellness cao cấp" "0901234504" "spa@goodgo.vn" 2>/dev/null)
SHOP_SPA=$(echo "$SHOP_SPA" | grep -oE '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | head -1)
log_ok "Spa: $SHOP_SPA"

SHOP_BEAUTY=$(create_shop "Thẩm Mỹ GoodGo" "tham-my-goodgo" "Beauty" "Thẩm mỹ viện công nghệ cao" "0901234505" "beauty@goodgo.vn" 2>/dev/null)
SHOP_BEAUTY=$(echo "$SHOP_BEAUTY" | grep -oE '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | head -1)
log_ok "Beauty: $SHOP_BEAUTY"

# If shops already exist, try to fetch existing
if [ -z "$SHOP_CAFE" ]; then
  log_step "Loading existing shops..."
  SHOPS_JSON=$(curl -s "${MERCHANT_URL}/api/v1/shops" -H "Authorization: Bearer $TOKEN")
  eval $(echo "$SHOPS_JSON" | python3 -c "
import sys, json
try:
    d = json.load(sys.stdin)
    items = d if isinstance(d, list) else d.get('data', d.get('items', []))
    if isinstance(items, list):
        for s in items:
            cat = (s.get('category','') or '').lower()
            sid = s.get('id', s.get('shopId', ''))
            if 'cafe' in cat: print(f'SHOP_CAFE=\"{sid}\"')
            elif 'restaurant' in cat: print(f'SHOP_RESTAURANT=\"{sid}\"')
            elif 'karaoke' in cat: print(f'SHOP_KARAOKE=\"{sid}\"')
            elif 'spa' in cat and 'beauty' not in cat: print(f'SHOP_SPA=\"{sid}\"')
            elif 'beauty' in cat: print(f'SHOP_BEAUTY=\"{sid}\"')
except: pass
" 2>/dev/null)
  log_ok "Loaded existing shop IDs"
fi

echo ""
echo "  Café:       $SHOP_CAFE"
echo "  Restaurant: $SHOP_RESTAURANT"
echo "  Karaoke:    $SHOP_KARAOKE"
echo "  Spa:        $SHOP_SPA"
echo "  Beauty:     $SHOP_BEAUTY"

# ═══════════════════════════════════════════════════════════════════════
# Helper: authenticated POST
# ═══════════════════════════════════════════════════════════════════════
api_post() {
  curl -s -w "\n%{http_code}" -X POST "$1" \
    -H 'Content-Type: application/json' \
    -H "Authorization: Bearer $TOKEN" \
    -d "$2"
}

check_result() {
  local label="$1" resp="$2"
  local code=$(echo "$resp" | tail -1)
  if [ "$code" = "201" ] || [ "$code" = "200" ]; then
    log_ok "$label"
  else
    log_warn "$label — HTTP $code"
  fi
}

# ═══════════════════════════════════════════════════════════════════════
# STEP 4: Create Categories
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 4: Create Categories"

cat_create() {
  local r=$(api_post "${CATALOG_URL}/api/v1/categories" \
    "{\"shopId\":\"$1\",\"name\":\"$2\",\"description\":\"$3\",\"displayOrder\":$4}")
  check_result "[$5] $2" "$r"
}

[ -n "$SHOP_CAFE" ] && {
  cat_create "$SHOP_CAFE" "Cà phê" "Các loại cà phê" 1 "Café"
  cat_create "$SHOP_CAFE" "Trà" "Trà xanh, trà đào" 2 "Café"
  cat_create "$SHOP_CAFE" "Sinh tố" "Sinh tố trái cây" 3 "Café"
  cat_create "$SHOP_CAFE" "Bánh ngọt" "Bánh mì, croissant" 4 "Café"
}
[ -n "$SHOP_RESTAURANT" ] && {
  cat_create "$SHOP_RESTAURANT" "Khai vị" "Món khai vị" 1 "Restaurant"
  cat_create "$SHOP_RESTAURANT" "Món chính" "Món chính" 2 "Restaurant"
  cat_create "$SHOP_RESTAURANT" "Tráng miệng" "Tráng miệng" 3 "Restaurant"
  cat_create "$SHOP_RESTAURANT" "Đồ uống" "Nước, bia, rượu" 4 "Restaurant"
}
[ -n "$SHOP_KARAOKE" ] && {
  cat_create "$SHOP_KARAOKE" "Bia" "Các loại bia" 1 "Karaoke"
  cat_create "$SHOP_KARAOKE" "Rượu" "Rượu vang, whisky" 2 "Karaoke"
  cat_create "$SHOP_KARAOKE" "Cocktail" "Cocktail pha chế" 3 "Karaoke"
  cat_create "$SHOP_KARAOKE" "Đồ ăn nhẹ" "Snack, trái cây" 4 "Karaoke"
}
[ -n "$SHOP_SPA" ] && {
  cat_create "$SHOP_SPA" "Massage" "Dịch vụ massage" 1 "Spa"
  cat_create "$SHOP_SPA" "Chăm sóc da" "Facial, skincare" 2 "Spa"
  cat_create "$SHOP_SPA" "Gói combo" "Combo dịch vụ" 3 "Spa"
}
[ -n "$SHOP_BEAUTY" ] && {
  cat_create "$SHOP_BEAUTY" "Tiêm filler" "Filler, botox" 1 "Beauty"
  cat_create "$SHOP_BEAUTY" "Laser" "Trị nám, trẻ hóa" 2 "Beauty"
  cat_create "$SHOP_BEAUTY" "Phẫu thuật" "Nâng mũi, cắt mí" 3 "Beauty"
  cat_create "$SHOP_BEAUTY" "Sản phẩm" "Mỹ phẩm, kem dưỡng" 4 "Beauty"
}

# ═══════════════════════════════════════════════════════════════════════
# STEP 5: Create Products
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 5: Create Products"

prod_create() {
  local r=$(api_post "${CATALOG_URL}/api/v1/products" \
    "{\"shopId\":\"$1\",\"name\":\"$2\",\"price\":$3,\"type\":\"$4\",\"description\":\"$5\",\"sku\":\"$6\"}")
  check_result "$2 (${3}₫)" "$r"
}

[ -n "$SHOP_CAFE" ] && {
  log_step "Café products..."
  prod_create "$SHOP_CAFE" "Cà phê sữa đá" 35000 "PreparedFood" "Cà phê phin truyền thống" "CAFE-001"
  prod_create "$SHOP_CAFE" "Cà phê đen đá" 29000 "PreparedFood" "Cà phê đen đậm đà" "CAFE-002"
  prod_create "$SHOP_CAFE" "Bạc xỉu" 39000 "PreparedFood" "Cà phê sữa nhẹ" "CAFE-003"
  prod_create "$SHOP_CAFE" "Espresso" 45000 "PreparedFood" "Espresso đậm vị" "CAFE-004"
  prod_create "$SHOP_CAFE" "Latte" 55000 "PreparedFood" "Latte kem sữa" "CAFE-005"
  prod_create "$SHOP_CAFE" "Cappuccino" 55000 "PreparedFood" "Cappuccino bọt sữa" "CAFE-006"
  prod_create "$SHOP_CAFE" "Trà đào cam sả" 45000 "PreparedFood" "Trà trái cây tươi" "CAFE-007"
  prod_create "$SHOP_CAFE" "Trà sen vàng" 49000 "PreparedFood" "Trà hoa sen thơm" "CAFE-008"
  prod_create "$SHOP_CAFE" "Sinh tố bơ" 55000 "PreparedFood" "Sinh tố bơ tươi" "CAFE-009"
  prod_create "$SHOP_CAFE" "Croissant bơ" 35000 "PreparedFood" "Croissant nướng giòn" "CAFE-010"
  prod_create "$SHOP_CAFE" "Bánh mì Việt Nam" 30000 "PreparedFood" "Bánh mì thịt nguội" "CAFE-011"
  prod_create "$SHOP_CAFE" "Matcha Latte" 59000 "PreparedFood" "Matcha Nhật Bản" "CAFE-012"
}

[ -n "$SHOP_RESTAURANT" ] && {
  log_step "Restaurant products..."
  prod_create "$SHOP_RESTAURANT" "Gỏi cuốn tôm" 65000 "PreparedFood" "Gỏi cuốn tôm tươi" "REST-001"
  prod_create "$SHOP_RESTAURANT" "Chả giò" 75000 "PreparedFood" "Chả giò giòn rụm" "REST-002"
  prod_create "$SHOP_RESTAURANT" "Phở bò tái" 89000 "PreparedFood" "Phở bò tái nạm gầu" "REST-003"
  prod_create "$SHOP_RESTAURANT" "Bún chả Hà Nội" 95000 "PreparedFood" "Bún chả kèm nem rán" "REST-004"
  prod_create "$SHOP_RESTAURANT" "Cơm tấm sườn" 85000 "PreparedFood" "Cơm tấm sườn bì chả" "REST-005"
  prod_create "$SHOP_RESTAURANT" "Bò lúc lắc" 185000 "PreparedFood" "Bò Úc lúc lắc rau sống" "REST-006"
  prod_create "$SHOP_RESTAURANT" "Cá kho tộ" 145000 "PreparedFood" "Cá lóc kho tộ đất" "REST-007"
  prod_create "$SHOP_RESTAURANT" "Gà nướng mật ong" 165000 "PreparedFood" "Nửa con gà nướng" "REST-008"
  prod_create "$SHOP_RESTAURANT" "Canh chua cá" 95000 "PreparedFood" "Canh chua cá lóc" "REST-009"
  prod_create "$SHOP_RESTAURANT" "Chè khúc bạch" 45000 "PreparedFood" "Chè trái cây khúc bạch" "REST-010"
  prod_create "$SHOP_RESTAURANT" "Bia Sài Gòn" 25000 "PreparedFood" "Bia Sài Gòn lon 330ml" "REST-011"
  prod_create "$SHOP_RESTAURANT" "Nước mía" 20000 "PreparedFood" "Nước mía tươi" "REST-012"
}

[ -n "$SHOP_KARAOKE" ] && {
  log_step "Karaoke menu..."
  prod_create "$SHOP_KARAOKE" "Bia Tiger" 30000 "PreparedFood" "Tiger lon 330ml" "KTV-001"
  prod_create "$SHOP_KARAOKE" "Bia Heineken" 35000 "PreparedFood" "Heineken lon 330ml" "KTV-002"
  prod_create "$SHOP_KARAOKE" "Rượu vang Chile" 350000 "PreparedFood" "Rượu vang đỏ Chile" "KTV-003"
  prod_create "$SHOP_KARAOKE" "Whisky Black Label" 1200000 "PreparedFood" "Johnnie Walker Black" "KTV-004"
  prod_create "$SHOP_KARAOKE" "Mojito" 85000 "PreparedFood" "Cocktail Mojito" "KTV-005"
  prod_create "$SHOP_KARAOKE" "Long Island" 95000 "PreparedFood" "Long Island Iced Tea" "KTV-006"
  prod_create "$SHOP_KARAOKE" "Trái cây tổng hợp" 150000 "PreparedFood" "Đĩa trái cây mix" "KTV-007"
  prod_create "$SHOP_KARAOKE" "Khoai tây chiên" 65000 "PreparedFood" "Khoai tây chiên giòn" "KTV-008"
  prod_create "$SHOP_KARAOKE" "Gà rán" 89000 "PreparedFood" "Gà rán sốt cay" "KTV-009"
  prod_create "$SHOP_KARAOKE" "Nước suối" 15000 "PreparedFood" "Nước suối Aquafina" "KTV-010"
}

[ -n "$SHOP_SPA" ] && {
  log_step "Spa services..."
  prod_create "$SHOP_SPA" "Massage body" 350000 "Service" "Massage toàn thân 60 phút" "SPA-001"
  prod_create "$SHOP_SPA" "Massage chân" 200000 "Service" "Massage chân thư giãn 45 phút" "SPA-002"
  prod_create "$SHOP_SPA" "Facial cơ bản" 300000 "Service" "Chăm sóc da mặt cơ bản" "SPA-003"
  prod_create "$SHOP_SPA" "Facial cao cấp" 550000 "Service" "Chăm sóc da mặt chuyên sâu" "SPA-004"
  prod_create "$SHOP_SPA" "Xông hơi thảo dược" 180000 "Service" "Xông hơi thảo dược 30 phút" "SPA-005"
  prod_create "$SHOP_SPA" "Tắm trắng body" 450000 "Service" "Tắm trắng phi thuyền" "SPA-006"
  prod_create "$SHOP_SPA" "Stone massage" 450000 "Service" "Massage đá nóng 75 phút" "SPA-007"
  prod_create "$SHOP_SPA" "Combo VIP" 850000 "Service" "Massage + Facial + Xông hơi" "SPA-008"
  prod_create "$SHOP_SPA" "Tinh dầu massage" 120000 "Physical" "Tinh dầu oải hương 100ml" "SPA-P01"
  prod_create "$SHOP_SPA" "Kem dưỡng ẩm" 280000 "Physical" "Kem dưỡng da mặt 50g" "SPA-P02"
}

[ -n "$SHOP_BEAUTY" ] && {
  log_step "Beauty services..."
  prod_create "$SHOP_BEAUTY" "Tiêm filler môi" 3500000 "Service" "Filler môi HA 1ml" "BTY-001"
  prod_create "$SHOP_BEAUTY" "Botox trán" 2500000 "Service" "Botox giảm nếp nhăn trán" "BTY-002"
  prod_create "$SHOP_BEAUTY" "Laser trị nám" 1800000 "Service" "Laser Pico trị nám 1 lần" "BTY-003"
  prod_create "$SHOP_BEAUTY" "Trẻ hóa Thermage" 8000000 "Service" "Thermage nâng cơ mặt" "BTY-004"
  prod_create "$SHOP_BEAUTY" "Nâng mũi S-line" 15000000 "Service" "Nâng mũi sụn tự thân" "BTY-005"
  prod_create "$SHOP_BEAUTY" "Cắt mí Hàn Quốc" 12000000 "Service" "Cắt mí mắt công nghệ Hàn" "BTY-006"
  prod_create "$SHOP_BEAUTY" "Mesotherapy" 2000000 "Service" "Meso trắng sáng da" "BTY-007"
  prod_create "$SHOP_BEAUTY" "Serum HA" 450000 "Physical" "Serum Hyaluronic Acid 30ml" "BTY-P01"
  prod_create "$SHOP_BEAUTY" "Kem chống nắng" 350000 "Physical" "Kem chống nắng SPF50 50ml" "BTY-P02"
}

# ═══════════════════════════════════════════════════════════════════════
# STEP 6: Create Tables (Restaurant) + Rooms (Karaoke)
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 6: Create Tables & Rooms"

table_create() {
  local r=$(api_post "${FNB_URL}/api/v1/tables" \
    "{\"shopId\":\"$1\",\"tableNumber\":\"$2\",\"capacity\":$3,\"zone\":\"$4\"}")
  check_result "$2 ($4)" "$r"
}

[ -n "$SHOP_RESTAURANT" ] && {
  log_step "Restaurant tables..."
  table_create "$SHOP_RESTAURANT" "A1" 2 "Tầng 1"
  table_create "$SHOP_RESTAURANT" "A2" 2 "Tầng 1"
  table_create "$SHOP_RESTAURANT" "A3" 4 "Tầng 1"
  table_create "$SHOP_RESTAURANT" "A4" 4 "Tầng 1"
  table_create "$SHOP_RESTAURANT" "A5" 6 "Tầng 1"
  table_create "$SHOP_RESTAURANT" "B1" 4 "Tầng 2"
  table_create "$SHOP_RESTAURANT" "B2" 6 "Tầng 2"
  table_create "$SHOP_RESTAURANT" "B3" 8 "Tầng 2"
  table_create "$SHOP_RESTAURANT" "VIP1" 10 "VIP"
  table_create "$SHOP_RESTAURANT" "VIP2" 12 "VIP"
}

[ -n "$SHOP_KARAOKE" ] && {
  log_step "Karaoke rooms..."
  table_create "$SHOP_KARAOKE" "P01" 6 "Standard"
  table_create "$SHOP_KARAOKE" "P02" 6 "Standard"
  table_create "$SHOP_KARAOKE" "P03" 8 "Standard"
  table_create "$SHOP_KARAOKE" "P04" 8 "Standard"
  table_create "$SHOP_KARAOKE" "P05" 10 "Deluxe"
  table_create "$SHOP_KARAOKE" "P06" 10 "Deluxe"
  table_create "$SHOP_KARAOKE" "V01" 15 "VIP"
  table_create "$SHOP_KARAOKE" "V02" 20 "Party"
}

# ═══════════════════════════════════════════════════════════════════════
# STEP 7: Create Resources (Spa + Beauty)
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 7: Create Spa/Beauty Resources"

res_create() {
  local r=$(api_post "${BOOKING_URL}/api/v1/resources" \
    "{\"shopId\":\"$1\",\"name\":\"$2\",\"resourceType\":\"$3\",\"capacity\":$4}")
  check_result "$2 ($3)" "$r"
}

[ -n "$SHOP_SPA" ] && {
  log_step "Spa resources..."
  res_create "$SHOP_SPA" "Phòng VIP 1" "Room" 1
  res_create "$SHOP_SPA" "Phòng VIP 2" "Room" 1
  res_create "$SHOP_SPA" "Phòng nhóm" "Room" 3
  res_create "$SHOP_SPA" "Giường massage 1" "Bed" 1
  res_create "$SHOP_SPA" "Giường massage 2" "Bed" 1
  res_create "$SHOP_SPA" "Giường massage 3" "Bed" 1
  res_create "$SHOP_SPA" "Phòng xông hơi" "Equipment" 4
}

[ -n "$SHOP_BEAUTY" ] && {
  log_step "Beauty resources..."
  res_create "$SHOP_BEAUTY" "Phòng phẫu thuật 1" "Room" 1
  res_create "$SHOP_BEAUTY" "Phòng phẫu thuật 2" "Room" 1
  res_create "$SHOP_BEAUTY" "Phòng laser" "Room" 1
  res_create "$SHOP_BEAUTY" "Phòng tư vấn" "Room" 2
  res_create "$SHOP_BEAUTY" "Giường tiêm filler" "Bed" 1
  res_create "$SHOP_BEAUTY" "Máy Thermage" "Equipment" 1
}

# ═══════════════════════════════════════════════════════════════════════
# STEP 8: Create Appointments
# ═══════════════════════════════════════════════════════════════════════
log_head "STEP 8: Create Appointments"

TODAY=$(date +%Y-%m-%d)
TOMORROW=$(date -v+1d +%Y-%m-%d 2>/dev/null || date -d "+1 day" +%Y-%m-%d 2>/dev/null || echo "2026-03-01")
DUMMY_SVC="00000000-0000-0000-0000-000000000001"

appt_create() {
  local r=$(api_post "${BOOKING_URL}/api/v1/appointments" \
    "{\"shopId\":\"$1\",\"serviceId\":\"$DUMMY_SVC\",\"startTime\":\"$2\",\"endTime\":\"$3\",\"status\":\"$4\"}")
  check_result "Appt $2 ($4)" "$r"
}

[ -n "$SHOP_SPA" ] && {
  log_step "Spa appointments..."
  appt_create "$SHOP_SPA" "${TODAY}T09:00:00" "${TODAY}T10:00:00" "Confirmed"
  appt_create "$SHOP_SPA" "${TODAY}T10:30:00" "${TODAY}T11:30:00" "Confirmed"
  appt_create "$SHOP_SPA" "${TODAY}T14:00:00" "${TODAY}T15:30:00" "Pending"
  appt_create "$SHOP_SPA" "${TOMORROW}T09:00:00" "${TOMORROW}T10:00:00" "Scheduled"
  appt_create "$SHOP_SPA" "${TOMORROW}T11:00:00" "${TOMORROW}T12:00:00" "Scheduled"
}

[ -n "$SHOP_BEAUTY" ] && {
  log_step "Beauty appointments..."
  appt_create "$SHOP_BEAUTY" "${TODAY}T10:00:00" "${TODAY}T11:00:00" "Confirmed"
  appt_create "$SHOP_BEAUTY" "${TODAY}T13:00:00" "${TODAY}T14:30:00" "Pending"
  appt_create "$SHOP_BEAUTY" "${TOMORROW}T09:00:00" "${TOMORROW}T10:30:00" "Scheduled"
}

# ═══════════════════════════════════════════════════════════════════════
# DONE
# ═══════════════════════════════════════════════════════════════════════
log_head "SEED COMPLETE"
echo ""
echo -e "${GREEN}═══════════════════════════════════════════════════${NC}"
echo -e "${GREEN}  ✓ Demo data seeded successfully!${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════${NC}"
echo ""
echo "  Login:"
echo "    Email:    hongochai10@icloud.com"
echo "    Password: Velik@2026"
echo ""
echo "  Admin:  http://localhost:3001/admin"
echo "  Stores: http://localhost:3001/admin/stores"
echo ""
