#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────────────
# CLC Fishery — One-command database setup
#
# Run this ONCE after cloning the project or after the Prisma migration:
#   chmod +x scripts/setup-db.sh
#   ./scripts/setup-db.sh
#
# What it does:
#   1. Installs/updates npm dependencies (adds prisma + @prisma/client)
#   2. Generates the Prisma client
#   3. Creates the SQLite database and all tables
#   4. Imports any existing data from ./data/*.json
# ─────────────────────────────────────────────────────────────────────────────

set -e
cd "$(dirname "$0")/.."

echo ""
echo "═══════════════════════════════════════════"
echo "  CLC Fishery — Database Setup"
echo "═══════════════════════════════════════════"
echo ""

# Step 1 — Install dependencies
echo "📦  Installing dependencies..."
npm install
echo ""

# Step 2 — Run Prisma migration (creates DB + tables)
echo "🗄️   Running Prisma migration..."
npx prisma migrate dev --name init
echo ""

# Step 3 — Seed from existing JSON files
echo "🌱  Seeding database from existing JSON data..."
node prisma/seed.js
echo ""

echo "═══════════════════════════════════════════"
echo "  ✅  Setup complete!"
echo ""
echo "  Start the dev server:  npm run dev"
echo "  Browse the database:   npm run db:studio"
echo "═══════════════════════════════════════════"
echo ""
