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