#!/bin/sh
#
# Seed the forum (site-level discussions) with sample data for testing.
# Usage: ./scripts/seed-forum.sh [path/to/forge.db]
#
set -e

DB="${1:-forge.db}"

if [ ! -f "$DB" ]; then
    echo "Database not found: $DB"
    echo "Usage: $0 [path/to/forge.db]"
    exit 1
fi

echo "Seeding forum in $DB ..."

sqlite3 "$DB" <<'SQL'
-- Discussion 1: welcome / intro
INSERT INTO discussions (repo, title, body, author, created_at)
VALUES ('_site', 'Welcome to the Radiant forum',
'Hey everyone, this is the general discussion space for *all things Radiant*.

Feel free to introduce yourself, ask questions, or share what you are working on. Check out the project at <https://radiant.dev> if you have not already.

A few ground rules:
- Be kind and constructive.
- Stay on topic (use per-repo discussions for repo-specific issues).
- Have fun.',
'cloudhead.io', datetime('now', '-14 days'));

-- Replies to discussion 1
INSERT INTO replies (discussion_id, body, author, created_at)
VALUES (last_insert_rowid(), 'Thanks for setting this up! *Excited* to be here.',
'alice.bsky.social', datetime('now', '-13 days'));

INSERT INTO replies (discussion_id, body, author, created_at)
VALUES ((SELECT MAX(id) FROM discussions), 'Great to see a forum that works without JavaScript. The source is really clean too.',
'bob.bsky.social', datetime('now', '-12 days'));

INSERT INTO replies (discussion_id, body, author, created_at)
VALUES ((SELECT MAX(id) FROM discussions), 'Agreed, this is refreshing. I found the contributing guide at <https://radiant.dev/contributing> very helpful. Looking forward to contributing.',
'alice.bsky.social', datetime('now', '-11 days'));

-- Discussion 2: build question
INSERT INTO discussions (repo, title, body, author, created_at)
VALUES ('_site', 'Cross-compiling forge for ARM?',
'Has anyone tried cross-compiling the forge binary for ARM (e.g. Raspberry Pi)?

I tried `GOARCH=arm64 go build .` and it built fine, but I am wondering if there are any gotchas with the SQLite dependency on *musl* vs *glibc*.',
'bob.bsky.social', datetime('now', '-10 days'));

INSERT INTO replies (discussion_id, body, author, created_at)
VALUES (last_insert_rowid(), 'I run it on a Pi 4 with no issues. Here is what I use:

```
export CGO_ENABLED=1
export CC=aarch64-linux-gnu-gcc
GOARCH=arm64 go build .
```

See <https://go.dev/doc/install/source#environment> for the full list of env vars.',
'alice.bsky.social', datetime('now', '-9 days'));

INSERT INTO replies (discussion_id, body, author, created_at)
VALUES ((SELECT MAX(id) FROM discussions), 'Thanks, that did the trick! Setting `CGO_ENABLED=1` was the key part I was missing.',
'bob.bsky.social', datetime('now', '-8 days'));

-- Discussion 3: feature idea
INSERT INTO discussions (repo, title, body, author, created_at)
VALUES ('_site', 'Idea: RSS feed for repository updates',
'It would be nice to have an RSS/Atom feed at `/feed` or `/rss` that lists recent commits across all public repos. That way people could subscribe and stay up to date without polling the web UI.

The Atom spec is at <https://www.rfc-editor.org/rfc/rfc4287>. Anyone else interested in this?',
'alice.bsky.social', datetime('now', '-7 days'));

INSERT INTO replies (discussion_id, body, author, created_at)
VALUES (last_insert_rowid(), '*Yes!* I would use this. Per-repo feeds would be great too.',
'bob.bsky.social', datetime('now', '-6 days'));

INSERT INTO replies (discussion_id, body, author, created_at)
VALUES ((SELECT MAX(id) FROM discussions), 'I might take a crack at this. The handler would be something like:

```go
func (s *server) handleFeed(w http.ResponseWriter, r *http.Request) {
    commits := s.getRecentCommits(20)
    w.Header().Set("Content-Type", "application/atom+xml")
    renderAtomFeed(w, commits)
}
```

Seems straightforward to shell out to `git log --all` for the data.',
'cloudhead.io', datetime('now', '-4 days'));

-- Discussion 4: a shorter thread
INSERT INTO discussions (repo, title, body, author, created_at)
VALUES ('_site', 'Syntax highlighting for more languages?',
'Currently forge ships `hirad.js` and `hiril.js` for Radiance and RIL. Any plans to support other languages, or is the intent to keep it *minimal*?',
'bob.bsky.social', datetime('now', '-3 days'));

INSERT INTO replies (discussion_id, body, author, created_at)
VALUES (last_insert_rowid(), 'I think keeping it minimal is the right call. You can always drop your own highlighter scripts into `static/js/` and rebuild. The embed directive picks them up automatically.',
'cloudhead.io', datetime('now', '-2 days'));

-- Discussion 5: no replies yet
INSERT INTO discussions (repo, title, body, author, created_at)
VALUES ('_site', 'Deploying behind nginx reverse proxy',
'I am trying to set up forge behind nginx with a `/git` base URL. I pass `-base-url /git` but static assets (CSS, fonts) are not loading. Here is what I have so far:

```
location /git/ {
    proxy_pass http://127.0.0.1:8080/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
```

I followed the reverse proxy guide at <https://nginx.org/en/docs/http/ngx_http_proxy_module.html> but *something* is still off. Has anyone got a working config they can share?',
'alice.bsky.social', datetime('now', '-1 days'));

-- Avatars
INSERT OR IGNORE INTO avatars (handle, url) VALUES
  ('cloudhead.io',       '/avatars/cloudhead.svg'),
  ('alice.bsky.social',  '/avatars/alice.svg'),
  ('bob.bsky.social',    '/avatars/bob.svg');

SQL

echo "Done. Inserted 5 discussions with replies and avatars."
