1-- Insert/update list with self-contained schema (no records table)
2-- Parameters: $1=actor_id(INT4), $2=cid(bytea), $3=list_type, $4=name, $5=description,
3-- $6=description_facets, $7=avatar_cid, $8=rkey
4-- Note: created_at is derived from TID rkey
5-- Note: actor_id is provided by caller after calling get_actor_id (ensures zero sequence waste)
6INSERT INTO lists (actor_id, rkey, cid, owner_actor_id, list_type, name, description, description_facets, avatar_cid)
7SELECT
8 $1, -- actor_id (provided by caller)
9 $8, -- rkey (INT8)
10 $2::bytea, -- cid (embedded)
11 $1, -- owner_actor_id (same as actor_id for lists)
12 $3::text::list_type,
13 $4, -- name
14 $5, -- description
15 $6, -- description_facets
16 $7 -- avatar_cid
17ON CONFLICT (actor_id, rkey) DO UPDATE SET
18 cid=EXCLUDED.cid,
19 list_type=EXCLUDED.list_type,
20 name=EXCLUDED.name,
21 description=EXCLUDED.description,
22 description_facets=EXCLUDED.description_facets,
23 avatar_cid=EXCLUDED.avatar_cid,
24 status='complete'::record_status
25RETURNING XMAX::text::int
26