-- Insert list_item with natural keys (no synthetic list_id)
-- This prevents foreign key violations when indexing list items
-- Parameters: $1=actor_id(INT4), $2=subject_actor_id(i32), $3=cid(bytea), $4=rkey,
--             $5=list_owner_actor_id(INT4), $6=list_rkey(TEXT)
-- 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 list_items (actor_id, rkey, cid, list_owner_actor_id, list_rkey, subject_actor_id)
SELECT
    $1,  -- actor_id (provided by caller)
    $4,  -- rkey (INT8)
    $3,  -- cid (embedded, already bytea)
    $5,  -- list_owner_actor_id (already resolved, may be NULL)
    $6,  -- list_rkey (already resolved, may be NULL)
    $2::int4  -- subject_actor_id (already resolved, cast to int4)
WHERE $1::int4 IS NOT NULL  -- Only insert if owner exists
  AND $2::int4 IS NOT NULL  -- Only insert if subject exists
ON CONFLICT (actor_id, rkey) DO UPDATE SET
    cid=EXCLUDED.cid,
    list_owner_actor_id=EXCLUDED.list_owner_actor_id,
    list_rkey=EXCLUDED.list_rkey,
    subject_actor_id=EXCLUDED.subject_actor_id
