#!/bin/bash
# Bulk-Import: All categories for dgray.io
# Usage: DIRECTUS_TOKEN=your_admin_token bash docs/import-categories.sh

API="https://api.dgray.io"
TOKEN="${DIRECTUS_TOKEN:?Set DIRECTUS_TOKEN environment variable}"

create_category() {
    local slug="$1" name="$2" icon="$3" sort="$4" parent="$5"
    local name_de="$6" name_en="$7" name_fr="$8"

    local parent_field="null"
    if [ -n "$parent" ]; then
        parent_field="\"$parent\""
    fi

    local response
    response=$(curl -s -X POST "$API/items/categories" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d "{
            \"name\": \"$name\",
            \"slug\": \"$slug\",
            \"icon\": \"$icon\",
            \"status\": \"published\",
            \"sort\": $sort,
            \"parent\": $parent_field,
            \"translations\": {
                \"create\": [
                    { \"languages_code\": \"de-DE\", \"name\": \"$name_de\" },
                    { \"languages_code\": \"en-US\", \"name\": \"$name_en\" },
                    { \"languages_code\": \"fr-FR\", \"name\": \"$name_fr\" }
                ]
            }
        }")

    local id
    id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)

    if [ -n "$id" ]; then
        echo "$id"
    else
        echo "ERROR: $response" >&2
        echo ""
    fi
}

echo "=== Importing categories into Directus ==="
echo ""

# ── 1. Electronics ──
echo "1/12 Electronics..."
ID_ELECTRONICS=$(create_category "electronics" "Electronics" "💻" 1 "" \
    "Elektronik" "Electronics" "Électronique")
echo "  → $ID_ELECTRONICS"

create_category "phones" "Phones & Tablets" "📱" 1 "$ID_ELECTRONICS" \
    "Handy & Tablets" "Phones & Tablets" "Téléphones & Tablettes" > /dev/null
create_category "computers" "Computers & Accessories" "🖥️" 2 "$ID_ELECTRONICS" \
    "Computer & Zubehör" "Computers & Accessories" "Ordinateurs & Accessoires" > /dev/null
create_category "tv-audio" "TV, Audio & Video" "📺" 3 "$ID_ELECTRONICS" \
    "TV, Audio & Video" "TV, Audio & Video" "TV, Audio & Vidéo" > /dev/null
create_category "gaming" "Gaming & Consoles" "🎮" 4 "$ID_ELECTRONICS" \
    "Gaming & Konsolen" "Gaming & Consoles" "Jeux & Consoles" > /dev/null
create_category "appliances" "Home Appliances" "🏠" 5 "$ID_ELECTRONICS" \
    "Haushaltsgeräte" "Home Appliances" "Électroménager" > /dev/null
create_category "cameras" "Cameras & Photography" "📷" 6 "$ID_ELECTRONICS" \
    "Kameras & Fotografie" "Cameras & Photography" "Appareils photo" > /dev/null
echo "  ✓ 6 subcategories"

# ── 2. Vehicles ──
echo "2/12 Vehicles..."
ID_VEHICLES=$(create_category "vehicles" "Vehicles" "🚗" 2 "" \
    "Fahrzeuge" "Vehicles" "Véhicules")
echo "  → $ID_VEHICLES"

create_category "cars" "Cars" "🚙" 1 "$ID_VEHICLES" \
    "Autos" "Cars" "Voitures" > /dev/null
create_category "motorcycles" "Motorcycles" "🏍️" 2 "$ID_VEHICLES" \
    "Motorräder" "Motorcycles" "Motos" > /dev/null
create_category "bikes" "Bicycles & E-Bikes" "🚲" 3 "$ID_VEHICLES" \
    "Fahrräder & E-Bikes" "Bicycles & E-Bikes" "Vélos & E-Bikes" > /dev/null
create_category "vehicle-parts" "Parts & Accessories" "🔧" 4 "$ID_VEHICLES" \
    "Ersatzteile & Zubehör" "Parts & Accessories" "Pièces & Accessoires" > /dev/null
create_category "boats" "Boats & Watercraft" "⛵" 5 "$ID_VEHICLES" \
    "Boote & Wasserfahrzeuge" "Boats & Watercraft" "Bateaux" > /dev/null
echo "  ✓ 5 subcategories"

# ── 3. Home & Garden ──
echo "3/12 Home & Garden..."
ID_HOME=$(create_category "home-garden" "Home & Garden" "🏡" 3 "" \
    "Haus & Garten" "Home & Garden" "Maison & Jardin")
echo "  → $ID_HOME"

create_category "furniture" "Furniture" "🪑" 1 "$ID_HOME" \
    "Möbel" "Furniture" "Meubles" > /dev/null
create_category "kitchen" "Kitchen & Dining" "🍳" 2 "$ID_HOME" \
    "Küche & Essen" "Kitchen & Dining" "Cuisine" > /dev/null
create_category "garden" "Garden & Outdoor" "🌿" 3 "$ID_HOME" \
    "Garten & Outdoor" "Garden & Outdoor" "Jardin & Extérieur" > /dev/null
create_category "tools" "Tools & Workshop" "🔨" 4 "$ID_HOME" \
    "Werkzeuge & Werkstatt" "Tools & Workshop" "Outils & Atelier" > /dev/null
create_category "decoration" "Decoration & Art" "🖼️" 5 "$ID_HOME" \
    "Deko & Kunst" "Decoration & Art" "Décoration & Art" > /dev/null
create_category "bathroom" "Bathroom & Sanitary" "🚿" 6 "$ID_HOME" \
    "Bad & Sanitär" "Bathroom & Sanitary" "Salle de bain" > /dev/null
echo "  ✓ 6 subcategories"

# ── 4. Fashion & Accessories ──
echo "4/12 Fashion..."
ID_FASHION=$(create_category "fashion" "Fashion & Accessories" "👗" 4 "" \
    "Mode & Accessoires" "Fashion & Accessories" "Mode & Accessoires")
echo "  → $ID_FASHION"

create_category "women" "Women's Fashion" "👚" 1 "$ID_FASHION" \
    "Damenmode" "Women's Fashion" "Mode femme" > /dev/null
create_category "men" "Men's Fashion" "👔" 2 "$ID_FASHION" \
    "Herrenmode" "Men's Fashion" "Mode homme" > /dev/null
create_category "kids-fashion" "Kids' Fashion" "👶" 3 "$ID_FASHION" \
    "Kindermode" "Kids' Fashion" "Mode enfant" > /dev/null
create_category "shoes" "Shoes" "👟" 4 "$ID_FASHION" \
    "Schuhe" "Shoes" "Chaussures" > /dev/null
create_category "watches-jewelry" "Watches & Jewelry" "⌚" 5 "$ID_FASHION" \
    "Uhren & Schmuck" "Watches & Jewelry" "Montres & Bijoux" > /dev/null
create_category "bags" "Bags & Luggage" "👜" 6 "$ID_FASHION" \
    "Taschen & Gepäck" "Bags & Luggage" "Sacs & Bagages" > /dev/null
echo "  ✓ 6 subcategories"

# ── 5. Sports & Leisure ──
echo "5/12 Sports..."
ID_SPORTS=$(create_category "sports" "Sports & Leisure" "⚽" 5 "" \
    "Sport & Freizeit" "Sports & Leisure" "Sports & Loisirs")
echo "  → $ID_SPORTS"

create_category "fitness" "Fitness & Gym" "🏋️" 1 "$ID_SPORTS" \
    "Fitness & Training" "Fitness & Gym" "Fitness" > /dev/null
create_category "outdoor-sports" "Hiking & Outdoor" "🥾" 2 "$ID_SPORTS" \
    "Wandern & Outdoor" "Hiking & Outdoor" "Randonnée & Outdoor" > /dev/null
create_category "winter-sports" "Winter Sports" "⛷️" 3 "$ID_SPORTS" \
    "Wintersport" "Winter Sports" "Sports d'hiver" > /dev/null
create_category "water-sports" "Water Sports" "🏄" 4 "$ID_SPORTS" \
    "Wassersport" "Water Sports" "Sports nautiques" > /dev/null
create_category "team-sports" "Team Sports" "🏀" 5 "$ID_SPORTS" \
    "Mannschaftssport" "Team Sports" "Sports d'équipe" > /dev/null
create_category "cycling" "Cycling Gear" "🚴" 6 "$ID_SPORTS" \
    "Radzubehör" "Cycling Gear" "Équipement cyclisme" > /dev/null
echo "  ✓ 6 subcategories"

# ── 6. Family & Kids ──
echo "6/12 Family & Kids..."
ID_FAMILY=$(create_category "family-kids" "Family & Kids" "👨‍👩‍👧" 6 "" \
    "Familie & Kinder" "Family & Kids" "Famille & Enfants")
echo "  → $ID_FAMILY"

create_category "toys" "Toys & Games" "🧸" 1 "$ID_FAMILY" \
    "Spielzeug & Spiele" "Toys & Games" "Jouets & Jeux" > /dev/null
create_category "baby" "Baby & Toddler" "🍼" 2 "$ID_FAMILY" \
    "Baby & Kleinkind" "Baby & Toddler" "Bébé & Puériculture" > /dev/null
create_category "school" "School Supplies" "🎒" 3 "$ID_FAMILY" \
    "Schulsachen" "School Supplies" "Fournitures scolaires" > /dev/null
echo "  ✓ 3 subcategories"

# ── 7. Books & Media ──
echo "7/12 Books & Media..."
ID_BOOKS=$(create_category "books-media" "Books & Media" "📚" 7 "" \
    "Bücher & Medien" "Books & Media" "Livres & Médias")
echo "  → $ID_BOOKS"

create_category "fiction" "Fiction" "📖" 1 "$ID_BOOKS" \
    "Belletristik" "Fiction" "Fiction" > /dev/null
create_category "nonfiction" "Non-Fiction & Science" "📘" 2 "$ID_BOOKS" \
    "Sachbücher" "Non-Fiction & Science" "Non-fiction" > /dev/null
create_category "textbooks" "Textbooks & Courses" "🎓" 3 "$ID_BOOKS" \
    "Lehrbücher & Kurse" "Textbooks & Courses" "Manuels scolaires" > /dev/null
create_category "music-movies" "Music, Movies & Games" "🎵" 4 "$ID_BOOKS" \
    "Musik, Filme & Spiele" "Music, Movies & Games" "Musique, Films & Jeux" > /dev/null
echo "  ✓ 4 subcategories"

# ── 8. Pets & Animals ──
echo "8/12 Pets..."
ID_PETS=$(create_category "pets" "Pets & Animals" "🐾" 8 "" \
    "Haustiere & Tierzubehör" "Pets & Animals" "Animaux")
echo "  → $ID_PETS"

create_category "pet-supplies" "Pet Supplies" "🦴" 1 "$ID_PETS" \
    "Tierbedarf" "Pet Supplies" "Accessoires animaux" > /dev/null
create_category "pet-adoption" "Adoption & Rehoming" "🐕" 2 "$ID_PETS" \
    "Adoption & Vermittlung" "Adoption & Rehoming" "Adoption" > /dev/null
echo "  ✓ 2 subcategories"

# ── 9. Jobs & Services ──
echo "9/12 Jobs..."
ID_JOBS=$(create_category "jobs" "Jobs & Services" "💼" 9 "" \
    "Jobs & Dienstleistungen" "Jobs & Services" "Emplois & Services")
echo "  → $ID_JOBS"

create_category "full-time" "Full-Time" "🏢" 1 "$ID_JOBS" \
    "Festanstellung" "Full-Time" "CDI" > /dev/null
create_category "part-time" "Part-Time & Mini Jobs" "⏰" 2 "$ID_JOBS" \
    "Teilzeit & Minijobs" "Part-Time & Mini Jobs" "Temps partiel" > /dev/null
create_category "freelance" "Freelance & Remote" "💻" 3 "$ID_JOBS" \
    "Freelance & Remote" "Freelance & Remote" "Freelance & Télétravail" > /dev/null
create_category "services" "Services & Crafts" "🛠️" 4 "$ID_JOBS" \
    "Dienstleistungen & Handwerk" "Services & Crafts" "Services & Artisanat" > /dev/null
echo "  ✓ 4 subcategories"

# ── 10. Real Estate ──
echo "10/12 Real Estate..."
ID_REALESTATE=$(create_category "real-estate" "Real Estate" "🏘️" 10 "" \
    "Immobilien" "Real Estate" "Immobilier")
echo "  → $ID_REALESTATE"

create_category "apartments" "Apartments" "🏢" 1 "$ID_REALESTATE" \
    "Wohnungen" "Apartments" "Appartements" > /dev/null
create_category "houses" "Houses" "🏠" 2 "$ID_REALESTATE" \
    "Häuser" "Houses" "Maisons" > /dev/null
create_category "rooms" "Rooms & WG" "🛏️" 3 "$ID_REALESTATE" \
    "Zimmer & WG" "Rooms & WG" "Chambres & Colocation" > /dev/null
create_category "commercial" "Commercial" "🏪" 4 "$ID_REALESTATE" \
    "Gewerbe" "Commercial" "Commercial" > /dev/null
echo "  ✓ 4 subcategories"

# ── 11. Collectibles & Hobbies ──
echo "11/12 Collectibles..."
ID_COLLECT=$(create_category "collectibles" "Collectibles & Hobbies" "🏆" 11 "" \
    "Sammeln & Hobby" "Collectibles & Hobbies" "Collections & Hobbies")
echo "  → $ID_COLLECT"

create_category "antiques" "Antiques" "🏺" 1 "$ID_COLLECT" \
    "Antiquitäten" "Antiques" "Antiquités" > /dev/null
create_category "coins-stamps" "Coins & Stamps" "🪙" 2 "$ID_COLLECT" \
    "Münzen & Briefmarken" "Coins & Stamps" "Monnaies & Timbres" > /dev/null
create_category "models" "Models & Figures" "🚂" 3 "$ID_COLLECT" \
    "Modellbau & Figuren" "Models & Figures" "Modélisme & Figurines" > /dev/null
create_category "art-crafts" "Art & Handmade" "🎨" 4 "$ID_COLLECT" \
    "Kunst & Handgemacht" "Art & Handmade" "Art & Fait main" > /dev/null
echo "  ✓ 4 subcategories"

# ── 12. Other ──
echo "12/12 Other..."
ID_OTHER=$(create_category "other" "Other" "📦" 12 "" \
    "Sonstiges" "Other" "Autres")
echo "  → $ID_OTHER"

create_category "free-stuff" "Free Stuff" "🎁" 1 "$ID_OTHER" \
    "Zu verschenken" "Free Stuff" "Gratuit" > /dev/null
create_category "barter" "Barter & Trade" "🔄" 2 "$ID_OTHER" \
    "Tauschen" "Barter & Trade" "Troc" > /dev/null
create_category "lost-found" "Lost & Found" "🔍" 3 "$ID_OTHER" \
    "Fundsachen" "Lost & Found" "Objets trouvés" > /dev/null
echo "  ✓ 3 subcategories"

echo ""
echo "=== Import complete! ==="
echo "12 main categories + 59 subcategories created."
